Skip to content

Commit

Permalink
Cybersource updates (#374)
Browse files Browse the repository at this point in the history
* WIP Cybersource updates

- Port cybersource updates based on WIP

* Updated credit card tokenization

* updating page after add new payment method

* Updated AddPaymentMethod component

* Added HolderName and AddPayment validation

* Update creditCardTokenization config and remove failed message in paymentForm
  • Loading branch information
shaunmaharaj committed Sep 16, 2019
1 parent ce8c900 commit e87213b
Show file tree
Hide file tree
Showing 10 changed files with 266 additions and 72 deletions.
171 changes: 171 additions & 0 deletions app/src/containers/AddPaymentMethod.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
/**
* Copyright © 2019 Elastic Path Software Inc. All rights reserved.
*
* This is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this license. If not, see
*
* https://www.gnu.org/licenses/
*
*
*/

import React from 'react';
import ReactRouterPropTypes from 'react-router-prop-types';
import intl from 'react-intl-universal';
import { cortexFetch } from '../utils/Cortex';
import { login } from '../utils/AuthService';

import * as Config from '../ep.config.json';

interface AddPaymentMethodProps {
match: any,
history: any,
}
interface AddPaymentMethodState {
isLoading: boolean,
errorMessage: string,
}

class AddPaymentMethod extends React.Component<AddPaymentMethodProps, AddPaymentMethodState> {
static propTypes = {
history: ReactRouterPropTypes.history.isRequired,
};

constructor(props) {
super(props);
this.state = {
isLoading: true,
errorMessage: '',
};
this.submitPayment = this.submitPayment.bind(this);
this.handleClose = this.handleClose.bind(this);
this.goToBack = this.goToBack.bind(this);
}

componentDidMount() {
this.submitPayment();
}

submitPayment() {
const data = window.location.search.replace('?', '');
const paymentData = JSON.parse(`{"${decodeURI(data.replace(/&/g, '","')
.replace(/=/g, '":"'))}"}`);
let codeCardType = '';
let cardType = '';
let paymentForm;

if (paymentData.payment_token && paymentData.req_card_type && paymentData.req_bill_to_forename && paymentData.req_bill_to_surname && paymentData.req_card_number) {
codeCardType = paymentData.req_card_type ? paymentData.req_card_type : '';
switch (codeCardType) {
case '001':
cardType = 'Visa';
break;
case '002':
cardType = 'Mastercard';
break;
case '003':
cardType = 'American Express';
break;
default:
}
login().then(() => {
cortexFetch('/?zoom=defaultcart:order:paymentmethodinfo:paymenttokenform,defaultprofile:paymentmethods:paymenttokenform', {
headers: {
'Content-Type': 'application/json',
Authorization: localStorage.getItem(`${Config.cortexApi.scope}_oAuthToken`),
},
})
.then(res => res.json())
.then((res) => {
const paymentFormLink = res._defaultprofile[0]._paymentmethods[0]._paymenttokenform[0].links.find(
link => link.rel === 'createpaymenttokenaction',
);
paymentForm = paymentFormLink.uri;
cortexFetch(paymentForm, {
method: 'post',
headers: {
'Content-Type': 'application/json',
Authorization: localStorage.getItem(`${Config.cortexApi.scope}_oAuthToken`),
},
body: JSON.stringify({
'display-name': `${paymentData.req_bill_to_forename} ${paymentData.req_bill_to_surname}'s ${cardType} ending in: ${paymentData.req_card_number}`,
token: paymentData.payment_token,
}),
})
.then(() => {
this.setState({
isLoading: false,
});
this.handleClose();
})
.catch((error) => {
this.setState({
isLoading: false,
});
// eslint-disable-next-line no-console
console.error(error.message);
});
})
.catch((error) => {
this.setState({
isLoading: false,
});
// eslint-disable-next-line no-console
console.error(error.message);
});
});
} else {
const errorMessage = paymentData.message ? paymentData.message.replace(/\+/g, ' ') : '';
this.setState({
errorMessage,
isLoading: false,
});
}
}

handleClose() {
const { history } = this.props;
history.push('/profile');
}

goToBack() {
const { history } = this.props;
history.push('/profile');
}

render() {
const { isLoading, errorMessage } = this.state;
return (
<div>
{isLoading ? (<div className="loader" />) : ''}
{errorMessage.length ? (
<div style={{ margin: '20px' }} className="error-message">
<div style={{ color: 'red', padding: '20px 0' }}>
{errorMessage}
</div>
<div>
<button className="ep-btn payment-cancel-btn" data-el-label="paymentForm.cancel" type="button" onClick={() => { this.goToBack(); }}>
{intl.get('go-back')}
</button>
<button className="ep-btn primary new-payment-btn" type="button" onClick={() => { this.goToBack(); }}>
{intl.get('add-new-payment-method')}
</button>
</div>
</div>
) : ''}
</div>
);
}
}

export default AddPaymentMethod;
5 changes: 3 additions & 2 deletions app/src/containers/CheckoutPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,8 @@ class CheckoutPage extends React.Component<RouteComponentProps, CheckoutPageStat
}

