@salla.sa/applepay 3.0.0-alpha.0 → 3.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/utils.js CHANGED
@@ -26,10 +26,55 @@ import http from "./http";
26
26
  * @param {ApplePayPaymentContact} shippingContact
27
27
  *
28
28
  */
29
- export function mutateShipmentAddress(SallaApplePay, shippingContact, isAuthorized = false) {
30
- console.log('mutateShipmentAddress called', shippingContact, isAuthorized);
29
+ export async function mutateShippingContact(SallaApplePay, shippingContact, isAuthorized = false) {
30
+ salla.logger.log('🍏 Pay: mutateShippingContact called', shippingContact, isAuthorized);
31
31
 
32
- if (!SallaApplePay.detail.requiredShippingContactFields) {
32
+ if (!SallaApplePay.detail.requiredShippingContactFields || SallaApplePay.detail.requiredShippingContactFields.length == 0) {
33
+ return;
34
+ }
35
+
36
+ if (isAuthorized) {
37
+ if (isGuestCheckout()) {
38
+
39
+ if (
40
+ !shippingContact.emailAddress ||
41
+ !shippingContact.givenName ||
42
+ !shippingContact.familyName ||
43
+ !shippingContact.phoneNumber
44
+ ) {
45
+
46
+ salla.logger.warn('🍏 Pay: Guest contact fields are required', shippingContact);
47
+
48
+ const errors = [];
49
+ if (!shippingContact.emailAddress) {
50
+ errors.push(new window.ApplePayError('shippingContactInvalid', 'emailAddress', 'Email address is required'));
51
+ }
52
+
53
+ if (!shippingContact.phoneNumber) {
54
+ errors.push(new window.ApplePayError('shippingContactInvalid', 'phoneNumber', 'Phone number is required'));
55
+ }
56
+
57
+ if (!shippingContact.givenName || !shippingContact.familyName) {
58
+ errors.push(new window.ApplePayError('shippingContactInvalid', 'name', 'Name is required'));
59
+ }
60
+
61
+ SallaApplePay.session.completePayment({
62
+ status: SallaApplePay.session.STATUS_INVALID_SHIPPING_CONTACT,
63
+ errors: errors
64
+ });
65
+
66
+ return;
67
+ }
68
+
69
+ await updateGuestContact(SallaApplePay, shippingContact);
70
+
71
+ }
72
+
73
+ // if authorized and not guest checkout, do nothing address already added
74
+ return;
75
+ }
76
+
77
+ if (!SallaApplePay.detail.requiredShippingContactFields.includes('postalAddress')) {
33
78
  return;
34
79
  }
35
80
 
@@ -38,15 +83,13 @@ export function mutateShipmentAddress(SallaApplePay, shippingContact, isAuthoriz
38
83
  {
39
84
  'country': shippingContact.country,
40
85
  'city': shippingContact.locality,
41
- 'local': shippingContact.subLocality,
86
+ 'local': shippingContact.subLocality || shippingContact.administrativeArea || shippingContact.locality,
42
87
  'description': shippingContact.subAdministrativeArea,
43
88
  'street': shippingContact.addressLines?.join(", ") || shippingContact.administrativeArea,
44
89
  'country_code': shippingContact.countryCode,
45
90
  'postal_code': shippingContact.postalCode,
46
- 'is_authorized': isAuthorized
47
91
  },
48
92
  async ({ data }) => {
49
- if (isAuthorized) { return }
50
93
  if (typeof SallaApplePay.detail.shippingContactSelected.onSuccess === 'function') {
51
94
  SallaApplePay.detail.shippingContactSelected.onSuccess(data);
52
95
  }
@@ -57,25 +100,29 @@ export function mutateShipmentAddress(SallaApplePay, shippingContact, isAuthoriz
57
100
  if (!SallaApplePay.shipping_methods || (SallaApplePay.shipping_methods && !SallaApplePay.shipping_methods.length)) {
58
101
  salla.logger.warn('🍏 Pay: We dont found any supported methods', data);
59
102
 
60
- return SallaApplePay.session.completeShippingContactSelection({
103
+ SallaApplePay.session.completeShippingContactSelection({
61
104
  status: SallaApplePay.session.STATUS_INVALID_SHIPPING_POSTAL_ADDRESS,
62
105
  errors: [
63
106
  new window.ApplePayError('addressUnserviceable')
64
107
  ]
65
108
  });
109
+
110
+ return
66
111
  }
67
112
 
68
113
  try {
69
- await SallaApplePay.selectApplePayShippingMethod(SallaApplePay.shipping_methods[0]['ship_id'], SallaApplePay.shipping_methods[0]['private_ship_id']);
114
+ await SallaApplePay.selectApplePayShippingMethod(SallaApplePay.shipping_methods[0]);
70
115
  } catch (error) {
71
116
  salla.logger.warn('Failed set the shipping details to api', error);
72
117
 
73
- return SallaApplePay.session.completeShippingContactSelection({
118
+ SallaApplePay.session.completeShippingContactSelection({
74
119
  status: SallaApplePay.session.STATUS_INVALID_SHIPPING_POSTAL_ADDRESS,
75
120
  errors: [
76
121
  new window.ApplePayError('addressUnserviceable')
77
122
  ]
78
123
  });
124
+
125
+ return;
79
126
  }
80
127
 
81
128
  try {
@@ -83,20 +130,25 @@ export function mutateShipmentAddress(SallaApplePay, shippingContact, isAuthoriz
83
130
  } catch (error) {
84
131
  salla.logger.warn('🍏 Pay: Failed recalculate total', error);
85
132
 
86
- return SallaApplePay.session.completeShippingContactSelection({
133
+ SallaApplePay.session.completeShippingContactSelection({
87
134
  status: SallaApplePay.session.STATUS_INVALID_SHIPPING_POSTAL_ADDRESS,
88
135
  errors: [
89
136
  new window.ApplePayError('addressUnserviceable')
90
137
  ]
91
138
  });
139
+
140
+ return;
92
141
  }
93
142
 
94
- SallaApplePay.session.completeShippingContactSelection({
143
+ const updatedShippingContactSelection = {
95
144
  newTotal: SallaApplePay.prepareTotal(),
96
145
  newLineItems: SallaApplePay.prepareLineItems(),
97
146
  newShippingMethods: SallaApplePay.mappingShippingMethods(SallaApplePay.shipping_methods)
98
- });
147
+ };
99
148
 
149
+ salla.logger.log('🍏 Pay: completeShippingContactSelection', updatedShippingContactSelection);
150
+
151
+ SallaApplePay.session.completeShippingContactSelection(updatedShippingContactSelection);
100
152
  },
101
153
  ({ response }) => {
102
154
  salla.logger.warn('🍏 Pay: Failed add address via api', response);
@@ -108,19 +160,11 @@ export function mutateShipmentAddress(SallaApplePay, shippingContact, isAuthoriz
108
160
  // parse 422 errors
109
161
  let fields = response?.data?.error?.fields;
110
162
 
111
- let errors = [];
112
-
113
- if (fields?.country_code) {
114
- errors.push(new window.ApplePayError('shippingContactInvalid', 'countryCode', fields?.country_code[0]))
115
- }
116
-
117
- if (fields?.city) {
118
- errors.push(new window.ApplePayError('shippingContactInvalid', 'locality', fields?.city[0]))
119
- }
120
-
121
- if (fields?.country) {
122
- errors.push(new window.ApplePayError('shippingContactInvalid', 'country', fields?.country[0]))
123
- }
163
+ let errors = getApplePayErrors({
164
+ countryCode: fields?.country_code && fields.country_code.length > 0 ? fields.country_code[0] : null,
165
+ locality: fields?.city && fields.city.length > 0 ? fields.city[0] : null,
166
+ country: fields?.country && fields.country.length > 0 ? fields.country[0] : null,
167
+ });
124
168
 
125
169
  if (errors.length === 0 && response?.data?.error?.message) {
126
170
  errors.push(new window.ApplePayError('shippingContactInvalid', 'locality', response?.data?.error?.message))
@@ -135,3 +179,53 @@ export function mutateShipmentAddress(SallaApplePay, shippingContact, isAuthoriz
135
179
  }
136
180
  );
137
181
  }
182
+
183
+ function isGuestCheckout() {
184
+ return salla.config.isGuest() && salla.config.get('store.features').includes('guest-checkout');
185
+ }
186
+
187
+ /**
188
+ * Update guest contact
189
+ *
190
+ * @param {SallaApplePay} SallaApplePay
191
+ * @param {ApplePayPaymentContact} shippingContact
192
+ *
193
+ */
194
+ async function updateGuestContact(SallaApplePay, shippingContact) {
195
+ salla.logger.log('🍏 Pay: Updating guest contact', shippingContact);
196
+
197
+ return new Promise((resolve, reject) => {
198
+ http.post(
199
+ SallaApplePay.detail.guestContactSelected.url.replace('{id}', SallaApplePay.id),
200
+ {
201
+ 'email': shippingContact.emailAddress || null,
202
+ 'first_name': shippingContact.givenName || null,
203
+ 'last_name': shippingContact.familyName || null,
204
+ 'phone_number': shippingContact.phoneNumber || null,
205
+ 'country_code': shippingContact.countryCode || null,
206
+ },
207
+ async ({ data }) => {
208
+ if (typeof SallaApplePay.detail.guestContactSelected?.onSuccess === 'function') {
209
+ SallaApplePay.detail.guestContactSelected.onSuccess(data);
210
+ }
211
+ resolve(data);
212
+ },
213
+ ({ response }) => {
214
+ salla.logger.warn('🍏 Pay: Failed to update guest contact via api', response);
215
+
216
+ if (typeof SallaApplePay.detail.guestContactSelected?.onFailed === 'function') {
217
+ SallaApplePay.detail.guestContactSelected.onFailed(response);
218
+ }
219
+
220
+ // Reject the promise so it can be caught in onPaymentAuthorized
221
+ reject({ response });
222
+ }
223
+ );
224
+ });
225
+ }
226
+
227
+ function getApplePayErrors(fields) {
228
+ return Object.entries(fields)
229
+ .filter(([field, messages]) => messages && messages.length > 0)
230
+ .map(([field, messages]) => new window.ApplePayError('shippingContactInvalid', field, messages[0]));
231
+ }