-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBoltBasicExample.cs
More file actions
executable file
·128 lines (112 loc) · 5.27 KB
/
BoltBasicExample.cs
File metadata and controls
executable file
·128 lines (112 loc) · 5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using System.Threading.Tasks;
using UnityEngine;
using BoltApp;
namespace BoltApp.Samples
{
/// <summary>
/// Basic example showing how to integrate the Bolt SDK and handle payment links
/// Use this as reference for your own implementation.
/// </summary>
public class BoltBasicExample : MonoBehaviour
{
private BoltSDK boltSDK;
private bool checkoutIsOpen = false;
void Start()
{
var boltConfig = new BoltConfig(
"com.myapp.test",
"example.publishable.key",
BoltConfig.Environment.Development);
// Setup SDK
boltSDK = new BoltSDK(boltConfig);
// Setup callbacks, handle flows appropriately
boltSDK.onTransactionComplete += OnTransactionComplete;
boltSDK.onTransactionFailed += OnTransactionFailed;
boltSDK.onWebLinkOpen += onWebLinkOpen;
// The SDK handles user data automatically. Use as needed in your APIs and Analytics.
var user = boltSDK.GetBoltUser();
Debug.Log("User: " + user.ToString());
// Open A Checkout Link, typically assigned to a button click in your UI
// Note: SDK automatically stores a pending payment link in player prefs, used in 'VerifyRecentCheckouts()'
var checkoutLinkFetchedFromYourBackend = "https://knights-of-valor-bolt.c-staging.bolt.com/c?u=Fv8ZMmDmRb86C4XRiB92x2&publishable_key=_Kq5XZXqaLiS.3TOhnz9Wmacb.9c59b297d066e94294895dd8617ad5d9d8ffc530fe1d36f8ed6d624a4f7855ae";
boltSDK.OpenCheckout(checkoutLinkFetchedFromYourBackend);
// Note: On app load you need to check for recent checkouts.
// Look at 'VerifyRecentCheckouts()' in this file for an example.
}
/// <summary>
/// If user manually opens the app again, check the status of the latest transactions.
/// </summary>
async Task OnApplicationFocus(bool hasFocus)
{
if (hasFocus)
{
// The app is refocused and we previously tracked checkout open with the onWebLinkOpen callback
// Therefore, the user returned to the app after web checkout but not via deep link
if (checkoutIsOpen)
{
VerifyRecentCheckouts();
}
}
// Web checkout is closed, make sure to update any of your UI variables
checkoutIsOpen = false;
}
/// <summary>
/// Verify the status of the latest checkouts with the backend server
/// </summary>
private async void VerifyRecentCheckouts()
{
// Check status of latest transaction with backend server
var pendingPaymentLinkSessions = boltSDK.GetPendingPaymentLinkSessions();
if (pendingPaymentLinkSessions.Count > 0)
{
foreach (var keyValuePair in pendingPaymentLinkSessions)
{
var paymentLinkSession = keyValuePair.Value;
// Call your backend to verify the payment link status
var paymentLinkResult = await VerifyPaymentLinkSuccess(paymentLinkSession.PaymentLinkId);
// Resolve the payment link session with the result
// Note: This SDK call will trigger the onTransactionComplete or onTransactionFailed callbacks
boltSDK.ResolvePaymentLinkSession(paymentLinkSession.PaymentLinkId, paymentLinkResult);
}
}
}
/// <summary>
/// Mock helper function to verify transaction with backend server
/// </summary>
/// <param name="paymentLinkId">The payment link ID to verify</param>
/// <returns>The payment link status</returns>
private async Task<PaymentLinkStatus> VerifyPaymentLinkSuccess(string paymentLinkId)
{
// TODO: Use your http client to call backend to verify the payment link status
// In that server call you can check for a webhook or perform a GET on the payment link ID
// Example response
return PaymentLinkStatus.Successful;
}
/// <summary>
/// When transaction is complete, this callback will be called.
/// Show a success screen and fire any analytic events here.
/// </summary>
private void OnTransactionComplete(PaymentLinkSession paymentLinkSession)
{
Debug.Log("Transaction complete: " + paymentLinkSession.PaymentLinkId);
}
/// <summary>
/// When transaction is failed, this callback will be called.
/// Show a failure screen and fire any analytic events here.
/// </summary>
private void OnTransactionFailed(PaymentLinkSession paymentLinkSession)
{
Debug.Log("Transaction failed: " + paymentLinkSession.PaymentLinkId);
}
/// <summary>
/// Optional: When web link is open, this callback will be called.
/// Fire any analytic events here.
/// </summary>
private void onWebLinkOpen()
{
// Consider firing analytic event here.
Debug.Log("Checkout open for user: " + boltSDK.GetBoltUser().ToString());
checkoutIsOpen = true;
}
}
}