renderPaymentSelector() {
const { profileData, openNewPaymentModal } = this.state;
const { profileData, openNewPaymentModal, orderData } = this.state;
const disableAddPayment = !(orderData && orderData._order && orderData._order[0] && orderData._order[0]._deliveries);
const isDisabled = !(!profileData || (profileData && profileData._emails[0]._element));
return (
<div>
Expand All @@ -578,7 +579,7 @@ class CheckoutPage extends React.Component<RouteComponentProps, CheckoutPageStat
<div data-region="paymentMethodSelectorsRegion" className="checkout-region-inner-container">
{this.renderPayments()}
</div>
<button className="ep-btn primary wide checkout-new-payment-btn" disabled={isDisabled} type="button" onClick={() => { this.newPayment(); }}>
<button className="ep-btn primary wide checkout-new-payment-btn" disabled={isDisabled || disableAddPayment} type="button" onClick={() => { this.newPayment(); }}>
{intl.get('add-new-payment-method')}
</button>
<Modal open={openNewPaymentModal} onClose={this.handleCloseNewPaymentModal}>
Expand Down
3 changes: 2 additions & 1 deletion app/src/containers/ProfilePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ class ProfilePage extends React.Component<RouteComponentProps, ProfilePageState>

render() {
const { profileData, showResetPasswordButton } = this.state;
const disableAddPayment = !(profileData && profileData._addresses && profileData._addresses[0]._billingaddresses);
return (
<div>
<div className="container profile-container">
Expand Down Expand Up @@ -228,7 +229,7 @@ class ProfilePage extends React.Component<RouteComponentProps, ProfilePageState>
<div className="profile-info-col">
<div className="profile-info-block">
{(profileData._paymentmethods) ? (
<ProfilePaymentMethodsMain paymentMethods={profileData._paymentmethods[0]} onChange={this.fetchProfileData} />
<ProfilePaymentMethodsMain paymentMethods={profileData._paymentmethods[0]} onChange={this.fetchProfileData} disableAddPayment={disableAddPayment} />
) : ('')}
</div>
</div>
Expand Down
4 changes: 3 additions & 1 deletion app/src/ep.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@
},
"creditCardTokenization": {
"enable": false,
"lambdaURI": ""
"lambdaURI": "/cssign",
"overrideCustomReceiptURI": "/postredirector",
"overrideCustomCancelURI": ""
},
"chatbot": {
"enable": false,
Expand Down
1 change: 1 addition & 0 deletions app/src/localization/en-CA.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
"currency": "Currency",
"logout": "Logout",
"login": "Login",
"go-back": "Go Back",
"help": "Help",
"hide": "Hide",
"shipping-returns": "Shipping & Returns",
Expand Down
1 change: 1 addition & 0 deletions app/src/localization/fr-FR.json
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
"currency": "C-u-r-r-e-n-c-y",
"logout": "L-o-g-o-u-t",
"login": "L-o-g-i-n",
"go-back": "G-o- -B-a-c-k",
"help": "H-e-l-p",
"hide": "H-i-d-e",
"shipping-returns": "S-h-i-p-p-i-n-g- -&- -R-e-t-u-r-n-s",
Expand Down
4 changes: 4 additions & 0 deletions app/src/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import ResetPasswordForm from './containers/ResetPasswordPage';
import B2BMain from './containers/b2b/B2BMain';
import Dashboard from './containers/b2b/Dashboard';
import AccountMain from './containers/b2b/AccountMain';
import AddPaymentMethod from './containers/AddPaymentMethod';

const router = [{
path: '/',
Expand Down Expand Up @@ -134,6 +135,9 @@ const router = [{
}, {
path: '/password_reset',
component: ResetPasswordForm,
}, {
path: '/newpaymentform/paymentdata',
component: AddPaymentMethod,
}, {
path: '/b2b/account/:uri',
component: AccountMain,
Expand Down
4 changes: 4 additions & 0 deletions components/src/PaymentForm/paymentform.main.less
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,7 @@
}
}
}

.payment_confirmation {
display: none;
}
Loading

0 comments on commit e87213b

Please sign in to comment.