Payment Initiation

Good to know: payment initiation is responsible for bank and card payment support.

Implement invocation function in native module

In order to configure kevin. mobile SDK payment initiation module, you will need to add configurations into the native platform specific components.

Android

KevinModule.kt
class KevinModule internal constructor(context: ReactApplicationContext?) :
    ReactContextBaseJavaModule(context) {

    private var callback: Callback? = null

    //...

    @ReactMethod
    fun openKevinBankPayment(state: String, callback: Callback) {
        try {
            val paymentConfiguration = PaymentSessionConfiguration.Builder(id)
                .setPaymentType(PaymentType.BANK)
                .setPreselectedCountry(KevinCountry.LITHUANIA)
                .build()
            val intent = Intent(currentActivity, PaymentSessionActivity::class.java)
            intent.putExtra(PaymentSessionContract.CONFIGURATION_KEY, paymentConfiguration)
            currentActivity?.startActivityForResult(intent, REQUEST_CODE_PAYMENT, null)
            this.callback = callback
        } catch (error: Exception) {
            callback("Failed", null)
        }
    }
    
    companion object {
        const val REQUEST_CODE_PAYMENT = 101
    }
}

iOS

KevinModule.swift
@objc(KevinModule)
class KevinModule: NSObject {
  
  private var callback: RCTResponseSenderBlock?
    
  //...
  
  @objc(openKevinBankPayment::)
  func openKevinBankPayment(id: String, callback: @escaping RCTResponseSenderBlock) -> Void {
    do {
      KevinPaymentSession.shared.delegate = self
      try KevinPaymentSession.shared.initiatePayment(
        configuration: KevinPaymentSessionConfiguration.Builder(
          paymentId: id
        )
          .setPreselectedCountry(.lithuania)
          .setSkipBankSelection(false)
          .build()
      )
      
      self.callback = callback
    } catch {
      callback(["Failed", nil])
    }
  }
}

Additionally add this function declaration to your native module bridge:

KevinModuleBridge.m
@interface RCT_EXTERN_MODULE(KevinModule, NSObject)

//...

RCT_EXTERN_METHOD(openKevinBankPayment:(NSString):(RCTResponseSenderBlock))

@end

For additional information on available SDK parameters, please refer to the corresponding platform-specific section of our documentation.

Handle SDK callbacks

To get results from kevin. SDK payment initiation flow you need to override platform-specific callbacks.

Android

Android handler module should conform to ActivityEventListener and should override onActivityResult(activity: Activity?, requestCode: Int, resultCode: Int, data: Intent?)

KevinModule.kt
class KevinModule internal constructor(context: ReactApplicationContext?) :
    ReactContextBaseJavaModule(context), ActivityEventListener {

    //...

    override fun onActivityResult(
        activity: Activity?,
        requestCode: Int,
        resultCode: Int,
        data: Intent?
    ) {
        if (requestCode == REQUEST_CODE_PAYMENT) {
            val result = data?.getParcelableExtra<SessionResult<PaymentSessionResult>>(PaymentSessionContract.RESULT_KEY)
            when (result) {
                is SessionResult.Success -> {
                    callback?.invoke(null, result.value.paymentId)
                }
                is SessionResult.Canceled -> {
                    callback?.invoke("Canceled", null)
                }
                is SessionResult.Failure -> {
                    callback?.invoke("Failed", null)
                }
            }
        }
    }
}

iOS

iOS handler module should conform to KevinPaymentSessionDelegate and override those methods:

KevinModule.swift
@objc(KevinModule)
class KevinModule: NSObject, KevinAccountLinkingSessionDelegate {

    //...

    func onKevinPaymentInitiationStarted(controller: UINavigationController) {
        let rootViewController = UIApplication.shared.windows.first!.rootViewController
        rootViewController?.present(controller, animated: true, completion: nil)
    }
    
    func onKevinPaymentCanceled(error: Error?) {
        callback?(["Failed", nil])
    }
    
    func onKevinPaymentSucceeded(paymentId: String) {
        callback?([nil, paymentId])
    }
}

Invoke payment initiation flow from React Native code

App.js
const { 
  KevinModule 
} = NativeModules;

onKevinPaymentCallback = (error, paymentId) => {
  if (error) {
    console.error(`Error found! ${error}`);
  } else {
    console.log(`Payment id ${paymentId} returned`);
  }
}

const App: () => Node = () => {
  return (
    <SafeAreaView>
      <Button
        onPress={ () => KevinModule.openKevinBankPayment("ID", onKevinPaymentCallback) }
        title="Bank payment"
      />
    </SafeAreaView>
  );
};

Last updated