Handling receipts

If you wish to show your own receipt handling view after a payment is complete, then you do not need to implement this delegate. By default if you do not implement the delegate the receipt screen in the YocoSDK will not show. Implement the completion handler as part of the Yoco.charge() to be notified when the payment is complete and you can then show your own screen.

Step 1: Implement Protocol

You will need to inherit from the YocoReceiptDelegate to be able to listen for callbacks from the receipt screen.

import YocoSDK
class ViewController: UIViewController, YocoReceiptDelegate {
func supportedReceiptTypes() -> [SupportedReceiptType] {
return [.email, .sms, .print]
}
}

Step 2: Implement Optional functions

To get notified of the receipt buttons being pressed inside the YocoSDK, you will need to implement the callback function that is appropriate to the SupportedReceiptType that you had specified in the supportedReceiptTypes() function. It is up to you to decide how the receipt is handled. Here is an example of one type of receipt being handled.

extension ViewController: YocoReceiptDelegate {
func supportedReceiptTypes() -> [SupportedReceiptType] {
return [.email]
}
// MARK: - optional delegate methods
func sendEmailReceipt(address: String, paymentResult: PaymentResult, progress: @escaping UpdateReceiptProgress) {
progress(.inProgress())
Webservice.validateAndSendEmailReceipt(emailAddress: address) { isValid in
if isValid {
progress(.complete())
} else {
progress(.error(message: "Email is invalid"))
}
}
}
}

We can see in this example the use of the progress parameter. This allows you to control the receipt screen's button's state. You can call this function multiple times and must be called on the main thread. The list of states the button can be in are defined here:

public enum ReceiptState {
case initial
case inProgress(message: String? = nil)
case error(message: String)
case complete(message: String? = nil)
}

Step 3: Register the delegate

You will need to pass the YocoReceiptDelegate to the YocoSDK as part of the Config.

import YocoSDK
class ViewController: UIViewController, YocoReceiptDelegate {
func charge(amount: UInt64) {
let parameters = PaymentParameters(receiptDelegate: self)
Yoco.charge(amount, parameters: parameters)
}
// ...
}

You now should have everything you need to handle receipts!