首页 > 解决方案 > PAYPAL PHP SDK OrdersCaptureRequest 错误,statusCode 422

问题描述

我使用最新的 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。

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

等待你的帮助:)

标签: phppaypalpaypal-sandbox

解决方案


COMPLIANCE_VIOLATION

此问题很可能与接收沙盒企业帐户的国家/地区有关。为其他国家/地区(例如美国)创建一个新帐户,然后创建一个使用该沙盒帐户的新 REST 应用程序,并在沙盒模式下测试其沙盒客户端 ID/秘密。

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


推荐阅读