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 pathShippingAddressViewModel.cs
215 lines (173 loc) · 5.69 KB
/
ShippingAddressViewModel.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
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading.Tasks;
using Stripe;
using XamarinStripe.Forms.Models;
using XamarinStripe.Forms.Services;
using Xamarin.Forms;
namespace XamarinStripe.Forms.ViewModels {
internal class ShippingAddressViewModel : ViewModelBase {
private const string UnitedStates = "US";
private readonly ShippingMethodsViewModel _shippingMethodsViewModel;
private bool _busy;
private Country _selectedCountry;
public ShippingAddressViewModel(ShippingMethodsViewModel shippingMethodsViewModel) {
NextCommand = new Command(async () => await Next(), () => !Busy && IsEverythingValid());
_shippingMethodsViewModel = shippingMethodsViewModel;
Countries = GetCountries();
}
internal ShippingAddress ShippingAddress { get; } = new ShippingAddress();
public bool Busy {
get => _busy;
set => SetProperty(ref _busy, value, NextCommand.ChangeCanExecute);
}
public List<Country> Countries { get; }
public string Name {
get => ShippingAddress.Name;
set
{
if (value == ShippingAddress.Name) return;
ShippingAddress.Name = value;
OnPropertyChanged();
CheckValues();
}
}
public string Address {
get => ShippingAddress.Address;
set
{
if (value == ShippingAddress.Address) return;
ShippingAddress.Address = value;
OnPropertyChanged();
CheckValues();
}
}
public string Apartment {
get => ShippingAddress.Apartment;
set
{
if (value == ShippingAddress.Apartment) return;
ShippingAddress.Apartment = value;
OnPropertyChanged();
CheckValues();
}
}
public Country SelectedCountry {
get => _selectedCountry;
set => SetProperty(ref _selectedCountry, value, () => {
CheckValues();
ShippingAddress.Country = value.Name;
});
}
public string ZipCode {
get => ShippingAddress.ZipCode;
set
{
if (value == ShippingAddress.ZipCode) return;
ShippingAddress.ZipCode = value;
OnPropertyChanged();
CheckValues();
}
}
public string City {
get => ShippingAddress.City;
set
{
if (value == ShippingAddress.City) return;
ShippingAddress.City = value;
OnPropertyChanged();
CheckValues();
}
}
public string State {
get => ShippingAddress.State;
set
{
if (value == ShippingAddress.State) return;
ShippingAddress.State = value;
OnPropertyChanged();
CheckValues();
}
}
public string Phone {
get => ShippingAddress.Phone;
set
{
if (value == ShippingAddress.Phone) return;
ShippingAddress.Phone = value;
OnPropertyChanged();
CheckValues();
}
}
public Command NextCommand { get; }
public ImageSource NextImage => ImageService.Instance.Next;
public ImageSource ShippingImage => ImageService.Instance.Shipping;
private List<Country> GetCountries() {
// Derived from https://csharp.net-tutorials.com/working-with-culture-and-regions/the-regioninfo-class/
var cultures = CultureInfo.GetCultures(CultureTypes.SpecificCultures);
var countries = from ci in cultures select new RegionInfo(ci.Name);
var result = countries.Select(c => new Country(c.TwoLetterISORegionName, c.EnglishName)).OrderBy(n => n.Name)
.Distinct().ToList();
var us = result.Single(i => i.Code == UnitedStates);
result.Remove(us);
result.Insert(0, us);
SelectedCountry = us;
return result;
}
private bool IsEverythingValid() {
// TODO: Add checks
if (string.IsNullOrWhiteSpace(Name)) return false;
if (string.IsNullOrWhiteSpace(Address)) return false;
if (string.IsNullOrWhiteSpace(ZipCode)) return false;
if (string.IsNullOrWhiteSpace(City)) return false;
if (string.IsNullOrWhiteSpace(State)) return false;
if (string.IsNullOrWhiteSpace(Phone)) return false;
return true;
}
private async Task Next() {
if (!IsEverythingValid()) return;
try {
Busy = true;
var stripeClient = await EphemeralService.Instance.GetClient();
var customerClient = new CustomerService(stripeClient);
var customerUpdateOptions = new CustomerUpdateOptions {
Name = Name,
Address = new AddressOptions {
Line1 = ShippingAddress.Address,
PostalCode = ShippingAddress.ZipCode,
City = ShippingAddress.City,
State = ShippingAddress.State,
Country = ShippingAddress.Country
}
};
var customer =
await customerClient.UpdateAsync(await EphemeralService.Instance.GetCustomerId(), customerUpdateOptions);
// TODO: something with the customer?
var (list, preferred) = await ShippingMethodService.Instance.MethodsFor(SelectedCountry.Code);
_shippingMethodsViewModel.Update(list, preferred);
await Navigator.ShippingMethod(_shippingMethodsViewModel);
}
catch (ShippingMethodService.ShippingException shippingException) {
await Navigator.ShowMessage(shippingException.Message, shippingException.Reason);
}
finally {
Busy = false;
}
}
private void CheckValues() {
NextCommand.ChangeCanExecute();
}
public struct Country {
public Country(string code, string name) {
Code = code;
Name = name;
}
public string Code { get; }
public string Name { get; }
public override string ToString() {
return Name;
}
}
}
}