-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlayFabPurchaseScript.cs
266 lines (182 loc) · 7.52 KB
/
PlayFabPurchaseScript.cs
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PlayFab;
using PlayFab.ClientModels;
using UnityEngine.UI;
public class PlayFabPurchaseScript : MonoBehaviour {
[SerializeField]
private playFabLogin playFabloginScript;
[SerializeField]
private UXPlayFab uxPlayFab;
[SerializeField]
private Text debugPurchaseText;
private string OrderID;
private string ProviderName;
private string Currency;
private string ProviderTransactionID;
private string CatalogueVersion;
private int iCountRetry = 0;
private int iMaxCount = 5;
private int iManualCheckCounter = 0;
private int iManualMaxCheckCounter = 3;
//https://api.playfab.com/docs/tutorials/landing-commerce/non-receipt-payment-processing
void Start(){
CatalogueVersion = GetComponent<playFabSKU> ().sPlayFabCataloguieVersionNumber;
}
//###########################################################################################################
// Start Purchase
//###########################################################################################################
public void onStartPurchase(string itemID, uint quantity, string annotation )
{
ItemPurchaseRequest itemToPurchase = new ItemPurchaseRequest();
itemToPurchase.ItemId = itemID;
itemToPurchase.Quantity = quantity;
itemToPurchase.Annotation = annotation;
StartPurchaseRequest startRequest = new StartPurchaseRequest();
startRequest.CatalogVersion = CatalogueVersion;
startRequest.Items = new List<ItemPurchaseRequest> ();
startRequest.Items.Add(itemToPurchase);
Debug.Log ("onStartPurchase");
PlayFabClientAPI.StartPurchase(startRequest, onStartPurchaseResult, onStartPurchaseError);
}
private void onStartPurchaseResult(StartPurchaseResult result)
{
OrderID = result.OrderId;
ProviderName = result.PaymentOptions [0].ProviderName;
Currency = result.PaymentOptions [0].Currency;
Debug.Log ("Start Purchase Result");
onPayForPurchase ();
}
private void onStartPurchaseError(PlayFabError error)
{
Debug.LogWarning("Something went wrong with your purchase call. :(");
Debug.LogError("Here's some debug information:");
Debug.LogError(error.GenerateErrorReport());
}
//###########################################################################################################
// PayForPurchase Purchase
//###########################################################################################################
public void onPayForPurchase()
{
PayForPurchaseRequest payRequest = new PayForPurchaseRequest();
payRequest.Currency = Currency;
payRequest.ProviderName = ProviderName;
payRequest.OrderId = OrderID;
Debug.Log ("onPayForPurchase");
PlayFabClientAPI.PayForPurchase(payRequest, onPurchaseResult, onPurchaseError);
}
private void onPurchaseResult(PayForPurchaseResult result)
{
debugPurchaseText.text = "onPayForPurchaseResult" + "\n\n" + result.PurchaseConfirmationPageURL;
Application.OpenURL(result.PurchaseConfirmationPageURL);
}
private void onPurchaseError(PlayFabError error)
{
Debug.LogWarning("Something went wrong with your purchase call. :(");
Debug.LogError("Here's some debug information:");
Debug.LogError(error.GenerateErrorReport());
}
//###########################################################################################################
// Confirm Purchase
//###########################################################################################################
public void onConfirmPurchase()
{
ConfirmPurchaseRequest confirmRequest = new ConfirmPurchaseRequest();
confirmRequest.OrderId = OrderID;
//debugPurchaseText.text = "onPayForPurchase";
PlayFabClientAPI.ConfirmPurchase(confirmRequest, onConfirmResult, onConfirmError);
uxPlayFab.UpdatePaymentText ("Validating purchase from PayPal");
}
private void onConfirmResult(ConfirmPurchaseResult result)
{
debugPurchaseText.text = "onConfirmPurchaseResult" + "\n\n" + result.Items[0].ItemId + " purchased at " + result.Items[0].PurchaseDate + " for " + result.Items[0].UnitPrice;
uxPlayFab.UpdatePaymentText ("Thanks for your purchase");
uxPlayFab.ShowThanksButton ();
}
private void onConfirmError(PlayFabError error)
{
//Debug.LogWarning("Something went wrong with your purchase call. :(");
Debug.Log("On Confirmation: Here's some debug information:");
Debug.Log(error.GenerateErrorReport());
if (iCountRetry <= iMaxCount) {
StartCoroutine (CheckPurchaseCoroutine (5f));
} else {
uxPlayFab.UpdatePaymentText ("When you have finished making payment through PayPal\n\nclick the button below");
uxPlayFab.ShowTryAgainButton ();
}
//error.ErrorMessage;
}
//###############################################################################################################
// Poll Server
//###############################################################################################################
public void PollServerForConfirmation(string itemID, uint quantity, string annotation ){
StartCoroutine(PurchaseCoroutine(itemID, quantity, annotation));
}
IEnumerator PurchaseCoroutine (string itemID, uint quantity, string annotation )
{
print("Purchasing " + itemID + ", " + quantity + ", " + annotation);
yield return new WaitForSeconds(3f);
onStartPurchase (itemID, quantity, annotation);
uxPlayFab.UpdatePaymentText ("Please make payment through PayPal as requested");
yield return new WaitForSeconds(20f);
onConfirmPurchase ();
}
IEnumerator CheckPurchaseCoroutine (float delay)
{
iCountRetry = iCountRetry + 1;
switch (iCountRetry) {
case 1:
uxPlayFab.UpdatePaymentText ("Validating purchase from PayPal" + ".");
break;
case 2:
uxPlayFab.UpdatePaymentText ("Validating purchase from PayPal" + "..");
break;
case 3:
uxPlayFab.UpdatePaymentText ("Validating purchase from PayPal" + "...");
break;
case 4:
uxPlayFab.UpdatePaymentText ("Validating purchase from PayPal" + ".");
break;
case 5:
uxPlayFab.UpdatePaymentText ("Validating purchase from PayPal" + "..");
break;
case 6:
uxPlayFab.UpdatePaymentText ("Validating purchase from PayPal" + "...");
break;
case 7:
uxPlayFab.UpdatePaymentText ("Validating purchase from PayPal" + ".");
break;
case 8:
uxPlayFab.UpdatePaymentText ("Validating purchase from PayPal" + "..");
break;
case 9:
uxPlayFab.UpdatePaymentText ("Validating purchase from PayPal" + "...");
break;
case 10:
uxPlayFab.UpdatePaymentText ("Validating purchase from PayPal" + ".");
break;
case 11:
uxPlayFab.UpdatePaymentText ("Validating purchase from PayPal" + "..");
break;
default:
uxPlayFab.UpdatePaymentText ("Validating purchase from PayPal" + "...");
break;
}
yield return new WaitForSeconds(delay);
onConfirmPurchase ();
}
public void ManuallyCheckPurchase(){
iManualCheckCounter = iManualCheckCounter + 1;
if (iManualCheckCounter >= iManualMaxCheckCounter) {
uxPlayFab.UpdatePaymentText ("\n\n\nThere was a problem validating your payment via PayPal.\nYou need to complete your payment through the PayPal browser that was loaded at the start of the payment flow.\nIf you have paid for the purchase successfully and need assistance. Please contatct [email protected].\n\nWe will aim to respond in 24 hours.");
//show cancel button
uxPlayFab.HideTryAgainButton ();
uxPlayFab.ShowCancelButton();
} else {
uxPlayFab.UpdatePaymentText ("Attempting to validate purchase\n\n" + iManualCheckCounter + " of " + iManualMaxCheckCounter + " attempts");
onConfirmPurchase ();
uxPlayFab.HideTryAgainButton ();
}
}
}