【问题标题】:integrate shopping cart value in google pay in react js将购物车价值整合到 google pay 中的 react js
【发布时间】:2021-07-22 06:14:56
【问题描述】:

我遇到了一个问题,我想在我点击使用谷歌支付按钮时将购物车价值整合到我的谷歌支付中,我想显示我选择的金额,谁能告诉我该怎么做??

这里使用 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

【问题讨论】:

    标签: reactjs bootstrap-4 google-pay


    【解决方案1】:

    您需要根据您的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。

    【讨论】:

      猜你喜欢
      • 2022-11-24
      • 2012-04-03
      • 2015-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-01
      • 2021-04-10
      • 1970-01-01
      相关资源
      最近更新 更多