-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #27 from Plant-for-the-Planet-org/feature/paypalSm…
…artButtons Paypal added
- Loading branch information
Showing
7 changed files
with
169 additions
and
182 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.