首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在react js中集成google pay中的购物车价值

在react js中集成google pay中的购物车价值
EN

Stack Overflow用户
提问于 2021-07-22 14:14:56
回答 1查看 57关注 0票数 0

我遇到了一个问题,当我用google按钮点击支付时,我想在我的google pay中集成购物车价值,我想显示我选择的金额,有人能告诉我怎么做吗??

这里我使用react-use-cart包添加到购物车产品集成google pay我使用@google-pay/button-react npm包

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2021-07-24 05:05:34

您需要根据您的cartTotal设置transactionInfo.totalPrice字段。

根据您的示例,它将如下所示:

代码语言:javascript
复制
<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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68479996

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档