Skip to content

Commit

Permalink
Create initiateOrder thunk against new request-payment transitions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Gnito authored and OtterleyW committed Jun 11, 2019
1 parent b073779 commit e0bb4f8
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 75 deletions.
93 changes: 35 additions & 58 deletions src/containers/CheckoutPage/CheckoutPage.duck.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,74 +110,51 @@ export const speculateTransactionError = e => ({

/* ================ Thunks ================ */

export const initiateOrder = (orderParams, initialMessage) => (dispatch, getState, sdk) => {
// // TODO initial message should be sent at the confirm-payment transition.
// const sendInitialMessage = (initialMessage, orderId, sdk) => {
// if (initialMessage) {
// return sdk.messages
// .send({ transactionId: orderId, content: initialMessage })
// .then(() => {
// return { orderId, initialMessageSuccess: true };
// })
// .catch(e => {
// log.error(e, 'initial-message-send-failed', { txId: orderId });
// return { orderId, initialMessageSuccess: false };
// });
// } else {
// return Promise.resolve({ orderId, initialMessageSuccess: true });
// }
// };

export const initiateOrder = (orderParams, transactionId) => (dispatch, getState, sdk) => {
dispatch(initiateOrderRequest());
const bodyParams = {
transition: TRANSITION_REQUEST_PAYMENT,
processAlias: config.bookingProcessAlias,
params: orderParams,
};
return sdk.transactions
.initiate(bodyParams)
.then(response => {
const orderId = response.data.data.id;
dispatch(initiateOrderSuccess(orderId));
dispatch(fetchCurrentUserHasOrdersSuccess(true));

if (initialMessage) {
return sdk.messages
.send({ transactionId: orderId, content: initialMessage })
.then(() => {
return { orderId, initialMessageSuccess: true };
})
.catch(e => {
log.error(e, 'initial-message-send-failed', { txId: orderId });
return { orderId, initialMessageSuccess: false };
});
} else {
return Promise.resolve({ orderId, initialMessageSuccess: true });
const bodyParams = transactionId
? {
id: transactionId,
transition: TRANSITION_REQUEST_PAYMENT_AFTER_ENQUIRY,
params: orderParams,
}
})
.catch(e => {
dispatch(initiateOrderError(storableError(e)));
log.error(e, 'initiate-order-failed', {
listingId: orderParams.listingId.uuid,
bookingStart: orderParams.bookingStart,
bookingEnd: orderParams.bookingEnd,
});
throw e;
});
};

/**
* Initiate an order after an enquiry. Transitions previously created transaction.
*/
export const initiateOrderAfterEnquiry = (transactionId, orderParams) => (
dispatch,
getState,
sdk
) => {
dispatch(initiateOrderRequest());
: {
processAlias: config.bookingProcessAlias,
transition: TRANSITION_REQUEST_PAYMENT,
params: orderParams,
};

const bodyParams = {
id: transactionId,
transition: TRANSITION_REQUEST_PAYMENT_AFTER_ENQUIRY,
params: orderParams,
};
const createOrder = transactionId ? sdk.transactions.transition : sdk.transactions.initiate;

return sdk.transactions
.transition(bodyParams)
return createOrder(bodyParams)
.then(response => {
const orderId = response.data.data.id;
dispatch(initiateOrderSuccess(orderId));
const order = response.data.data;
dispatch(initiateOrderSuccess(order.id));
dispatch(fetchCurrentUserHasOrdersSuccess(true));
// set initialMessageSuccess to true to unify promise handling with initiateOrder
return Promise.resolve({ orderId, initialMessageSuccess: true });
return order;
})
.catch(e => {
dispatch(initiateOrderError(storableError(e)));
const transactionIdMaybe = transactionId ? { transactionId: transactionId.uuid } : {};
log.error(e, 'initiate-order-failed', {
transactionId: transactionId.uuid,
...transactionIdMaybe,
listingId: orderParams.listingId.uuid,
bookingStart: orderParams.bookingStart,
bookingEnd: orderParams.bookingEnd,
Expand Down
34 changes: 17 additions & 17 deletions src/containers/CheckoutPage/CheckoutPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,7 @@ import { StripePaymentForm } from '../../forms';
import { isScrollingDisabled } from '../../ducks/UI.duck';
import { handleCardPayment } from '../../ducks/stripe.duck.js';

import {
initiateOrder,
initiateOrderAfterEnquiry,
setInitialValues,
speculateTransaction,
} from './CheckoutPage.duck';

import { initiateOrder, setInitialValues, speculateTransaction } from './CheckoutPage.duck';
import { storeData, storedData, clearData } from './CheckoutPageSessionHelpers';
import css from './CheckoutPage.css';

Expand Down Expand Up @@ -157,19 +151,26 @@ export class CheckoutPageComponent extends Component {

handlePaymentIntent(handlePaymentParams) {
const {
// onRequestPayment,
onInitiateOrder,
onHandleCardPayment,
// onConfirmPayment,
} = this.props;

// Step 1: initiate order by requesting payment from Marketplace API
const fnRequestPayment = fnParams => {
// fnParams should be { listingId, bookingStart, bookingEnd }
const { stripePaymentIntentClientSecret, stripePaymentIntentId } = handlePaymentParams;
const {
pageData,
stripePaymentIntentClientSecret,
stripePaymentIntentId,
} = handlePaymentParams;
const transactionId = pageData.enquiredTransaction ? pageData.enquiredTransaction.id : null;
return stripePaymentIntentClientSecret
? Promise.resolve({ stripePaymentIntentClientSecret, stripePaymentIntentId })
: Promise.resolve(fnParams); // : onRequestPayment(parameters);
: onInitiateOrder(fnParams, transactionId);
};

// Step 2: pay using Stripe SDK
const fnHandleCardPayment = fnParams => {
// fnParams should be { stripePaymentIntentClientSecret, stripePaymentIntentId }
const { stripe, card, billingDetails, paymentIntent } = handlePaymentParams;
Expand All @@ -189,11 +190,12 @@ export class CheckoutPageComponent extends Component {
: onHandleCardPayment(params);
};

// Step 3: complete order by confirming payment to Marketplace API
const fnConfirmPayment = fnParams => {
// fnParams should include { paymentIntent }
const { message } = handlePaymentParams;
const params = { ...fnParams, message };
return Promise.resolve(params); // onConfirmPayment(params);
return Promise.resolve(params); // onConfirmPayment(params, message);
};

// Here we create promise calls in sequence
Expand All @@ -213,12 +215,12 @@ export class CheckoutPageComponent extends Component {
// NOTE: if unit type is line-item/units, quantity needs to be added.
// The way to pass it to checkout page is through pageData.bookingData
const { pageData, speculatedTransaction } = handlePaymentParams;
const initialParams = {
const orderParams = {
listingId: pageData.listing.id,
bookingStart: speculatedTransaction.booking.attributes.start,
bookingEnd: speculatedTransaction.booking.attributes.end,
};
return handlePaymentIntentCreation(initialParams);
return handlePaymentIntentCreation(orderParams);
}

handleSubmit(values) {
Expand Down Expand Up @@ -639,7 +641,7 @@ CheckoutPageComponent.propTypes = {
id: string,
slug: string,
}).isRequired,
sendOrderRequest: func.isRequired,
onInitiateOrder: func.isRequired,
onHandleCardPayment: func.isRequired,
handleCardPaymentInProgress: bool.isRequired,
handleCardPaymentError: propTypes.error,
Expand Down Expand Up @@ -689,9 +691,7 @@ const mapStateToProps = state => {

const mapDispatchToProps = dispatch => ({
dispatch,
sendOrderRequest: (params, initialMessage) => dispatch(initiateOrder(params, initialMessage)),
sendOrderRequestAfterEnquiry: (transactionId, params) =>
dispatch(initiateOrderAfterEnquiry(transactionId, params)),
onInitiateOrder: (params, transactionId) => dispatch(initiateOrder(params, transactionId)),
fetchSpeculatedTransaction: params => dispatch(speculateTransaction(params)),
onHandleCardPayment: params => dispatch(handleCardPayment(params)),
});
Expand Down

0 comments on commit e0bb4f8

Please sign in to comment.