I have two questions, first about the error, Laravel 8.20.1 + Paypal SDK v1.
App Routes:
Route::get('/agreement/execute', [PaymentController::class, 'agreementExecute']);
Route::post('/agreement/create', [PaymentController::class, 'subscribeMonthly']);
Admin Routes:
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:
Plan code:
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);
}
After I send data with the form
<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>
After succesful redirect to paypal with all data, i click accept (sandbox) and after that i get succesfull rerict back, redirect functions:
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);
}
And when $agreement->execute
runs I get errors with no detailed information.
Form subscribe function:
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);
}
Error is : PayPal\Exception\PayPalConnectionException Got Http response code 400 when accessing https://api.sandbox.paypal.com/v1/payments/billing-agreements/EC-1LE052463N345662M/agreement-execute. https://am.test/account/agreement?ba_token=BA-9X735270PX851462W&success=true&token=EC-1LE052463N345662M
I reviewed a lot of tutorials, re-read the code in PayPal guides several times. I'm new to this and can't figure out what the reason is, it just doesn't work for me. I do everything one to one. No subscriptions are created in the buyer account or by the administrator, everything is empty.
And the second question, Paypal writes that v1 is deprecated. How can I use the second version with a checkout v2 for subscriptions and where can I find detailed guides with the Laravel about this question. It's hard for me to fully understand API and code not created by myself, I create a big project on my own, but stuck a few days with that PayPal error. Thank you for reading so much of what I have written, I hope for your support.
The PayPal-PHP-SDK is deprecated, and not compatible with the current version of PayPal Subscriptions. You should not be using it for anything.
Instead, integrate directly with the necessary Product, Plan, and subscription management API calls described in that Subscriptions documentation. (Plus webhooks, if desired--to be notified of future subscription events)
You can also manage Products and Plans manually in the receiver account: