Error recovery

Previous transaction lookup

If the application crashes or is terminated during a transaction, it is useful to know what happened to the transaction that was in progress. To look up the state of a previous transaction, call:

1YocoSDK.getPaymentResult(transactionID: transactionID) { paymentResult, error in
2 ...
3}

This will allow you to get the PaymentResult of the transaction so you can determine if it was a success or failure.

Showing the user the result

After receiving the PaymentResult above, you probably want to show the user the result (and allow them to send a receipt). The YocoSDK has a function to allow you to present the result of the payment and send a receipt via the function below:

1YocoSDK.showPaymentResult(paymentResult, paymentParameters: parameters) { paymentResult in
2 ...
3}

You can pass the same PaymentParameters object you normally use with the YocoSDK.charge() method. You can chain both the above methods in your application’s initialisation to automatically display a previous transaction:

1// If something happened during a transaction, attempt to get the transaction status and display the result to the user.
2if let transactionID = UserDefaults.standard.value(forKey: "transactionInProgress") as? String {
3 YocoSDK.getPaymentResult(transactionID: transactionID) { paymentResult, error in
4 if let paymentResult = paymentResult {
5 YocoSDK.showPaymentResult(paymentResult, paymentParameters: parameters) { paymentResult in
6 UserDefaults.standard.set(nil, forKey: "transactionInProgress") // Clear the transaction ID we saved, so we don't accidentally show it again.
7 }
8 }
9 }
10}