sendWebhook method

Future<PaymentWebhookResult> sendWebhook({
  1. required String paymentId,
  2. required num amount,
  3. required String externalUserId,
  4. String status = 'paid',
  5. String provider = 'generic',
  6. int? timestamp,
  7. String? signature,
  8. String? webhookSecret,
})

POST /api/digital-wallet/webhooks/payments

Implementation

Future<PaymentWebhookResult> sendWebhook({
  required String paymentId,
  required num amount,
  required String externalUserId,
  String status = 'paid',
  String provider = 'generic',
  int? timestamp,
  String? signature,
  String? webhookSecret,
}) async {
  if (amount <= 0) {
    throw WalletApiError('amount must be > 0', statusCode: 400);
  }
  if (paymentId.trim().isEmpty) {
    throw WalletApiError('paymentId is required', statusCode: 400);
  }

  final body = <String, dynamic>{
    'paymentId': paymentId,
    'amount': amount,
    'externalUserId': externalUserId,
    'status': status,
    'provider': provider,
  };

  Map<String, String>? headers;
  WalletAuthMode auth;

  if (signature != null && timestamp != null) {
    headers = {
      'X-Payments-Timestamp': '$timestamp',
      'X-Payments-Signature': normalizeWebhookSignature(signature),
    };
    auth = WalletAuthMode.none;
  } else if (webhookSecret != null && webhookSecret.isNotEmpty) {
    final signed = signPaymentWebhook(
      secret: webhookSecret,
      paymentId: paymentId,
      amount: amount,
      externalUserId: externalUserId,
      status: status,
      timestamp: timestamp,
    );
    headers = signed.headers;
    auth = WalletAuthMode.none;
  } else {
    auth = WalletAuthMode.payments;
  }

  final data = await _http.request(
    'POST',
    PaymentsEndpoints.webhooksPayments,
    body: body,
    auth: auth,
    extraHeaders: headers,
  );
  return PaymentWebhookResult.fromJson(data ?? body);
}