2020-03-13 12:25:57 +03:00
|
|
|
/* eslint-disable camelcase */
|
|
|
|
|
|
|
|
import React, { Component } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import { createSelector } from 'reselect';
|
2020-04-20 22:16:34 +03:00
|
|
|
import PayPalButtonScriptLoader from './PayPalButtonScriptLoader';
|
2020-03-23 19:33:37 +05:30
|
|
|
import { paypalClientId, deploymentEnv } from '../../../config/env.json';
|
2020-03-13 12:25:57 +03:00
|
|
|
import { verifySubscriptionPaypal } from '../../utils/ajax';
|
2020-03-23 19:33:37 +05:30
|
|
|
import {
|
|
|
|
paypalConfigurator,
|
|
|
|
paypalConfigTypes
|
|
|
|
} from '../../../../config/donation-settings';
|
2020-03-20 04:33:27 +03:00
|
|
|
import { signInLoadingSelector, userSelector } from '../../redux';
|
2020-03-13 12:25:57 +03:00
|
|
|
|
|
|
|
export class PaypalButton extends Component {
|
2020-03-20 04:33:27 +03:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2020-03-13 12:25:57 +03:00
|
|
|
this.handleApproval = this.handleApproval.bind(this);
|
|
|
|
}
|
|
|
|
|
2020-03-20 04:33:27 +03:00
|
|
|
state = {};
|
|
|
|
|
|
|
|
static getDerivedStateFromProps(props, state) {
|
|
|
|
const { donationAmount, donationDuration } = props;
|
|
|
|
|
|
|
|
const configurationObj = paypalConfigurator(
|
|
|
|
donationAmount,
|
2020-03-23 19:33:37 +05:30
|
|
|
donationDuration,
|
|
|
|
paypalConfigTypes[deploymentEnv || 'staging']
|
2020-03-20 04:33:27 +03:00
|
|
|
);
|
|
|
|
if (state === configurationObj) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return { ...configurationObj };
|
|
|
|
}
|
|
|
|
|
2020-03-13 12:25:57 +03:00
|
|
|
handleApproval = data => {
|
2020-03-20 04:33:27 +03:00
|
|
|
const { amount, duration } = this.state;
|
2020-03-20 13:07:02 +05:30
|
|
|
const { skipAddDonation = false } = this.props;
|
2020-04-20 22:16:34 +03:00
|
|
|
if (!skipAddDonation || duration === 'oneTime') {
|
2020-03-20 13:07:02 +05:30
|
|
|
this.props.handleProcessing(
|
|
|
|
duration,
|
|
|
|
amount,
|
|
|
|
'Paypal payment submission'
|
|
|
|
);
|
|
|
|
this.props.onDonationStateChange(false, true, '');
|
|
|
|
verifySubscriptionPaypal(data)
|
|
|
|
.then(response => {
|
|
|
|
const data = response && response.data;
|
|
|
|
this.props.onDonationStateChange(
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
data.error ? data.error : ''
|
|
|
|
);
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
const data =
|
|
|
|
error.response && error.response.data
|
|
|
|
? error.response.data
|
|
|
|
: {
|
|
|
|
error: `Something is not right. Please contact team@freecodecamp.org`
|
|
|
|
};
|
|
|
|
this.props.onDonationStateChange(false, false, data.error);
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.props.onDonationStateChange(
|
|
|
|
true,
|
|
|
|
false,
|
|
|
|
data.error ? data.error : ''
|
|
|
|
);
|
|
|
|
}
|
2020-03-13 12:25:57 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
2020-04-20 22:16:34 +03:00
|
|
|
const { duration, planId, amount } = this.state;
|
|
|
|
const isSubscription = duration !== 'onetime';
|
|
|
|
return (
|
|
|
|
<PayPalButtonScriptLoader
|
|
|
|
amount={amount}
|
|
|
|
clinetId={paypalClientId}
|
|
|
|
createOrder={(data, actions) => {
|
|
|
|
return actions.order.create({
|
|
|
|
purchase_units: [
|
|
|
|
{
|
|
|
|
amount: {
|
|
|
|
currency_code: 'USD',
|
|
|
|
value: (amount / 100).toString()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
createSubscription={(data, actions) => {
|
|
|
|
return actions.subscription.create({
|
|
|
|
plan_id: planId
|
|
|
|
});
|
|
|
|
}}
|
|
|
|
isSubscription={isSubscription}
|
2020-04-24 19:57:19 +03:00
|
|
|
on={true}
|
|
|
|
onApprove={data => {
|
|
|
|
this.handleApproval(data);
|
2020-04-20 22:16:34 +03:00
|
|
|
}}
|
|
|
|
onCancel={() => {
|
|
|
|
this.props.onDonationStateChange(
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
`Uh - oh. It looks like your transaction didn't go through. Could you please try again?`
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
onError={() =>
|
|
|
|
this.props.onDonationStateChange(false, false, 'Please try again.')
|
|
|
|
}
|
|
|
|
plantId={planId}
|
|
|
|
style={{
|
|
|
|
tagline: false,
|
|
|
|
height: 43
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
2020-03-13 12:25:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const propTypes = {
|
2020-03-20 04:33:27 +03:00
|
|
|
donationAmount: PropTypes.number,
|
|
|
|
donationDuration: PropTypes.string,
|
2020-03-13 12:25:57 +03:00
|
|
|
handleProcessing: PropTypes.func,
|
|
|
|
isDonating: PropTypes.bool,
|
2020-03-20 13:07:02 +05:30
|
|
|
onDonationStateChange: PropTypes.func,
|
|
|
|
skipAddDonation: PropTypes.bool
|
2020-03-13 12:25:57 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const mapStateToProps = createSelector(
|
|
|
|
userSelector,
|
|
|
|
signInLoadingSelector,
|
|
|
|
({ isDonating }, showLoading) => ({
|
|
|
|
isDonating,
|
|
|
|
showLoading
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
PaypalButton.displayName = 'PaypalButton';
|
|
|
|
PaypalButton.propTypes = propTypes;
|
|
|
|
|
|
|
|
export default connect(mapStateToProps)(PaypalButton);
|