startPayment method

Future<Map> startPayment(
  1. Map options
)

Implementation

Future<Map<dynamic, dynamic>> startPayment(
  Map<dynamic, dynamic> options,
) async {
  /// required for sending value after the data has been populated
  var completer = Completer<Map<String, dynamic>>();

  /// reply that to be return
  var reply = <String, dynamic>{};

  /// collect data from razorpay response
  var data = <String, dynamic>{};

  /// check retry enabled in options
  /// true (default): Enables customers to retry payments.
  bool isRetryEnabled = _retryStatus(options);

  options['handler'] = allowInterop((dynamic res) {
    final response = _mapify(res);

    reply['type'] = _CODE_PAYMENT_SUCCESS;
    data['razorpay_payment_id'] = response?['razorpay_payment_id'];
    data['razorpay_order_id'] = response?['razorpay_order_id'];
    data['razorpay_signature'] = response?['razorpay_signature'];
    reply['data'] = data;

    /// send payment success reply
    completer.complete(reply);
  });

  options['modal.ondismiss'] = allowInterop((response) {
    if (!completer.isCompleted) {
      reply['type'] = _CODE_PAYMENT_ERROR;
      data['code'] = PAYMENT_CANCELLED;
      data['message'] = 'Payment processing cancelled by user';
      reply['data'] = data;

      /// send payment cancel reply
      completer.complete(reply);
    }
  });

  // assign options
  _jsRazorpay = JsRazorpay(
    options: options,
    onFailed: (res) {
      /// if retry is enabled then do not
      /// complete completer
      if (!isRetryEnabled) {
        final response = _mapify(res);

        reply['type'] = _CODE_PAYMENT_ERROR;
        data['code'] = BASE_REQUEST_ERROR;
        // final  errorResponse = response?['error'];
        data['message'] = response?['error']['description'];

        var metadata = <String, dynamic>{};
        metadata['payment_id'] = response?['error']['metadata']['payment_id'];
        data['metadata'] = metadata;

        data['source'] = response?['error']['source'];
        data['step'] = response?['error']['step'];
        reply['data'] = data;

        /// send payment failure reply
        completer.complete(reply);
      }
    },
  );

  // open payment gateway.
  // _jsRazorpay not null.
  _jsRazorpay?.open();

  return completer.future;
}