Checkout (Unity SDK)
The checkout flow wraps the complete payment experience, allowing users to add and select a payment method, before creating and paying the invoice.
note
If a user would like to add a payment method, they will be redirected to a web browser, where they can securely add their payment method. Upon returning to your application, their list of payment methods will be refreshed and any new payment methods will be included.
Prerequisites
- Authorized User Password Token (A User Password Token is a string that enables Tilia Pay to verify that a request belongs to an authorized session)
- Authorized Invoice ID (The ID for the Authorized invoice you created - see more info below)
note
All methods and flows in the Tilia SDK require you to have a server setup that communicates with Tilia’s APIs (your server will have a private api key which needs to remain secure and should never be exposed to client-side applications). Your client application should communicate with your own api (ensuring user authentication) to retrieve data required by the Tilia SDK.
Server-Side Code Example to create an Authorized Invoice and retrieve required information for the SDK
- Requires your server to retrieve a client credentials access token. See more here .
- Requires an account id for a user that has been registered with Tilia. See more here .
- Requires a payload outlining the details of your Authorized Invoice. See more here .
curl --location --request POST 'https://invoicing.staging.tilia-inc.com/v2/authorize/invoice' \
--header 'Content-Type: application/json' \
--header 'Authorization: Bearer CLIENT_CREDENTIALS_ACCESS_TOKEN' \
--data-raw '{
"account_id": "USER_ACCOUNT_ID",
"is_escrow": false,
"invoice_mechanism": "api",
"reference_type": "My test order",
"reference_id": "123456789",
"line_items": [
{
"description": "Apple",
"product_sku": "sku_apple",
"transaction_type": "user_to_integrator",
"currency": "USD",
"amount": 122,
"recipients": []
},
{
"description": "Orange",
"product_sku": "sku_orange",
"transaction_type": "user_to_integrator",
"currency": "USD",
"amount": 229,
"recipients": []
},
{
"description": "Dragonfruit",
"product_sku": "sku_dragonfruit",
"transaction_type": "user_to_integrator",
"currency": "USD",
"amount": 3299,
"recipients": []
}
]
}'
Example Response Payload
{
"status": "Success",
"message": [],
"codes": [],
"payload": {
"authorized_invoice_id": "AUTHORIZED_INVOICE_ID",
"password_token": "AUTHORIZED_USER_PASSWORD_TOKEN"
}
}
Your api should return the authorized_invoice_id
and password_token
to your client application to use in the Tilia SDK.
C# Code Example
This code snippet is an example of how to execute the Checkout Flow from inside your own code.
- This example assumes that your code contains a variable called TiliaManager that is a valid reference to a TiliaUIManager class that is active in your Unity scene (part of the standard Tilia Canvas Group).
- TiliaUIMessages.Message is a static function that assumes you have the TiliaUIMessages class active in your Unity scene (part of the standard Tilia Canvas Group).
public void DoCheckoutFlow()
{
// AuthorizedInvoice here as an input to CheckoutFlow must be a valid TiliaAuthorizedInvoiceToken object.
TiliaManager.CheckoutFlow(AuthorizedInvoice,
(result) => {
// Completed
// result is a TiliaEvent class
// result.Data is the invoice ID
// result.Payload is a TiliaInvoice class object containing all of the details of the successfully paid invoice.
TiliaUIMessages.Message("The user paid the invoice.");
},
(result) => {
// Update
// result is a TiliaEvent class
TiliaUIMessages.Message("The user paid the invoice.");
},
(result) => {
// Canceled
// result is a TiliaEvent class
TiliaUIMessages.Message("The user canceled the checkout flow.");
},
(result) => {
// Error
// result is a TiliaEvent class
// result.Payload is an object of TiliaResponse type which includes API error data such as the web server response code and any API specific messages.
TiliaUIMessages.Message("There was an error while attempting to execute checkout flow.");
});
}
GetPaymentMethods
In the case of a virtual purchase, the authorized invoice call will fail if the user does not have enough funds in their spendable token balance to cover the invoice. If you would like, you can employ the GetPaymentMethods function to see if their balance will cover the invoice.
Example
TiliaReturnCode GetPaymentMethods(Action onComplete, TiliaToken authToken = null)
- onComplete: Action callback event. Callback happens on both success and failure.
- authToken: Optional authentication token to use. If not specified, will default to SDK global auth token.