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-12-16 02:02:52 -06:00
|
|
|
import { withTranslation } from 'react-i18next';
|
|
|
|
|
2021-03-26 00:43:43 +05:30
|
|
|
import envData from '../../../../config/env.json';
|
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
|
|
|
|
2021-03-26 00:43:43 +05:30
|
|
|
const { paypalClientId, deploymentEnv } = envData;
|
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-04-25 00:00:43 +05:30
|
|
|
handleApproval = (data, isSubscription) => {
|
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-10-14 13:23:26 +03:00
|
|
|
|
2020-04-25 00:00:43 +05:30
|
|
|
// Skip the api if user is not signed in or if its a one-time donation
|
2020-10-14 13:23:26 +03:00
|
|
|
if (!skipAddDonation || isSubscription) {
|
|
|
|
this.props.addDonation(data);
|
2020-03-20 13:07:02 +05:30
|
|
|
}
|
2020-10-14 13:23:26 +03:00
|
|
|
|
|
|
|
this.props.handleProcessing(duration, amount, 'Paypal payment submission');
|
|
|
|
|
|
|
|
// Show success anytime because the payment has gone through paypal
|
|
|
|
this.props.onDonationStateChange({
|
|
|
|
processing: false,
|
|
|
|
success: true,
|
|
|
|
error: data.error ? data.error : null
|
|
|
|
});
|
2020-03-13 12:25:57 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
2020-04-20 22:16:34 +03:00
|
|
|
const { duration, planId, amount } = this.state;
|
2020-12-16 02:02:52 -06:00
|
|
|
const { t } = this.props;
|
2020-04-20 22:16:34 +03:00
|
|
|
const isSubscription = duration !== 'onetime';
|
2020-05-12 15:34:24 +03:00
|
|
|
|
|
|
|
if (!paypalClientId) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-04-20 22:16:34 +03:00
|
|
|
return (
|
|
|
|
<PayPalButtonScriptLoader
|
|
|
|
amount={amount}
|
2020-05-12 15:34:24 +03:00
|
|
|
clientId={paypalClientId}
|
2020-04-20 22:16:34 +03:00
|
|
|
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
|
|
|
onApprove={data => {
|
2020-04-25 00:00:43 +05:30
|
|
|
this.handleApproval(data, isSubscription);
|
2020-04-20 22:16:34 +03:00
|
|
|
}}
|
|
|
|
onCancel={() => {
|
2020-10-14 13:23:26 +03:00
|
|
|
this.props.onDonationStateChange({
|
|
|
|
processing: false,
|
|
|
|
success: false,
|
2020-12-16 02:02:52 -06:00
|
|
|
error: t('donate.failed-pay')
|
2020-10-14 13:23:26 +03:00
|
|
|
});
|
2020-04-20 22:16:34 +03:00
|
|
|
}}
|
|
|
|
onError={() =>
|
2020-10-14 13:23:26 +03:00
|
|
|
this.props.onDonationStateChange({
|
|
|
|
processing: false,
|
|
|
|
success: false,
|
2020-12-16 02:02:52 -06:00
|
|
|
error: t('donate.try-again')
|
2020-10-14 13:23:26 +03:00
|
|
|
})
|
2020-04-20 22:16:34 +03:00
|
|
|
}
|
|
|
|
plantId={planId}
|
|
|
|
style={{
|
|
|
|
tagline: false,
|
|
|
|
height: 43
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
);
|
2020-03-13 12:25:57 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const propTypes = {
|
2020-10-14 13:23:26 +03:00
|
|
|
addDonation: PropTypes.func,
|
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,
|
2020-12-16 02:02:52 -06:00
|
|
|
skipAddDonation: PropTypes.bool,
|
|
|
|
t: PropTypes.func.isRequired
|
2020-03-13 12:25:57 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
const mapStateToProps = createSelector(
|
|
|
|
userSelector,
|
|
|
|
signInLoadingSelector,
|
|
|
|
({ isDonating }, showLoading) => ({
|
|
|
|
isDonating,
|
|
|
|
showLoading
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
PaypalButton.displayName = 'PaypalButton';
|
|
|
|
PaypalButton.propTypes = propTypes;
|
|
|
|
|
2020-12-16 02:02:52 -06:00
|
|
|
export default connect(mapStateToProps)(withTranslation()(PaypalButton));
|