From 368aa688fa7b5abfda2442e2c749082898fb7da9 Mon Sep 17 00:00:00 2001 From: Mrugesh Mohapatra Date: Sun, 20 Oct 2019 16:10:38 +0530 Subject: [PATCH] fix(client): call donate api withCredentials --- .../Donation/components/DonateForm.js | 33 +++++++++++-------- client/src/utils/ajax.js | 12 +++++++ 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/client/src/components/Donation/components/DonateForm.js b/client/src/components/Donation/components/DonateForm.js index c5ceddae4d..d4f9b2c9ae 100644 --- a/client/src/components/Donation/components/DonateForm.js +++ b/client/src/components/Donation/components/DonateForm.js @@ -14,11 +14,10 @@ import { } from '@freecodecamp/react-bootstrap'; import { injectStripe } from 'react-stripe-elements'; -import { apiLocation } from '../../../../config/env.json'; import Spacer from '../../../components/helpers/Spacer'; import StripeCardForm from './StripeCardForm'; import DonateCompletion from './DonateCompletion'; -import { postJSON$ } from '../../../templates/Challenges/utils/ajax-stream.js'; +import { postChargeStripe } from '../../../utils/ajax'; import { userSelector, isSignedInSelector } from '../../../redux'; const propTypes = { @@ -123,34 +122,40 @@ class DonateForm extends Component { } })); - const chargeStripePath = isSignedIn - ? `${apiLocation}/internal/donate/charge-stripe` - : `${apiLocation}/unauthenticated/donate/charge-stripe`; - return postJSON$(chargeStripePath, { + return postChargeStripe(isSignedIn, { token, amount - }).subscribe( - res => + }) + .then(response => { + const data = response && response.data; this.setState(state => ({ ...state, donationState: { ...state.donationState, processing: false, success: true, - error: res.error + error: data.error ? data.error : null } - })), - err => + })); + }) + .catch(error => { + const data = + error.response && error.response.data + ? error.response.data + : { + error: + 'Something is not right. Please contact team@freecodecamp.org' + }; this.setState(state => ({ ...state, donationState: { ...state.donationState, processing: false, success: false, - error: err.error + error: data.error } - })) - ); + })); + }); } resetDonation() { diff --git a/client/src/utils/ajax.js b/client/src/utils/ajax.js index 793a9ffad4..b21a80cdc1 100644 --- a/client/src/utils/ajax.js +++ b/client/src/utils/ajax.js @@ -3,9 +3,14 @@ import { apiLocation } from '../../config/env.json'; import axios from 'axios'; const base = apiLocation + '/internal'; +const baseUnauthenticated = apiLocation + '/unauthenticated'; axios.defaults.withCredentials = true; +export function postUnauthenticated(path, body) { + return axios.post(`${baseUnauthenticated}${path}`, body); +} + function get(path) { return axios.get(`${base}${path}`); } @@ -45,6 +50,13 @@ export function getArticleById(shortId) { } /** POST **/ +export function postChargeStripe(isSignedIn, body) { + const donatePath = '/donate/charge-stripe'; + return isSignedIn + ? post(donatePath, body) + : postUnauthenticated(donatePath, body); +} + export function putUpdateLegacyCert(body) { return post('/update-my-projects', body); }