Programmatically start the SDK
In case your app doesn't automatically start the SDK
SDKBetaLast updated: October 1st 2021, @ 12:45:16 pm
If you use a custom UI to launch the PayPal Checkout experience, you must programmatically start the PayPal Mobile Checkout SDK from either an activity or a ViewModel.
Launch the SDK from an activity
Register all callbacks to the
onCreate()
method of your activity (or to theonAttach()
method of your fragment). This enables you to listen to certain key events in the checkout process, such as order approvals, orders cancellations, or errors.Note: If the activity is destroyed or re-created, you must register the callbacks again.
Sample code
- Kotlin
- Java
1class YourActivity: Activity {2 override fun onCreate(savedInstanceState: Bundle?) {3 PayPalCheckout.registerCallbacks(4 onApprove = OnApprove { approval ->5 approval.orderActions.capture { captureOrderResult ->6 Log.i("CaptureOrder", "Order successfully captured: $captureOrderResult")7 }8 },9 onCancel = OnCancel {10 // Optional callback for when a buyer cancels the paysheet11 },12 onError = OnError { errorInfo ->13 // Optional error callback14 },15 onShippingChange = OnShippingChange { shippingChangeData, shippingChangeActions ->16 // Optional onShippingChange callback. See update shipping section for more details.17 }18 )19 }20 setupButtons()21 }
- Launch the PayPal Checkout process by invoking
PayPalCheckout.startCheckout(createOrder)
when users select your custom UI button.
Sample code
This sample code creates an order of a single item for $10.00 USD.
- Kotlin
- Java
1PayPalCheckout.startCheckout(2 CreateOrder { createOrderActions ->3 val order = Order(4 intent = OrderIntent.CAPTURE,5 appContext = AppContext(6 userAction = UserAction.PAY_NOW7 ),8 purchaseUnitList = listOf(9 PurchaseUnit(10 amount = Amount(11 currencyCode = CurrencyCode.USD,12 value = "10.00"13 )14 )15 )16 )17 createOrderActions.create(order)18 }19 )
Launch the SDK from a ViewModel
Register all callbacks in the ViewModel constructor.
Note: Don't reference UI objects in your callbacks that might get destroyed by Android.
Code sample
- Kotlin
- Java
1class MyViewModel: ViewModel {2 val _message = MutableLiveData<String>()3 val message: LiveData<String>4 get() = _message5 init {6 PayPalCheckout.registerCallbacks(7 onApprove = OnApprove { approval ->8 message.value = "Order approved"9 },10 onCancel = OnCancel {11 message.value = "Order cancelled"12 },13 onError = OnError { errorInfo ->14 message.value = "Error creating order"15 }16 )17 }18 }
- Launch the PayPal Checkout process by invoking a method in your ViewModel when users select your custom UI button.
Code sample
Our example receives a list of items to add to the order.
- Kotlin
- Java
1class MyViewModel: ViewModel {2 val _message = MutableLiveData<String>()3 val message: LiveData<String>4 get() = _message5 init {6 PayPalCheckout.registerCallbacks(7 onApprove = OnApprove { approval ->8 message.value = "Order approved"9 },10 onCancel = OnCancel {11 message.value = "Order cancelled"12 },13 onError = OnError { errorInfo ->14 message.value = "Error creating order"15 }16 )17 }18 fun startCheckout(items: List<PurchaseUnit>) {19 PayPalCheckout.startCheckout(20 CreateOrder { createOrderActions ->21 val order = Order(22 intent = OrderIntent.CAPTURE,23 appContext = AppContext(24 userAction = UserAction.PAY_NOW25 ),26 purchaseUnitList = items27 )28 createOrderActions.create(order)29 }30 )31 }32 }
- (Optional) Use the
String
to configure the following messages to display to buyers:
- Success message when funds capture successfully.
- Cancellation confirmation when the buyer selects to cancel the order.
- Error message when the capture is unsuccessful.