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 pathAddCardViewModel.cs
130 lines (106 loc) · 4.1 KB
/
AddCardViewModel.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
using System;
using System.Threading.Tasks;
using Stripe;
using XamarinStripe.Forms.Services;
using Xamarin.Forms;
namespace XamarinStripe.Forms.ViewModels {
internal class AddCardViewModel : ViewModelBase {
private readonly Action<PaymentMethod> _callback;
private ImageSource _image = ImageService.Instance.CardUnknown;
private int _length = 16;
private string _month = "";
private string _number = "";
private bool _useCardFront;
private string _verificationCode = "";
private string _year = "";
private string _zip = "";
public AddCardViewModel(Action<PaymentMethod> callback) {
DoneCommand = new Command(async () => await Done(), IsValid);
_callback = callback;
}
public ImageSource CardFront => ImageService.Instance.CardFront;
public ImageSource CardBack => ImageService.Instance.CardBack;
public bool UseCardFront {
get => _useCardFront;
set => SetProperty(ref _useCardFront, value);
}
public ImageSource Image {
get => _image;
set => SetProperty(ref _image, value);
}
public int Length {
get => _length;
set => SetProperty(ref _length, value);
}
public string Number {
get => _number;
set => SetProperty(ref _number, value, UpdateCardDetails);
}
public string Month {
get => _month;
set => SetProperty(ref _month, value, DoneCommand.ChangeCanExecute);
}
public string Year {
get => _year;
set => SetProperty(ref _year, value, DoneCommand.ChangeCanExecute);
}
public string VerificationCode {
get => _verificationCode;
set => SetProperty(ref _verificationCode, value, DoneCommand.ChangeCanExecute);
}
public string Zip {
get => _zip;
set => SetProperty(ref _zip, value, DoneCommand.ChangeCanExecute);
}
public Command DoneCommand { get; }
public ImageSource DoneImage => ImageService.Instance.Done;
private bool IsValid() {
if (Number.Length != Length) return false;
if (Month.Length != 2 || !int.TryParse(Month, out var month)) return false;
if (Year.Length != 2 || !int.TryParse(Year, out var year)) return false;
if (string.IsNullOrWhiteSpace(VerificationCode) || !int.TryParse(VerificationCode, out _)) return false;
if (string.IsNullOrWhiteSpace(Zip)) return false;
if (month < 1 || month > 12) return false;
year += DateTime.Now.Year / 100 * 100; // Two digit to four digit
if (year < DateTime.Now.Year || year > DateTime.Now.AddYears(16).Year) return false;
// TODO: Probably a little more validation here!
return true;
}
private async Task Done() {
try {
// Create the payment method
var paymentMethodService = new PaymentMethodService(await EphemeralService.Instance.GetClient(true));
var paymentMethodCreateOptions = new PaymentMethodCreateOptions {
Card = new PaymentMethodCardOptions {
Number = Number,
Cvc = VerificationCode,
ExpMonth = long.Parse(Month),
ExpYear = long.Parse(Year)
},
Type = "card",
BillingDetails = new PaymentMethodBillingDetailsOptions {
Address = new AddressOptions {
PostalCode = Zip
}
}
};
var paymentMethod = await paymentMethodService.CreateAsync(paymentMethodCreateOptions);
// Attach the payment method to the customer
paymentMethodService = new PaymentMethodService(await EphemeralService.Instance.GetClient());
var paymentMethodAttachOptions = new PaymentMethodAttachOptions {
Customer = await EphemeralService.Instance.GetCustomerId()
};
paymentMethod = await paymentMethodService.AttachAsync(paymentMethod.Id, paymentMethodAttachOptions);
_callback(paymentMethod);
await Navigator.CardAdded();
}
catch (Exception ex) {
await Navigator.ShowMessage("Error", ex.Message);
}
}
private void UpdateCardDetails() {
(Length, Image) = CardDefinitionService.Instance.DetailsFor(Number);
DoneCommand.ChangeCanExecute();
}
}
}