addInvoice method

Future<Map<String, dynamic>> addInvoice({
  1. required double amount,
  2. required String clientMobile,
  3. required String clientName,
  4. String? clientEmail,
  5. required String orderNumber,
  6. required String callBackUrl,
  7. String? cancelUrl,
  8. String? currency,
  9. required List<PaylinkProduct> products,
  10. String? note,
  11. String? smsMessage,
  12. List<String>? supportedCardBrands,
  13. bool displayPending = true,
})

Add invoice to Paylink.

amount - The total amount of the invoice. NOTE: Buyer will pay this amount regardless of the total amounts of the products' prices. clientMobile - The mobile number of the client. clientName - The name of the client. clientEmail - The email address of the client. orderNumber - A unique identifier for the invoice. callBackUrl - Call back URL that will be called by the Paylink to the merchant system. This callback URL will receive two parameters: orderNumber, and transactionNo. cancelUrl - Call back URL to cancel orders that will be called by the Paylink to the merchant system. This callback URL will receive two parameters: orderNumber, and transactionNo. currency - The currency code of the invoice. The default value is SAR. (e.g., USD, EUR, GBP). products - An array of PaylinkProduct objects to be included in the invoice. note - A note for the invoice. smsMessage - This option will enable the invoice to be sent to the client's mobile specified in clientMobile. supportedCardBrands - List of supported card brands. This list is optional. values are: mada, visaMastercard, amex, tabby, tamara, stcpay, urpay displayPending - This option will make this invoice displayed in my.paylink.sa

Returns a map containing invoice details.

Implementation

Future<Map<String, dynamic>> addInvoice({
  required double amount,
  required String clientMobile,
  required String clientName,
  String? clientEmail,
  required String orderNumber,
  required String callBackUrl,
  String? cancelUrl,
  String? currency,
  required List<PaylinkProduct> products,
  String? note,
  String? smsMessage,
  List<String>? supportedCardBrands,
  bool displayPending = true,
}) async {
  try {
    if (paymentToken == null) await _authenticate();

    /// Filter and sanitize supportedCardBrands
    List<String> filteredCardBrands = filterCardBrands(supportedCardBrands);

    /// Convert PaylinkProduct objects to maps
    List<Map<String, dynamic>> productsArray = [];
    if (products.isNotEmpty) {
      for (var product in products) {
        productsArray.add(product.toMap());
      }
    }

    /// Request body parameters
    Map<String, dynamic> requestBody = {
      'amount': amount,
      'callBackUrl': callBackUrl,
      'cancelUrl': cancelUrl,
      'clientEmail': clientEmail,
      'clientMobile': clientMobile,
      'currency': currency,
      'clientName': clientName,
      'note': note,
      'orderNumber': orderNumber,
      'products': productsArray,
      'smsMessage': smsMessage,
      'supportedCardBrands': filteredCardBrands,
      'displayPending': displayPending,
    };

    final response = await http.post(
      Uri.parse('$apiLink/api/addInvoice'),
      headers: {
        'accept': '*/*',
        'content-type': 'application/json',
        'Authorization': 'Bearer $paymentToken',
      },
      body: jsonEncode(requestBody),
    );

    if (response.statusCode != 200) {
      throw Exception('Failed to add the invoice: ${response.body}');
    }

    /// Decode the order details
    Map<String, dynamic> orderDetails = json.decode(response.body);

    return orderDetails;
  } catch (e) {
    rethrow;
  }
}