当前位置: 首页 > 知识库问答 >
问题:

使用PayPal PHP SDK在PayPal沙箱上执行支付时获得Http响应代码400

逑景铄
2023-03-14
PayPal\Exception\PayPalConnectionException
Got Http response code 400 when accessing https://api.sandbox.paypal.com/v1/payments/payment/PAYID-L7KLVNA5VA13973Y0247405F/execute.

我的灵感来自:https://github.com/evolutednewmedia/paypal-rest-api-example/blob/master/src/response.php

我也尝试过:https://paypal.github.io/paypal-php-sdk/sample/doc/payments/executepayment.html

下面是我返回URL的代码:

public function actionResponse($paymentId, $PayerID)
    {
        if( $paypalPayment = PaypalPayment::find()->where(['transaction_id' => $paymentId])->one() ) {
            $order = $paypalPayment->getOrder()->one();
        }else{
            die('Can not find PayPal reference in DB');
        }

        $apiContext = $this->getApiContext(self::OMT_PAYPAL_CLIENT_ID, self::OMT_PAYPAL_CLIENT_SECRET, self::OMT_PAYPAL_SANDBOX);

        $amount = new Amount();
        $amount->setCurrency($order->currency);
        $amount->setTotal($order->amount/100);

        $transaction = new Transaction();
        $transaction->setAmount($amount);


        $execution = new PaymentExecution();
        $execution->setPayerId($PayerID);
        $execution->addTransaction($transaction);

        //die($execution);

        $payment = Payment::get($paymentId, $apiContext);

        try {
            $payment->execute($execution, $apiContext);

            try {

                $paypalPayment = PaypalPayment::find()->where(['transaction_id' => $payment->getId()])->one();
                $paypalPayment->payment_amount =  $payment->transactions[0]->amount->total;
                $paypalPayment->payment_status =  $payment->getState();
                $paypalPayment->invoice_id =  $payment->transactions[0]->invoice_number;

                if ($paypalPayment->save() && $paypalPayment->payment_status === 'approved') {
                    // Payment successfully added, redirect to the payment complete page.
                    echo 'success';
                } else {
                    // Payment failed
                    echo 'Payment failed';

                }

            } catch (Exception $ex) {
                // Failed to retrieve payment from PayPal
                die('Failed to retrieve payment from PayPal');
                echo $ex->getCode();
                echo $ex->getData();
                die($ex);

            }

        } catch (Exception $ex) {
            // Failed to take payment
            die('Failed to take payment from PayPal');
            echo $ex->getCode();
            echo $ex->getData();
            die($ex);

        }
    }

{
    "id": "PAYID-L7KNALA80B388166U211654T",
    "intent": "sale",
    "state": "failed",
    "cart": "3AY17068NG554090V",
    "payer": {
        "payment_method": "paypal",
        "status": "VERIFIED",
        "payer_info": {
            "email": "sb-mbri64023415@business.example.com",
            "first_name": "John",
            "last_name": "Doe",
            "payer_id": "MWYZL3GMGV86S",
            "shipping_address": {
                "recipient_name": "John Doe",
                "line1": "Rue du Cornet 6",
                "city": "Verviers",
                "state": "BE_zip = 4800",
                "postal_code": "4800",
                "country_code": "BE"
            },
            "country_code": "BE",
            "business_name": "John Doe's Test Store"
        }
    },
    "transactions": [
        {
            "amount": {
                "total": "5880.60",
                "currency": "EUR",
                "details": {
                    "subtotal": "5880.60",
                    "tax": "0.00",
                    "shipping": "0.00"
                }
            },
            "payee": {
                "merchant_id": "AEVEQ26UKMLZL",
                "email": "XXXX"
            },
            "description": "XXXX",
            "custom": "b1900ec71f8694995925602422215428",
            "invoice_number": "b1900ec71f8694995925602422215428",
            "item_list": {
                "shipping_address": {
                    "recipient_name": "John Doe",
                    "line1": "Rue du Cornet 6",
                    "city": "Verviers",
                    "state": "BE_zip = 4800",
                    "postal_code": "4800",
                    "country_code": "BE"
                }
            },
            "related_resources": []
        }
    ],
    "create_time": "2020-12-12T14:14:04Z",
    "update_time": "2020-12-12T14:17:17Z",
    "links": [
        {
            "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAYID-L7KNALA80B388166U211654T",
            "rel": "self",
            "method": "GET"
        },
        {
            "href": "https://api.sandbox.paypal.com/v1/payments/payment/PAYID-L7KNALA80B388166U211654T/execute",
            "rel": "execute",
            "method": "POST"
        },
        {
            "href": "https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-3AY17068NG554090V",
            "rel": "approval_url",
            "method": "REDIRECT"
        }
    ]
}

谢谢:)克莱姆

共有1个答案

令狐晟
2023-03-14

不推荐使用V1/Payments PayPal-PHP-SDK。

使用v2 Checkout-PHP-SDK,在这里进行了说明:https://developer.paypal.com/docs/business/checkout/server-side-api-calls/

您需要两条路由,一条用于“创建订单”,另一条用于“捕获订单”。

 类似资料:
  • 如何使用 PHP 自动PayPal付款? 我有我的PayPal企业帐户的电子邮件,收件人的PayPal电子邮件和要转移的金额。有没有办法在没有我互动的情况下进行汇款?(付款来自我的帐户) 当然我会手动运行PHP脚本

  • 本文向大家介绍SpringBoot集成支付宝沙箱支付(支付、退款),包括了SpringBoot集成支付宝沙箱支付(支付、退款)的使用技巧和注意事项,需要的朋友参考一下 前言 支付宝推出一个沙箱环境,能够很好的模拟支付宝支付,并且还提供了demo,但demo是一个普通web项目,怎么整合到Spring Boot项目呢,其实很简单 简单配置请参照支付宝沙箱支付开发文档 一、支付部分 AlipayCon

  • 我正在学习有关如何使用PayPal-PHP-SDK的教程,我被困在控制台中的某个地方出现此错误: 更新 尝试更新函数名称后,日志中的新消息是: 这是代码: 任何方向,问题,以改善同一问题,评论,建议或要求澄清/更多信息,等等[...如能帮助解决这个问题,我将不胜感激。

  • 我正在尝试将Papal与我的SpringWeb服务集成。我指的是高级服务器集成,并使用此SDK创建支付示例。 这是我的客户端代码 这是我的网络服务 当我点击Paypal结账按钮时。它弹出Paypal登录屏幕,授权工作正常。之后,我尝试使用我的API执行付款。但是当我呼叫

  • 在沙盒上测试快速结账时,买家无法找到信用卡的付款方式。 在页面的某个地方,我们找到了以下信息。 很抱歉,您的某些付款方式不适用于此次购买。 无法使用的付款方式: 信用卡、借记卡或即时银行转账 卖家帐户已启用为商务专业版。 我们是否缺少买方或卖方帐户的任何配置?

  • 我们在PayPal沙箱上测试我们的应用程序。我能够通过一个账户(ewa)付款。tkacz@zoho.com)另一个(ewa.tkacz)-facilitator@mmigroup.pl),并且此付款的状态在付款人帐户(付款ID 3F335538TV000622E)上已完成,但在收款人业务帐户上,我无法看到此付款,也无法通过API获取它。 这个问题是贝宝的,正如你在Stackoverflow论坛上建