I need help with my project I want to add a dynamic option to the PayPal payment process. ( change the value to be dynamic )
the default option is value: ‘0.01’ and the dynamic payment in my project is cart.subtotal.formatted_with_symbol
I try to add [const {amount} = cart.subtotal.formatted_with_symbol;] this line to try to change the value to value: amount but this is not working for me.
thanks for help
JavaScript
x
41
41
1
import React from 'react'
2
import ReactDOM from "react-dom"
3
import CartItem from './CartItem';
4
5
const PayPalButton = window.paypal.Buttons.driver("react", { React, ReactDOM });
6
7
// paypal payment buttons
8
const createOrder = (data, actions) => {
9
const {amount} = cart.subtotal.formatted_with_symbol;
10
return actions.order.create({
11
purchase_units: [
12
{
13
amount: {
14
value: amount,
15
},
16
},
17
],
18
});
19
}
20
21
const onApprove = (data, actions) => {
22
return actions.order.capture();
23
}
24
25
onst FilledCart = () => (
26
<>
27
<div>
28
{cart.line_items.map((item) => (
29
<div key={item.id}>
30
<CartItem item={item} handleUpdateCratQty={handleUpdateCratQty} handleRemoveFromCart={handleRemoveFromCart} />
31
</div>
32
))}
33
</div>
34
<div>
35
<div>
36
<button onClick={handleEmptyCart}>EmptyCart</button>
37
</div>
38
</div>
39
</>
40
);
41
Advertisement
Answer
The react-paypal-js documentation is straightforward enough…
JavaScript
1
28
28
1
import { useEffect } from "react";
2
import {
3
PayPalScriptProvider,
4
PayPalButtons,
5
usePayPalScriptReducer
6
} from "@paypal/react-paypal-js";
7
8
// This values are the props in the UI
9
const amount = "2";
10
const currency = "USD";
11
const style = {"layout":"vertical"};
12
13
// Custom component to wrap the PayPalButtons and handle currency changes
14
const ButtonWrapper = ({ currency, showSpinner }) => {
15
// usePayPalScriptReducer can be use only inside children of PayPalScriptProviders
16
// This is the main reason to wrap the PayPalButtons in a new component
17
const [{ options, isPending }, dispatch] = usePayPalScriptReducer();
18
19
useEffect(() => {
20
dispatch({
21
type: "resetOptions",
22
value: {
23
options,
24
currency: currency,
25
},
26
});
27
}, [currency, showSpinner]);
28