Skip to content

Commit

Permalink
Merge pull request #27 from Plant-for-the-Planet-org/feature/paypalSm…
Browse files Browse the repository at this point in the history
…artButtons

Paypal added
  • Loading branch information
sagararyal authored Jun 7, 2021
2 parents 19e8db2 + fbdd266 commit 5d05864
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 182 deletions.
60 changes: 45 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"@material-ui/core": "^4.11.3",
"@material-ui/lab": "^4.0.0-alpha.57",
"@next/bundle-analyzer": "^10.1.3",
"@paypal/react-paypal-js": "^6.0.1",
"@sentry/browser": "^6.2.5",
"@sentry/integrations": "^6.2.5",
"@sentry/node": "^6.2.5",
Expand All @@ -29,7 +30,6 @@
"next-i18next": "^8.1.3",
"node-sass": "^5.0.0",
"react": "17.0.2",
"react-async-script-loader": "^0.3.0",
"react-dom": "17.0.2",
"react-hook-form": "6.14.2",
"styled-jsx": "^3.4.4",
Expand Down
14 changes: 12 additions & 2 deletions src/Donations/Components/PaymentsForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import {
import ToggleSwitch from "../../Common/InputTypes/ToggleSwitch";
import CardPayments from "../PaymentMethods/CardPayments";
import SepaPayments from "../PaymentMethods/SepaPayments";
import PaypalPayments from "../PaymentMethods/PaypalPayments";
import GiroPayPayments from "../PaymentMethods/GiroPayPayments";
import SofortPayments from "../PaymentMethods/SofortPayment";
import TaxDeductionOption from "../Micros/TaxDeductionOption";
import ButtonLoader from "../../Common/ContentLoaders/ButtonLoader";
import { useAuth0 } from "@auth0/auth0-react";
import NewPaypal from "../PaymentMethods/NewPaypal";

interface Props {}

Expand All @@ -34,6 +34,7 @@ function PaymentsForm({}: Props): ReactElement {

const { isLoading, isAuthenticated, getAccessTokenSilently } = useAuth0();

const [isDonationLoading, setisDonationLoading] = React.useState(false)
const {
paymentSetup,
country,
Expand All @@ -52,6 +53,7 @@ function PaymentsForm({}: Props): ReactElement {
giftDetails,
isTaxDeductible,
isDirectDonation,
setDonationUid
} = React.useContext(QueryParamContext);

React.useEffect(() => {
Expand Down Expand Up @@ -91,6 +93,7 @@ function PaymentsForm({}: Props): ReactElement {
if (!isLoading && isAuthenticated) {
token = await getAccessTokenSilently();
}
setisDonationLoading(true)
const donation = await createDonationFunction({
isTaxDeductible,
country,
Expand All @@ -113,7 +116,9 @@ function PaymentsForm({}: Props): ReactElement {
setdonationID(donation.id);
setshouldCreateDonation(false);
setisCreatingDonation(false);
setDonationUid(donation.uid)
}
setisDonationLoading(false)
}

// This feature allows the user to show or hide their names in the leaderboard
Expand All @@ -135,6 +140,10 @@ function PaymentsForm({}: Props): ReactElement {
}
}, [shouldCreateDonation]);

React.useEffect(()=>{
setPaymentType("CARD")
},[currency])

return ready ? (
isPaymentProcessing ? (
<PaymentProgress isPaymentProcessing={isPaymentProcessing} />
Expand Down Expand Up @@ -273,13 +282,14 @@ function PaymentsForm({}: Props): ReactElement {
aria-labelledby={`scrollable-force-tab-${"Paypal"}`}
>
{paymentType === "Paypal" && (
<PaypalPayments
<NewPaypal
paymentSetup={paymentSetup}
treeCount={treeCount}
treeCost={paymentSetup.treeCost}
currency={currency}
donationID={donationID}
payDonationFunction={onSubmitPayment}
setPaymentError={setPaymentError}
/>
)}
</div>
Expand Down
105 changes: 105 additions & 0 deletions src/Donations/PaymentMethods/NewPaypal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import React, { ReactElement } from "react";
import {
PayPalScriptProvider,
PayPalButtons,
usePayPalScriptReducer,
} from "@paypal/react-paypal-js";
import { QueryParamContext } from "../../Layout/QueryParamContext";

interface Props {
paymentSetup: any;
treeCount: number;
treeCost: number;
currency: string;
donationID: any;
payDonationFunction: Function;
setPaymentError: Function;
}

function NewPaypal({
paymentSetup,
treeCount,
treeCost,
currency,
donationID,
payDonationFunction,
setPaymentError
}: Props): ReactElement {
const initialOptions = {
"client-id": paymentSetup?.gateways.paypal.authorization.client_id,
"enable-funding": "venmo,giropay,sofort",
"disable-funding": "card",
currency: currency,
};

const { donationUid } = React.useContext(QueryParamContext);

function createOrder(data, actions) {
return actions.order.create({
purchase_units: [
{
amount: {
value: (treeCount * treeCost).toFixed(2),
currency: currency,
},
invoice_id: `planet-${donationID}`,
custom_id: donationUid,
},
],
application_context: {
brand_name: "Plant-for-the-Planet",
},
});
}

function onApprove(data, actions) {
return actions.order.capture().then(function (details) {
// This function shows a transaction success message to your buyer.
data = {
...data,
type: "sdk",
};
payDonationFunction("paypal", data);
});
}

const onError = (data) => {
setPaymentError(`Your order ${data.orderID} failed due to some error.`)
};

const onCancel = (data) => {
setPaymentError('Order was cancelled, please try again')
};

return (
<>
<PayPalScriptProvider options={initialOptions}>
<ReloadButton currency={currency} />
<PayPalButtons
createOrder={createOrder}
onError={onError}
onApprove={onApprove}
onCancel={onCancel}
/>
</PayPalScriptProvider>
</>
);
}

function ReloadButton({ currency }: any) {
const [{ isPending, options }, dispatch] = usePayPalScriptReducer();

React.useEffect(() => {
dispatch({
type: "resetOptions",
value: {
...options,
currency: currency,
},
});
}, [currency]);

return isPending ? <div className="spinner" /> : null;
}

export default NewPaypal;
Loading

0 comments on commit 5d05864

Please sign in to comment.