【问题标题】:PAYPAL PHP SDK OrdersCaptureRequest error with statusCode 422PAYPAL PHP SDK OrdersCaptureRequest 错误,statusCode 422
【发布时间】:2021-11-24 12:12:50
【问题描述】:

我使用最新的 v2 paypal php sdk 沙箱和带有 laravel 框架的示例,创建订单总是成功但捕获订单时总是失败

并返回此错误:

"{"name":"UNPROCESSABLE_ENTITY",
"details":[
{"issue":"COMPLIANCE_VIOLATION",
"description":"Transaction cannot be processed due to a possible compliance violation. To get more information about the transaction, call Customer Support."}],
"message":"The requested action could not be performed, semantically incorrect, or failed business validation.",
"debug_id":"d701744348160",
"links":[{"href":"https://developer.paypal.com/docs/api/orders/v2/#error-COMPLIANCE_VIOLATION","rel":"information_link","method":"GET"}]}

我的 web.php 文件:

Route::get('capture_order', 'PayController@capture_order')->name('capture_order');
Route::get('create_order', 'PayController@create_order')->name('create_order');
Route::get('cancel', 'PayController@cancel')->name('payment.cancel');
Route::get('return', 'PayController@return')->name('payment.return');
Route::get('success', 'PayController@success')->name('payment.success');

& 支付控制器:

<?php 

namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
use PayPalCheckoutSdk\Core\PayPalHttpClient;
use PayPalCheckoutSdk\Core\SandboxEnvironment;
use PayPalCheckoutSdk\Orders\OrdersCreateRequest;
use PayPalCheckoutSdk\Orders\OrdersCaptureRequest;

class PayController extends Controller
{
    public $clientId;
    public $clientSecret;
    public $client;
    public $cancel_url = 'http://localhost:8000/cancel';
    public $return_url = 'http://localhost:8000/return';

    public function __construct()
    {
        $mode = config('paypal.mode', 'sandbox');
        if ($mode == "live")  {
            $this->clientId     = config('paypal.live.client_id');
            $this->clientSecret = config('paypal.live.client_secret');
        } else {
            $this->clientId     = config('paypal.sandbox.client_id');
            $this->clientSecret = config('paypal.sandbox.client_secret');
        }

        $environment = new SandboxEnvironment($this->clientId, $this->clientSecret);
        $this->client = new PayPalHttpClient($environment);
    }


    private function buildRequestBody()
    {
        return [
            'intent' => 'CAPTURE',
            'application_context' =>[ "cancel_url" => $this->cancel_url,
                                       "return_url" => $this->return_url],
    
            'purchase_units' =>
                [
                    0 => [
                            'amount' =>
                                [
                                    'currency_code' => 'USD',
                                    'value' => '20'
                                ]
                        ]
                ]
            ];
    }


    public function create_order()
    {
        $request = new OrdersCreateRequest();
        $request->prefer('return=representation');
        $request->body = $this->buildRequestBody();

        try {
            $response = $this->client->execute($request);
            foreach ($response->result->links as $key => $value) {
                if ($value->rel == "approve") 
                {
                    return redirect($value->href);
                }
            }
        }catch (\Exception $ex) {
            echo $ex->statusCode;
            print_r($ex->getMessage());
        }

    }

    public function capture_order(Request $request)
    {
        // if ($request->token) {
            $request = new OrdersCaptureRequest($request->token);
            $request->prefer('return=representation');
            try {
                $response = $this->client->execute($request);
            }catch (\Exception $ex) {
                echo $ex->statusCode;
                dd($ex->getMessage());
            }
        // }
     
    }

   
   
    /**
     * Responds with a welcome message with instructions
     *
     * @return \Illuminate\Http\Response
     */
    public function cancel(Request $request)
    {
        dump($request->all());
        dd('Your payment is canceled. You can create cancel page here.');
    }
  
 

    /**
     * Responds with a welcome message with instructions
     *
     * @return \Illuminate\Http\Response
     */
    public function return(Request $request)
    {
        if ($request->token) {
            return redirect()->route('capture_order', [
                'token' => $request->token,
                'PayerID' => $request->PayerID
            ]);
        }
        
        dd($request->all());
    }
}

我使用带有沙盒帐户的 paypal php sdk。

我可以使用旧的 paypal 版本还是完全弃用?

等待您的帮助 :)

【问题讨论】:

    标签: php paypal paypal-sandbox


    【解决方案1】:

    COMPLIANCE_VIOLATION

    此问题很可能与接收沙盒企业帐户的国家/地区有关。 Create a new account 用于其他国家/地区(例如美国),然后创建使用该沙盒帐户的 a new REST app,并改为在沙盒模式下测试其沙盒 clientid/secret。

    为了以后在实时模式下使用,请确保如果实时业务帐户来自countries that require it 之一,则该实时帐户具有有效的自动扫描提款方法并在该帐户上启用并启用,例如美国银行或当地签证卡。如果您需要帮助配置实时模式的自动扫描,请联系 PayPal 的业务支持

    【讨论】:

    • 我在美国国家/地区使用了不同的沙盒企业帐户,并在此帐户中使用了不同的应用程序,但响应相同。
    • 旧的php sdk可以用了吗?
    • 我不相信你做出了正确的改变。使用新帐户收到的回复更新您的问题。
    • 另外,不推荐使用的PHP SDK不应该用于任何事情,并且与正在使用的帐户和应用程序无关。
    【解决方案2】:

    我致电支持,得到的答复是:

    埃及账户收到的资金需要自动提取到附加的资金来源,例如银行。您可以通过进入帐户的帐户设置部分,然后进入“钱、银行和卡”部分并在在该页面的底部,您会找到“自动提款”,您可以在其中指定用于自动提款的金融工具 -- https://www.sandbox.paypal.com/businessmanage/account/money

    谢谢大家:)

    【讨论】:

    • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center。
    猜你喜欢
    • 2011-04-19
    • 2013-09-10
    • 2016-07-05
    • 2021-07-05
    • 2016-10-23
    • 2015-02-13
    • 2021-10-03
    • 2016-08-10
    • 1970-01-01
    相关资源
    最近更新 更多