我遇到了一个问题,当我用google按钮点击支付时,我想在我的google pay中集成购物车价值,我想显示我选择的金额,有人能告诉我怎么做吗??
这里我使用react-use-cart包添加到购物车产品集成google pay我使用@google-pay/button-react npm包
import React from 'react';
import { useCart } from 'react-use-cart';
import GooglePayButton from '@google-pay/button-react';
const Cart = () => {
const {
isEmpty,
totalUniqueItems,
items,
totalItems,
cartTotal,
updateItemQuantity,
removeItem,
emptyCart,
} = useCart();
if (isEmpty) return <h1 className="text-center">Your Cart is Empty</h1>
return (
<section className="py-4 container">
<div className="row justify-content-center">
<div className="col-12">
<h5 className="mb-5">Cart({totalUniqueItems}) total Items:({totalItems})</h5>
<table className="table table-light table-hover m-0 shadow p-2 mb-3">
<tbody>
{items.map((fruit, index) => {
return (
<tr key={index} >
<td>
<img className="img-fluid" src={fruit.img} style={{ height: '5rem', marginRight: '10px' }} />
</td>
<td>
{fruit.title}
</td>
<td>
{fruit.price}
</td>
<td>
Quantity({fruit.quantity})
</td>
<td>
<button className="btn btn-info ms-2 mr-2 " onClick={() => updateItemQuantity(fruit.id, fruit.quantity - 1)}>-</button>
<button className="btn btn-info ms-2 mr-2" onClick={() => updateItemQuantity(fruit.id, fruit.quantity + 1)}>+</button>
<button className="btn btn-danger ms-2" onClick={() => removeItem(fruit.id)}>Remove Item</button>
</td>
</tr>
)
})}
</tbody>
</table>
</div>
<div className="col-auto mx-auto">
<h2>Total Price: ${cartTotal}</h2>
</div>
<div className="col-auto" >
<button className="btn btn-danger btn-block mt-2" onClick={() => emptyCart()}>Clear cart</button>
<GooglePayButton
environment="TEST"
paymentRequest={{
apiVersion: 2,
apiVersionMinor: 0,
allowedPaymentMethods: [
{
type: 'CARD',
parameters: {
allowedAuthMethods: ['PAN_ONLY', 'CRYPTOGRAM_3DS'],
allowedCardNetworks: ['MASTERCARD', 'VISA'],
},
tokenizationSpecification: {
type: 'PAYMENT_GATEWAY',
parameters: {
gateway: 'example',
gatewayMerchantId: 'exampleGatewayMerchantId',
},
},
},
],
merchantInfo: {
merchantId: '12345678901234567890',
merchantName: 'Demo Merchant',
},
transactionInfo: {
totalPriceStatus: 'FINAL',
totalPriceLabel: 'Total',
totalPrice: '100.00',
currencyCode: 'USD',
countryCode: 'US',
},
}}
onLoadPaymentData={paymentRequest => {
alert('Payment success', paymentRequest);
}}
/>
</div>
</div>
</section>
)
}
export default Cart发布于 2021-07-24 05:05:34
您需要根据您的cartTotal设置transactionInfo.totalPrice字段。
根据您的示例,它将如下所示:
<GooglePayButton
environment="TEST"
paymentRequest={{
apiVersion: 2,
apiVersionMinor: 0,
allowedPaymentMethods: [
{
type: 'CARD',
parameters: {
allowedAuthMethods: ['PAN_ONLY', 'CRYPTOGRAM_3DS'],
allowedCardNetworks: ['MASTERCARD', 'VISA'],
},
tokenizationSpecification: {
type: 'PAYMENT_GATEWAY',
parameters: {
gateway: 'example',
gatewayMerchantId: 'exampleGatewayMerchantId',
},
},
},
],
merchantInfo: {
merchantId: '12345678901234567890',
merchantName: 'Demo Merchant',
},
transactionInfo: {
totalPriceStatus: 'FINAL',
totalPriceLabel: 'Total',
totalPrice: cartTotal,
currencyCode: 'USD',
countryCode: 'US',
},
}}
onLoadPaymentData={paymentRequest => {
alert('Payment success', paymentRequest);
}}
/>请注意,它只包含一行更改:totalPrice: cartTotal。
https://stackoverflow.com/questions/68479996
复制相似问题