PayPal订阅PHP Laravel

发布于 2025-01-25 05:59:16 字数 6195 浏览 1 评论 0原文

我创建一个计划,一切都是正确的,我进入贝宝平台并付费,一切都很好,它将我重定向到我的网站,到目前为止,一切都正确,但是当我尝试在Paypal面板中看到时,它不会向我展示订阅或计划以及当我查询API时是否向我展示了计划。为什么?

我的计划创建功能

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

到目前为止, 都是正确的。 然后,当PayPal将我重定向到我的网站时,我会运行另一个功能。

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

一切对我来说都很好,但是当我尝试检查贝宝面时,如果有订阅或计划时,它不会向我展示任何东西 但是,如果我查询API,则制定计划并活跃

 public function getplans()
{
    $params = array('page_size' => '10','status' => 'ACTIVE');
   return $planList = Plan::all($params, $this->apiContext);

}

帮助我

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文