This repository has been archived by the owner on Aug 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCheckoutViewModel.cs
194 lines (146 loc) · 6.39 KB
/
CheckoutViewModel.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
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Stripe;
using XamarinStripe.Forms.Services;
using Xamarin.Forms;
namespace XamarinStripe.Forms.ViewModels {
internal class CheckoutViewModel : ViewModelBase {
private readonly PaymentOptionsViewModel _paymentOptionsViewModel;
private readonly ShippingAddressViewModel _shippingAddressViewModel;
private readonly ShippingMethodsViewModel _shippingMethodsViewModel;
private bool _busy;
private bool _paymentMethodLoading;
public CheckoutViewModel(ReadOnlyCollection<ProductViewModel> products) {
_shippingMethodsViewModel = new ShippingMethodsViewModel(ShippingAddressUpdated);
_shippingAddressViewModel = new ShippingAddressViewModel(_shippingMethodsViewModel);
_paymentOptionsViewModel = new PaymentOptionsViewModel(PaymentMethodUpdated);
SelectShippingAddressCommand = new Command(SelectShippingAddress);
SelectPaymentOptionCommand = new Command(SelectPaymentOption);
Products = products;
Device.BeginInvokeOnMainThread(async () => await Load());
BuyCommand = new Command(async () => await Buy(), CanBuy);
}
public ReadOnlyCollection<ProductViewModel> Products { get; }
public bool PaymentMethodLoading {
get => _paymentMethodLoading;
set => SetProperty(ref _paymentMethodLoading, value, () => OnPropertyChanged(nameof(PaymentMethodLoaded)));
}
public bool PaymentMethodLoaded => !PaymentMethodLoading;
public Command SelectShippingAddressCommand { get; }
public Command SelectPaymentOptionCommand { get; }
public float Total => Products.Sum(p => p.Price);
private bool HaveShipTo => _shippingMethodsViewModel.Methods?.First(m => m.Selected) != null &&
!string.IsNullOrWhiteSpace(_shippingAddressViewModel.Name);
public string ShipTo {
get
{
var method = _shippingMethodsViewModel.Methods?.First(m => m.Selected)?.Label;
if (method != null && !string.IsNullOrWhiteSpace(_shippingAddressViewModel.Name))
return _shippingAddressViewModel.Name + ", " + method;
return "Select address";
}
}
private bool HavePayFrom => _paymentOptionsViewModel?.PaymentMethods.SingleOrDefault(m => m.Selected) != null;
public string PayFrom => _paymentOptionsViewModel?.PaymentMethods.SingleOrDefault(m => m.Selected)?.Name ??
"Select Payment Method";
public Command BuyCommand { get; }
public bool Busy {
get => _busy;
set => SetProperty(ref _busy, value, () => BuyCommand.ChangeCanExecute());
}
private bool CanBuy() {
return !Busy && HavePayFrom && HaveShipTo;
}
public class UseStripeSdk {
public class DirectoryServerEncryptionKind {
[JsonProperty("directory_server_id")] public string DirectoryServerId { get; set; }
[JsonProperty("algorithm")] public string Algorithm { get; set; }
[JsonProperty("certificate")] public string Certificate { get; set; }
[JsonProperty("root_certificate_authorities")]
public IList<string> RootCertificateAuthorities { get; set; }
}
[JsonProperty("type")] public string Type { get; set; }
[JsonProperty("three_d_secure_2_source")]
public string ThreeDSecure2Source { get; set; }
[JsonProperty("directory_server_name")]
public string DirectoryServerName { get; set; }
[JsonProperty("server_transaction_id")]
public string ServerTransactionId { get; set; }
[JsonProperty("three_ds_method_url")] public string ThreeDsMethodUrl { get; set; }
[JsonProperty("three_ds_optimizations")]
public string ThreeDsOptimizations { get; set; }
[JsonProperty("directory_server_encryption")]
public DirectoryServerEncryptionKind DirectoryServerEncryption { get; set; }
}
public class Example
{
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("use_stripe_sdk")]
public UseStripeSdk UseStripeSdk { get; set; }
}
private async Task Buy() {
try {
Busy = true;
var products = Products.Select(p => p.OriginalProduct).ToList();
var address = _shippingAddressViewModel.ShippingAddress;
var method = _paymentOptionsViewModel.PaymentMethods.SingleOrDefault(pm => pm.Selected)?.PaymentMethod;
if (method == null) throw new Exception("Payment method unexpectedly null");
var (secret, intent) = await EphemeralService.Instance.CreatePaymentIntent(products, address, method);
var paymentIntentService = new PaymentIntentService(await EphemeralService.Instance.GetClient(true));
var paymentConfirmOptions = new PaymentIntentConfirmOptions {
ClientSecret = secret,
Expand = new List<string> {"payment_method"},
PaymentMethod = method.Id,
UseStripeSdk = true,
ReturnUrl = "payments-example://stripe-redirect"
};
var result = await paymentIntentService.ConfirmAsync(intent, paymentConfirmOptions);
switch (result.NextAction?.Type) {
case null:
Busy = false;
await Navigator.ShowMessage("Success", "Your purchase was successful!");
break; // All good
case "stripe_3ds2_fingerprint":
default:
throw new NotImplementedException($"Not implemented: Can't handle {result.NextAction.Type}");
}
}
catch (Exception ex) {
Busy = false;
await Navigator.ShowMessage("Error", ex.Message);
}
}
private async Task Load() {
try {
PaymentMethodLoading = true;
await _paymentOptionsViewModel.Refresh();
}
catch (Exception ex) {
await Navigator.ShowMessage("Error", ex.Message);
}
finally {
PaymentMethodLoading = false;
}
OnPropertyChanged(nameof(PayFrom));
}
private void SelectPaymentOption() {
Navigator.PaymentOptions(_paymentOptionsViewModel);
}
private void SelectShippingAddress() {
Navigator.ShippingAddress(_shippingAddressViewModel);
}
public void ShippingAddressUpdated() {
OnPropertyChanged(nameof(ShipTo));
BuyCommand.ChangeCanExecute();
}
private void PaymentMethodUpdated() {
OnPropertyChanged(nameof(PayFrom));
BuyCommand.ChangeCanExecute();
}
}
}