Laravel return url 上的 PayPal 协议执行错误

Laravel PayPal agreement-execute error on return url

我有两个问题,第一个是关于错误,Laravel 8.20.1 + Paypal SDK v1.

应用路线:

Route::get('/agreement/execute', [PaymentController::class, 'agreementExecute']);
Route::post('/agreement/create', [PaymentController::class, 'subscribeMonthly']);

管理路由:

Route::prefix('paypal')->name('plan.')->group(function () {
    Route::get('/monthly/create', [PrivatePaypal::class, 'createMonthly'])
    Route::get('/list', [PrivatePaypal::class, 'showPlans'])
    Route::get('/plan/{plan}', [PrivatePaypal::class, 'plan'])
    Route::get('/delete', [PrivatePaypal::class, 'deletePlan'])
});

已创建计划:

Created plan image

计划代码:

public function createMonthly() {
    $plan = new \PayPal\Api\Plan();
    $plan->setName('Monthly')
        ->setDescription('Activate partnership for one month.')
        ->setType('INFINITE'); // or FIXED: The plan has a fixed number of payment cycles

    $paymentDefinition = new \PayPal\Api\PaymentDefinition();
    $paymentDefinition->setName('Monthly Payments')
        ->setType('REGULAR') // or TRIAL
        ->setFrequency('Month') // or WEEK, DAY, YEAR, MONTH
        ->setFrequencyInterval("1") // The interval at which the customer is charged. Value cannot be greater than 12 months
        ->setAmount(new \PayPal\Api\Currency(array('value' => 20, 'currency' => 'USD')));
        // ->setCycles("12")

    $merchantPreferences = new \PayPal\Api\MerchantPreferences();
    $merchantPreferences->setReturnUrl(route('account.agreement',['success'=>'true'])) 
    ->setCancelUrl(route('account.agreement',['success'=>'false'])) 
    ->setAutoBillAmount("yes") 
    ->setInitialFailAmountAction("CONTINUE") 
    ->setMaxFailAttempts("0")
    ->setSetupFee(new \PayPal\Api\Currency(array('value' => config('settings.price_monthly'), 'currency' => 'USD'))) 
    ;

    $plan->setPaymentDefinitions(array($paymentDefinition));
    $plan->setMerchantPreferences($merchantPreferences);
    try {
        $createdPlan = $plan->create($this->apiContext);
    } catch(\Exception $ex) {
        print_r($ex->getMessage());
        die();
    }
    // dd($createdPlan);
    $this->activatePlan($createdPlan);
}

我用表单发送数据后

<form action="https://site.test/agreement/create" method="post">
    <input type="hidden" name="_token" value="XSM2gxx0Cqs5dlloYScQfl2GdeGqrz4lkWLfm42a">             
    <input type="hidden" name="_method" value="POST">
    <input type="hidden" name="id" value="P-0UV961714R317531UT5H72WI">
    <button class="button compact">Activate</button>
</form>

使用所有数据成功重定向到 paypal 后,我单击接受(沙盒),然后我成功返回,重定向功能:

 if (!empty($request->input('success')))
    {
        $success = $request->input('success');
        if ($success && !empty($request->input('token')))
        {
            $token = $request->input('token');
            $agreement = new \PayPal\Api\Agreement();

            
            try {
                // Execute agreement
                $agreement->execute($token, $this->apiContext);
              } catch (PayPal\Exception\PayPalConnectionException $ex) {
                    print_r($ex->getMessage());  
                    echo $ex->getCode();
                    echo $ex->getData();
                  print_r($ex->getData());
                die($ex);
              } catch (Exception $ex) {
                // die($ex);
              }

$agreement->execute 运行时,我得到没有详细信息的错误。

表单订阅功能:

public function subscribeMonthly(Request $request) {
     $id = $request->id;
     $agreement = new \PayPal\Api\Agreement();
     $agreement->setName('Monthly subscription')
     ->setDescription('Activate partnership for one month.') 
     ->setStartDate(Carbon::now()->addMonth()->toIso8601String());

     $plan = new \PayPal\Api\Plan();
     $plan->setId($id);
    $agreement->setPlan($plan);

    $payer = new \PayPal\Api\Payer();
    $payer->setPaymentMethod('paypal');
    $agreement->setPayer($payer);

    try {
        $agreement = $agreement->create($this->apiContext);
        $approvalUrl = $agreement->getApprovalLink();
    } catch(\Exception $ex) {
        print_r($ex->getMessage());
        die();
    }

    return redirect($approvalUrl);
        
 }

错误是:PayPal\Exception\PayPalConnectionException 访问 https://api.sandbox.paypal.com/v1/payments/billing-agreements/EC-1LE052463N345662M/agreement-execute 时得到 Http 响应代码 400。 https://am.test/account/agreement?ba_token=BA-9X735270PX851462W&success=true&token=EC-1LE052463N345662M

我看了很多教程,重读了几次PayPal指南中的代码。我对此很陌生,无法弄清楚原因是什么,它对我不起作用。我做每件事都是一对一的。 买家帐户或管理员均未创建任何订阅,一切都是空的。

第二个问题,Paypal 写道 v1 已弃用。我如何使用第二个版本和订阅的结帐 v2,我在哪里可以找到关于这个问题的 Laravel 的详细指南。 我很难完全理解 API 和不是我自己创建的代码,我自己创建了一个大项目,但由于 PayPal 错误而停滞了几天。感谢大家看完我写的这么多,希望大家多多支持

PayPal-PHP-SDK 已弃用,PayPal Subscriptions 的当前版本不兼容。您不应该将其用于任何用途。

相反,直接与该订阅文档中描述的必要产品、计划和订阅管理 API 调用集成。 (加上 webhooks,如果需要——在未来得到通知 subscription events

您还可以在接收方帐户中手动管理产品和计划: