Skip to content

Commit

Permalink
Merge pull request #1090 from sharetribe/handle-card-payment-thunk
Browse files Browse the repository at this point in the history
Thunk function for stripe.handleCardPayment
  • Loading branch information
Gnito authored May 16, 2019
2 parents 6bcd03d + f3ce38b commit 52d875a
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions src/ducks/stripe.duck.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ export const STRIPE_PAYMENT_TOKEN_CREATE_REQUEST = 'app/stripe/STRIPE_PAYMENT_TO
export const STRIPE_PAYMENT_TOKEN_CREATE_SUCCESS = 'app/stripe/STRIPE_PAYMENT_TOKEN_CREATE_SUCCESS';
export const STRIPE_PAYMENT_TOKEN_CREATE_ERROR = 'app/stripe/STRIPE_PAYMENT_TOKEN_CREATE_ERROR';

export const HANDLE_CARD_PAYMENT_REQUEST = 'app/stripe/HANDLE_CARD_PAYMENT_REQUEST';
export const HANDLE_CARD_PAYMENT_SUCCESS = 'app/stripe/HANDLE_CARD_PAYMENT_SUCCESS';
export const HANDLE_CARD_PAYMENT_ERROR = 'app/stripe/HANDLE_CARD_PAYMENT_ERROR';

// ================ Reducer ================ //

const initialState = {
Expand All @@ -36,6 +40,9 @@ const initialState = {
stripePaymentTokenInProgress: false,
stripePaymentTokenError: false,
stripePaymentToken: null,
handleCardPaymentInProgress: false,
handleCardPaymentError: null,
paymentIntent: null,
};

export default function reducer(state = initialState, action = {}) {
Expand Down Expand Up @@ -113,6 +120,18 @@ export default function reducer(state = initialState, action = {}) {
console.error(payload);
return { ...state, stripePaymentTokenError: payload, stripePaymentTokenInProgress: false };

case HANDLE_CARD_PAYMENT_REQUEST:
return {
...state,
handleCardPaymentError: null,
handleCardPaymentInProgress: true,
};
case HANDLE_CARD_PAYMENT_SUCCESS:
return { ...state, paymentIntent: payload, handleCardPaymentInProgress: false };
case HANDLE_CARD_PAYMENT_ERROR:
console.error(payload);
return { ...state, handleCardPaymentError: payload, handleCardPaymentInProgress: false };

default:
return state;
}
Expand Down Expand Up @@ -184,6 +203,21 @@ export const stripePaymentTokenCreateError = payload => ({
error: true,
});

export const handleCardPaymentRequest = () => ({
type: HANDLE_CARD_PAYMENT_REQUEST,
});

export const handleCardPaymentSuccess = payload => ({
type: HANDLE_CARD_PAYMENT_SUCCESS,
payload,
});

export const handleCardPaymentError = payload => ({
type: HANDLE_CARD_PAYMENT_ERROR,
payload,
error: true,
});

// ================ Thunks ================ //

// Util: rename address fields to match Stripe API specifications
Expand Down Expand Up @@ -494,6 +528,51 @@ export const createStripeAccount = payoutDetails => (dispatch, getState, sdk) =>
}
};

export const handleCardPayment = params => dispatch => {
// It's required to use the same instance of Stripe as where the card has been created
// so that's why Stripe needs to be passed here and we can't create a new instance.
const { stripe, card, stripePaymentIntentClientSecret, paymentParams } = params;

dispatch(handleCardPaymentRequest());

return stripe
.handleCardPayment(stripePaymentIntentClientSecret, card, paymentParams)
.then(response => {
if (response.error) {
return Promise.reject(response);
} else {
dispatch(handleCardPaymentSuccess(response));
return response;
}
})
.catch(err => {
const containsPaymentIntent = err.error && err.error.payment_intent;
const e = containsPaymentIntent ? err.error : storableError(err);

dispatch(handleCardPaymentError(e));

if (config.dev) {
// Print sensitive error object only to browser's console in dev mode
console.error('stripe.handleCardPayment failed with error', err);
}

// Log error
const { code, doc_url, message, payment_intent } = containsPaymentIntent ? err.error : {};
const loggableError = containsPaymentIntent
? {
code,
message,
doc_url,
paymentIntentStatus: payment_intent.status,
}
: storableError(err);
log.error(loggableError, 'stripe-handle-card-payment-failed', {
stripeMessage: loggableError.message,
});
throw e;
});
};

export const createStripePaymentToken = params => dispatch => {
// It's required to use the same instance of Stripe as where the card has been created
// so that's why Stripe needs to be passed here and we can't create a new instance.
Expand Down

0 comments on commit 52d875a

Please sign in to comment.