简体   繁体   English

将购物车价值整合到 google pay 中的 react js

[英]integrate shopping cart value in google pay in react js

am stuck with a problem, I want to integrate shopping cart value in my google pay when I click pay with google button , I want to display amount those I selected, can anyone tell me how can I do it??遇到一个问题,我想在我点击谷歌支付按钮时将购物车价值整合到我的谷歌支付中,我想显示我选择的金额,谁能告诉我该怎么做?

Here am using react-use-cart package for add to cart product for integrate google pay i used @google-pay/button-react npm package这里我使用 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

You need to set the transactionInfo.totalPrice field based your cartTotal .您需要根据您的cartTotal设置transactionInfo.totalPrice字段。

Based on your example, it would look something like this:根据您的示例,它看起来像这样:

<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);
    }}
/>

Notice that it includes a single line change: totalPrice: cartTotal .请注意,它包含一行更改: totalPrice: cartTotal

transactionInfo: {
                                totalPriceStatus: 'FINAL',
                                totalPriceLabel: 'Total',
                                totalPrice: '<!Look description below!>',
                                currencyCode: 'USD',
                                countryCode: 'US',
                            },

You should put the total-price from cart state to string totalPrice in your code (transactionInfo parapeters)您应该将购物车状态的总价格放入代码中的字符串 totalPrice (transactionInfo parapeters)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM