createWebhook method

Future<Map<String, dynamic>> createWebhook({
  1. required String url,
  2. required List<String> events,
  3. required String secret,
  4. bool testMode = false,
})

Implementation

Future<Map<String, dynamic>> createWebhook({
  required String url,
  required List<String> events,
  required String secret,
  bool testMode = false,
}) async {
  Map<String, dynamic> requestBody = {
    "data": {
      "type": "webhooks",
      "attributes": {
        "url": url,
        "events": events,
        "secret": secret,
      },
      "relationships": {
        "store": {
          "data": {
            "type": "stores",
            "id": "1" // Replace with your actual store ID
          }
        }
      }
    }
  };

  if (testMode) {
    requestBody['data']['attributes']['test_mode'] = true;
  }

  Options dioOptions = Options(
    headers: {
      "Authorization": "Bearer $apiKey",
      "Accept": "application/vnd.api+json",
      "Content-Type": "application/vnd.api+json",
    },
  );

  try {
    Response response = await dio.post(
      "https://api.lemonsqueezy.com/v1/webhooks",
      options: dioOptions,
      data: requestBody,
    );

    return response.data;
  } catch (e) {
    return {'error': e.toString()};
  }
}