-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c7b7b69
commit 60c9a03
Showing
9 changed files
with
360 additions
and
4 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
import { useState } from "react"; | ||
import { PayPalButtons } from "@paypal/react-paypal-js"; | ||
import getConfig from 'next/config'; | ||
import { IconRefresh } from '@tabler/icons'; | ||
|
||
const { publicRuntimeConfig } = getConfig(); | ||
|
||
const PaypalCheckout = ({ email, onSuccess, onError }) => { | ||
const [loading, setLoading] = useState(false); | ||
|
||
const handleCreateOrder = async () => { | ||
try { | ||
setLoading(true); | ||
const response = await fetch(publicRuntimeConfig.PAYPAL_ORDERS_API, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ | ||
}), | ||
}); | ||
|
||
const orderData = await response.json(); | ||
setLoading(false); | ||
|
||
if (orderData.id) { | ||
return orderData.id; | ||
} else { | ||
const errorDetail = orderData?.details?.[0]; | ||
const errorMessage = errorDetail | ||
? `${errorDetail.issue} ${errorDetail.description} (${orderData.debug_id})` | ||
: JSON.stringify(orderData); | ||
|
||
throw new Error(errorMessage); | ||
} | ||
} catch (error) { | ||
onError(error); | ||
setLoading(false); | ||
throw error; | ||
} | ||
}; | ||
|
||
const handleApproveOrder = async (data, actions) => { | ||
try { | ||
setLoading(true); | ||
const response = await fetch(`${publicRuntimeConfig.PAYPAL_ORDERS_API}/${data.orderID}/capture`, { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
}); | ||
|
||
const orderData = await response.json(); | ||
setLoading(false); | ||
// Three cases to handle: | ||
// (1) Recoverable INSTRUMENT_DECLINED -> call actions.restart() | ||
// (2) Other non-recoverable errors -> Show a failure message | ||
// (3) Successful transaction -> Show confirmation or thank you message | ||
|
||
const errorDetail = orderData?.details?.[0]; | ||
|
||
if (errorDetail?.issue === "INSTRUMENT_DECLINED") { | ||
// (1) Recoverable INSTRUMENT_DECLINED -> call actions.restart() | ||
// recoverable state, per https://developer.paypal.com/docs/checkout/standard/customize/handle-funding-failures/ | ||
return actions.restart(); | ||
} else if (errorDetail) { | ||
// (2) Other non-recoverable errors -> Show a failure message | ||
throw new Error( | ||
`${errorDetail.description} (${orderData.debug_id})`, | ||
); | ||
} else { | ||
// (3) Successful transaction -> Show confirmation or thank you message | ||
// Or go to another URL: actions.redirect('thank_you.html'); | ||
onSuccess(orderData); | ||
} | ||
} catch (error) { | ||
setLoading(false); | ||
onError(error); | ||
} | ||
}; | ||
|
||
return ( | ||
<> | ||
<div className="flex items-center"> | ||
<div className="font-semibold text-lg mr-1">Order Summary</div> | ||
{loading && <IconRefresh size={18} className="loading-icon" />} | ||
</div> | ||
<div className="mt-2 mb-4"> | ||
<div className='flex mb-1'> | ||
<span className='mr-1' style={{width: '65px'}}>Product</span>: Golden Edition Individual License | ||
</div> | ||
<div className='flex mb-1'> | ||
<span className='mr-1' style={{width: '65px'}}>Email</span>: [email protected] | ||
</div> | ||
<div className='flex'> | ||
<span className='mr-1' style={{width: '65px'}}>Amount</span>: $9 | ||
</div> | ||
</div> | ||
<div style={{maxWidth: '300px'}}> | ||
<PayPalButtons | ||
style={{ | ||
shape: "rect", | ||
layout: "vertical", | ||
}} | ||
createOrder={handleCreateOrder} | ||
onApprove={handleApproveOrder} | ||
/> | ||
</div> | ||
</> | ||
); | ||
}; | ||
|
||
export default PaypalCheckout; |
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
Oops, something went wrong.