使用 Square API 集成 Cash App 事件触发问题

问题描述 投票:0回答:1

我已经通过 Square API SDK 集成了 Cash App Payment

我使用过Javascript Web SDK & PHP SDK

我有用于购买美元的移动应用程序,并且从移动应用程序它将打开用于现金应用程序支付的网页视图(为此我使用了 Square Javascript Web SDK)

  • 使用此 webSDK 将创建 CashApp 付款按钮
  • 单击该按钮后,它将重定向到现金应用程序页面,其中有 2 个按钮
    1. 打开现金应用程序
    2. 获取现金应用程序
  • 点击“打开现金应用程序”按钮,它将打开现金应用程序并重定向付款屏幕。
  • 就我而言,当它被重定向到现金应用程序上的支付屏幕时,我的事件
    ontokenization
    被触发。我还得到
    status = OK
    token
    进行进一步处理,以使用
    PHP SDK
    进行付款。
  • 即使在屏幕上,我也不会单击 OK 按钮,然后事件就会被触发。另外,当我按下后退按钮时,事件也会被触发。

据我所知,理想的方法是当我单击OK按钮时,事件将触发并处理付款。

下面是我的代码块。

const appId = 'MYAPPID';
        const locationId = 'MY LOCATION ID';

        function buildPaymentRequest(payments) {
            const paymentRequest = payments.paymentRequest({
                countryCode: 'US',
                currencyCode: 'USD',
                total: {
                    amount: '10',
                    label: 'Total',
                },
            });
            return paymentRequest;
        }

        async function initializeCashApp(payments) {
            const paymentRequest = buildPaymentRequest(payments);
            // console.log(paymentRequest)
            const cashAppPay = await payments.cashAppPay(paymentRequest, {
                redirectURL: window.location.href,
                referenceId: 'my-website-00000001'
            });
            const buttonOptions = {
                shape: 'semiround',
                width: 'full',
            };
            await cashAppPay.attach('#cash-app-pay', buttonOptions);
            return cashAppPay;
        }

        async function createPayment(token, resData) {
            const body = JSON.stringify({
                locationId,
                sourceId: token,
                temp_order_id: '123'
            });

            const paymentResponse = await fetch('/web/payment/cash-app-payment', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body,
            });

            if (paymentResponse.ok) {
                return paymentResponse.json();
            }

            const errorBody = await paymentResponse.text();
            throw new Error(errorBody);
        }

        async function paymentFail(tokenResponse) {
            const body = JSON.stringify({
                locationId,
                temp_order_id: '123',
                tokenResponse: tokenResponse
            });

            const paymentResponse = await fetch('/web/payment/cash-app-payment-fail', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body,
            });

            if (paymentResponse.ok) {
                return paymentResponse.json();
            }

            const errorBody = await paymentResponse.text();
            throw new Error(errorBody);
        }

        // status is either SUCCESS or FAILURE;
        function redirectToPaymentResults(status, errorMessage) {
            let redirectUrl = '';
            if (status === 'SUCCESS') {
                redirectUrl = 'api/payment/close?action=success';
            } else {
                redirectUrl = 'api/payment/close?action=error&message='+errorMessage;
            }

            window.location.href = redirectUrl;
        }



        document.addEventListener('DOMContentLoaded', async function () {
            if (!window.Square) {
                throw new Error('Square.js failed to load properly');
            }

            let payments;
            try {
                payments = window.Square.payments(appId, locationId);
            } catch (e) {
                const statusContainer = document.getElementById(
                    'payment-status-container'
                );
                statusContainer.className = 'missing-credentials';
                statusContainer.style.visibility = 'visible';
                console.log(e.message);
                return;
            }

            let cashAppPay;
            try {
                cashAppPay = await initializeCashApp(payments);
            } catch (e) {
                console.error('Initializing Cash App Pay failed', e);
            }
            if (cashAppPay) {
                cashAppPay.addEventListener(
                    'ontokenization',
                    async function ({ detail }) {
                        await paymentFail(detail);
                        const tokenResult = detail.tokenResult;
                        if (tokenResult.status === 'OK') {
                            const paymentResults = await createPayment(tokenResult.token, tokenResult);
                            
                            if(paymentResults.status === 'SUCCESS') {
                               
                                const msg = paymentResults.message || '';
                                
                                redirectToPaymentResults('SUCCESS', '');
                            } else if(paymentResults.status === 'ERROR') {
                                // await paymentFail();
                                const msg = paymentResults.message || '';
                               
                                redirectToPaymentResults('FAILURE', msg);
                            }

                        } else {
                            // const paymentFail = await paymentFail();
                            let errorMessage = `Tokenization failed with status: ${tokenResult.status}`;

                            if (tokenResult.errors) {
                                errorMessage += ` and errors: ${JSON.stringify(
                                    tokenResult.errors
                                )}`;
                            }

                            redirectToPaymentResults('FAILURE', errorMessage);
                        }
                    }
                );
            }
        });
javascript square square-connect
1个回答
0
投票

什么是经过验证的现金应用程序帐户?

经过验证的 Cash App 帐户涉及用户通过提供姓名、地址和社会安全号码等个人信息来确认其身份的过程。验证可增强帐户安全性并允许访问更多功能。 了解验证的重要性 验证是 Cash App Account 实施的一项安全措施,用于验证用户身份。

如需了解更多详情请联系 24/7 客户支持

电报:@USASMMSTORE WhatsApp : +1 (913) 701-7893 Skype : 直播:.cid.32b76ba857700527 电子邮件 : ussmmstore@gmail com

ussmmstore com/product/buy-verified-cash-app-account/

© www.soinside.com 2019 - 2024. All rights reserved.