'PayPal subscription PHP Laravel
I create a plan and everything is correct, I enter the PayPal platform and pay and everything is fine, it redirects me to my website and everything is correct so far, but when I try to see in my PayPal panel it does not show me the subscriptions or the plans and when I query the API if it shows me the plans. Why?
My plan creation function
public function subscriptionpackage($package, $request)
{
$plan = new Plan();
$plan->setName($package['name'])
->setDescription('subscription')
->setType('fixed');
$paymentDefinition = new PaymentDefinition();
$paymentDefinition->setName('Regular Payments')
->setType('REGULAR')
->setFrequency('Month')
->setFrequencyInterval("1")
->setCycles("12")
->setAmount(new Currency(array('value' => $package['price'], 'currency' => 'USD')));
$chargeModel = new ChargeModel();
$chargeModel->setType('SHIPPING')
->setAmount(new Currency(array('value' => 0, 'currency' => 'USD')));
$paymentDefinition->setChargeModels(array($chargeModel));
$merchantPreferences = new MerchantPreferences();
$devices = json_decode($request['devices'], true);
$newpayment = PaymentHistory::create([
'user_id' => auth()->id(),
'package_id' => $package['id'],
'name' => $package['name'],
'amount' => $package['price'],
'payment_id' => null,
'payer_id' => null,
'devices' => json_encode($devices),
'status' => 'Pending',
]);
$callbackUrl = route('paypal.suscription.status', ['payment' => $newpayment['id']]);
$merchantPreferences->setReturnUrl($callbackUrl)
->setCancelUrl($callbackUrl)
->setAutoBillAmount("yes")
->setInitialFailAmountAction("CONTINUE")
->setMaxFailAttempts("0")
->setSetupFee(new Currency(array('value' => $package['price'], 'currency' => 'USD')));
$plan->setPaymentDefinitions(array($paymentDefinition));
$plan->setMerchantPreferences($merchantPreferences);
try {
$plan->create($this->apiContext);
$patch = new Patch();
$value = new PayPalModel('{"state":"ACTIVE"}');
$patch->setOp('replace')->setPath('/')->setValue($value);
$patchRequest = new PatchRequest();
$patchRequest->addPatch($patch);
$plan->update($patchRequest, $this->apiContext);
$patchplan = Plan::get($plan->getId(), $this->apiContext);
$time = time() + 30 * (24 * 3600);
$startTime = date('Y-m-d\\TH:i:s\\Z', $time);
$agreement = new Agreement();
$agreement->setName($package['name'])
->setDescription('subscription')
->setStartDate($startTime);
// Add Plan ID
$plan = new Plan();
$plan->setId($patchplan->getId());
$agreement->setPlan($plan);
// Add Payer
$payer = new Payer();
$payer->setPaymentMethod('paypal');
$agreement->setPayer($payer);
$agreement = $agreement->create($this->apiContext);
return redirect()->away($agreement->getApprovalLink());
} catch (PayPalConnectionException $ex) {
echo $ex->getData();
}
}
So far everything is correct. Then when PayPal redirects me to my site I run another function.
public function suscriptionstatus($payment, Request $request)
{
$token = $request->input('token');
$agreement = new Agreement();
$result = $agreement->execute($token, $this->apiContext);
$payerId = $result->getPayer()->getPayerInfo()->getPayerId();
$paymentId = $result->getId();
$getpayment = PaymentHistory::find($payment);
if (!$token) {
$updatepayment = $getpayment->update([
'payment_id' => $paymentId,
'transaction_id' => null,
'payer_id' => $payerId,
'status' => 'failed',
]);
return redirect()->route('paypal.payment.failed');
}
if ($result->getState() === 'Active') {
$transactionid = $result->getId();
$updatepayment = $getpayment->update([
'payment_id' => $paymentId,
'transaction_id' => $transactionid,
'payer_id' => $payerId,
'status' => 'approved',
]);
$getdatapackage = PackagePlan::find($getpayment['package_id']);
$getuserdata = User::find($getpayment['user_id']);
$data = array(
'name' => $getdatapackage['name'],
'price' => $getdatapackage['price'],
'validity' => $getdatapackage['validity'],
'customer' => $getuserdata['email'],
'order' => $getpayment['id'],
'date' => Carbon::parse($getpayment['created_at'])->format('F d, Y'),
'transaction_id' => $transactionid,
);
Mail::to(env('EMAIL_CONTACT'))->queue(new NewPayment($data));
Mail::to($getuserdata['email'])->queue(new BillMail($data));
$devices = ($getpayment['devices']) ? json_decode($getpayment['devices'], true) : [];
foreach ($devices as $key => $dv) {
$gettemplate = template::where('device_id', '=', $dv)->first();
$datatemplate = array(
'content' => $gettemplate['content'],
'date' => Carbon::parse($getpayment['created_at'])->format('F d, Y'),
);
Mail::to($getuserdata['email'])->queue(new TempMail($datatemplate));
}
return redirect()->route('paypal.payment.success', ['transactionid' => $transactionid]);
}
$updatepayment = $getpayment->update([
'payment_id' => $paymentId,
'transaction_id' => null,
'payer_id' => $payerId,
'status' => 'failed',
]);
return redirect()->route('paypal.payment.failed');
}
Everything runs fine for me, but when I try to check in the PayPal panel if there is a subscription or plans, it doesn't show me anything But if I query the API the plans are created and active
public function getplans()
{
$params = array('page_size' => '10','status' => 'ACTIVE');
return $planList = Plan::all($params, $this->apiContext);
}
HELP ME
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|