Search code examples
phppaypalpaypal-sandbox

PAYPAL PHP SDK OrdersCaptureRequest error with statusCode 422


i use the latest v2 paypal php sdk sandbox and samples with laravel framework, the create order always success but when capture the order it always fails

and return this error:

"{"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"}]}

my web.php file :

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');

& the PayController :

<?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());
    }
}

i use paypal php sdk with a sandbox account.

can i use the old paypal version or it's completely deprecated ?

waiting your help :)


Solution

  • i call the support and the response was:

    Funds received into Egyptian accounts need to be automatically withdrawn to an attached funding source, such as a bank. You can set this up by going into the account settings section of the account, then the "money, banks and cards" section and at the bottom of that page you'll find "automatic withdrawal" where you can specify a financial instrument to use for automatic withdrawals -- https://www.sandbox.paypal.com/businessmanage/account/money

    thanks for u all :)