【发布时间】:2021-08-14 00:01:04
【问题描述】:
我有两个问题,首先是关于错误,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'])
});
创建的计划:
计划代码:
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>
使用所有数据成功重定向到贝宝后,我点击接受(沙盒),然后我成功返回,重定向功能:
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 错误而卡住了几天。感谢您阅读我写的这么多内容,希望您的支持。
【问题讨论】:
-
确保 DEBUG 标志打开,并写入日志。这通常有助于指出实际问题是什么。我只是试图从 v1 SDK 迁移到 Checkout SDK,但由于 Checkout 仍然使用 v1 SDK,所以我决定坚持使用我的原始代码。
-
谢谢,忘记查看日志,有 somw 堆栈跟踪和 [2021-05-25 17:50:08] local.ERROR: Got Http response code 400 when access api.sandbox.paypal.com/v1/payments/billing-agreements/… . {"userId":34,"exception":"[object] (PayPal\\Exception\\PayPalConnectionException(code: 400): 访问api.sandbox.paypal.com/v1/payments/billing-agreements/…时得到 Http 响应代码 400。在 C:\\laragon\\www\ \am\\vendor\\paypal\est-api-sdk-php\\lib\\PayPal\\Core\\PayPalHttpConnection.php:207)
-
查看是否有单独的 PayPal 日志。我在我的配置中设置了一个,它在创建时传递给 ApiContext。
'log.LogEnabled'=>true, 'log.FileName' => storage_path() . '/logs/paypal_'.date('Y-m-d').'.log', 'log.LogLevel' => 'DEBUG', -
非常感谢,这是个好主意!
标签: php laravel paypal paypal-subscriptions