Skip to content

Commit 20acf73

Browse files
author
Artem
committed
Add the ability to start the payment process + refactor
1 parent 8280f6d commit 20acf73

File tree

2 files changed

+60
-4
lines changed

2 files changed

+60
-4
lines changed

demo/peacock/src/modules/checkout.js

+38-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,6 @@ export type CheckoutState = {
3232
billingAddress: ShippingAddress,
3333
};
3434

35-
export const checkApplePay = () => foxApi.applePay.available();
36-
3735
export const selectCreditCard = createAction('CHECKOUT_SET_CREDIT_CARD');
3836
export const setEditStage = createAction('CHECKOUT_SET_EDIT_STAGE');
3937
export const setBillingData = createAction('CHECKOUT_SET_BILLING_DATA', (key, value) => [key, value]);
@@ -43,6 +41,7 @@ export const setBillingAddress = createAction('CHECKOUT_SET_BILLING_ADDRESS');
4341
export const toggleShippingModal = createAction('TOGGLE_SHIPPING_MODAL');
4442
export const toggleDeliveryModal = createAction('TOGGLE_DELIVERY_MODAL');
4543
export const togglePaymentModal = createAction('TOGGLE_PAYMENT_MODAL');
44+
const applePayAvailable = createAction('APPLE_PAY_AVAILABLE');
4645
const markAddressAsDeleted = createAction('CHECKOUT_MARK_ADDRESS_AS_DELETED');
4746
const markAddressAsRestored = createAction(
4847
'CHECKOUT_MARK_ADDRESS_AS_RESTORED',
@@ -107,6 +106,36 @@ function addressToPayload(address) {
107106
return payload;
108107
}
109108

109+
const _checkApplePay = createAsyncActions(
110+
'checkApplePay',
111+
function() {
112+
const { dispatch } = this;
113+
114+
return foxApi.applePay.available()
115+
.then((resp) => {
116+
dispatch(applePayAvailable(resp));
117+
});
118+
}
119+
);
120+
121+
export const checkApplePay = _checkApplePay.perform;
122+
123+
const _beginApplePay = createAsyncActions(
124+
'beginApplePay',
125+
function(payment) {
126+
return foxApi.applePay.beginApplePay(payment)
127+
.then(() => {
128+
console.log('payment is successfull, from checkout.js');
129+
})
130+
.catch((err) => {
131+
console.log('error occurred in checkout.js, _beginApplePay');
132+
throw new Error(err);
133+
});
134+
}
135+
);
136+
137+
export const beginApplePay = _beginApplePay.perform;
138+
110139
const _removeShippingMethod = createAsyncActions(
111140
'removeShippingMethod',
112141
function() {
@@ -465,6 +494,7 @@ const initialState: CheckoutState = {
465494
shippingModalVisible: false,
466495
deliveryModalVisible: false,
467496
paymentModalVisible: false,
497+
applePayAvailable: false,
468498
};
469499

470500
function sortAddresses(addresses: Array<Address>): Array<Address> {
@@ -505,6 +535,12 @@ const reducer = createReducer({
505535
billingData,
506536
};
507537
},
538+
[applePayAvailable]: (state, available) => {
539+
return {
540+
...state,
541+
applePayAvailable: available,
542+
};
543+
},
508544
[shippingMethodsActions.succeeded]: (state, list) => {
509545
return {
510546
...state,

demo/peacock/src/pages/checkout/checkout.jsx

+22-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import type { AsyncStatus } from 'types/async-actions';
3232

3333
// actions
3434
import * as actions from 'modules/checkout';
35-
import { checkApplePay } from 'modules/checkout';
3635
import { EditStages } from 'modules/checkout';
3736
import { fetch as fetchCart, hideCart } from 'modules/cart';
3837
import { fetchUser } from 'modules/auth';
@@ -74,6 +73,8 @@ class Checkout extends Component {
7473
tracking.checkoutStart(cart.lineItems);
7574
});
7675

76+
this.props.checkApplePay();
77+
7778
if (!this.isEmailSetForCheckout()) {
7879
this.props.fetchUser();
7980
}
@@ -182,12 +183,31 @@ class Checkout extends Component {
182183
return emailIsSet(user);
183184
}
184185

186+
@autobind beginApplePay() {
187+
console.log('starting the apple pay inside the checkout.jsx');
188+
const total = this.props.cart.totals.total;
189+
console.log('the total from the cart -> ', total);
190+
const amount = (parseFloat(total)/100).toFixed(2);
191+
console.log('amount -> ', amount);
192+
const paymentRequest = {
193+
countryCode: 'US',
194+
currencyCode: 'USD',
195+
total: {
196+
label: 'Stripe.com',
197+
amount: amount.toString(),
198+
},
199+
};
200+
console.log('payment request obj -> ', paymentRequest);
201+
this.props.beginApplePay(paymentRequest);
202+
}
203+
185204
get applePayButton() {
186-
if (!checkApplePay()) return null;
205+
if (!this.props.applePayAvailable) return null;
187206

188207
return (
189208
<Button
190209
styleName="apple-pay"
210+
onClick={this.beginApplePay}
191211
/>
192212
);
193213
}

0 commit comments

Comments
 (0)