【问题标题】:How to insert data after a PayPal payment with laravel如何在使用 laravel 进行 PayPal 付款后插入数据
【发布时间】:2020-01-12 21:40:14
【问题描述】:

我正在使用贝宝进行付款,一切都正确,但是我想在付款成功时在我的表用户中插入数据,并在“小时”列中保存交易数据,我正在尝试传入根据计划,表单输入具有相应小时数的值,但是在提出请求时不会带来该数据。我正在使用“贝宝/rest-api-sdk-php:*”

查看:

<form class="w3-container w3-display-middle w3-card-4 w3-padding-16" method="POST" id="payment-form"
      action="{!! URL::to('paypal') !!}">
    {{ csrf_field() }}
    <input class="w3-input w3-border" id="hours" type="text" name="hours" value="13" style="display: none;">
    <input class="w3-input w3-border" id="amount" type="text" name="amount" value="199" style="display: none;">
    <button href="#" class="btn btn-block btn-primary text-uppercase">Pagar con Paypal</button>
</form>

PaymentController.php

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Input;
use PayPal\Api\Amount;
use PayPal\Api\Details;
use PayPal\Api\Item;
/** All Paypal Details class **/
use PayPal\Api\ItemList;
use PayPal\Api\Payer;
use PayPal\Api\Payment;
use PayPal\Api\PaymentExecution;
use PayPal\Api\RedirectUrls;
use PayPal\Api\Transaction;
use PayPal\Auth\OAuthTokenCredential;
use PayPal\Rest\ApiContext;
use Redirect;
use Session;
use URL;
use App\User;
class PaymentController extends Controller
{
    private $_api_context;
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        /** PayPal api context **/
        $paypal_conf = \Config::get('paypal');
        $this->_api_context = new ApiContext(new OAuthTokenCredential(
                $paypal_conf['client_id'],
                $paypal_conf['secret'])
        );
        $this->_api_context->setConfig($paypal_conf['settings']);
    }
    public function index()
    {
        return view('paywithpaypal');
    }
    public function payWithpaypal(Request $request)
    {

        $payer = new Payer();
        $payer->setPaymentMethod('paypal');
        $item_1 = new Item();
        $item_1->setName('Item 1') /** item name **/
        ->setCurrency('USD')
            ->setQuantity(1)
            ->setPrice($request->get('amount')); /** unit price **/
        $item_list = new ItemList();
        $item_list->setItems(array($item_1));
        $amount = new Amount();
        $amount->setCurrency('USD')
            ->setTotal($request->get('amount'));
        $transaction = new Transaction();
        $transaction->setAmount($amount)
            ->setItemList($item_list)
            ->setDescription('Your transaction description');
        $redirect_urls = new RedirectUrls();
        $redirect_urls->setReturnUrl(URL::to('status')) /** Specify return URL **/
        ->setCancelUrl(URL::to('status'));
        $payment = new Payment();
        $payment->setIntent('Sale')
            ->setPayer($payer)
            ->setRedirectUrls($redirect_urls)
            ->setTransactions(array($transaction));
        /** dd($payment->create($this->_api_context));exit; **/
        try {
            $payment->create($this->_api_context);
        } catch (\PayPal\Exception\PPConnectionException $ex) {
            if (\Config::get('app.debug')) {
                \Session::put('error', 'Connection timeout');
                return Redirect::to('/');
            } else {
                \Session::put('error', 'Some error occur, sorry for inconvenient');
                return Redirect::to('/');
            }
        }
        foreach ($payment->getLinks() as $link) {
            if ($link->getRel() == 'approval_url') {
                $redirect_url = $link->getHref();
                break;
            }
        }
        /** add payment ID to session **/
        Session::put('paypal_payment_id', $payment->getId());
        if (isset($redirect_url)) {
            /** redirect to paypal **/
            return Redirect::away($redirect_url);
        }
        \Session::put('error', 'Unknown error occurred');
        return Redirect::to('/');
    }
    public function getPaymentStatus(Request $request)
    {

        **$hours = $request->get('hours');
        dd($hours);
        //output NULL**

        /** Get the payment ID before session clear **/
        $payment_id = Session::get('paypal_payment_id');
        /** clear the session payment ID **/
        Session::forget('paypal_payment_id');
        if (empty(Input::get('PayerID')) || empty(Input::get('token'))) {
            \Session::put('error', 'Payment failed');
            return Redirect::to('/');
        }
        $payment = Payment::get($payment_id, $this->_api_context);
        $execution = new PaymentExecution();
        $execution->setPayerId(Input::get('PayerID'));
        /**Execute the payment **/
        $result = $payment->execute($execution, $this->_api_context);
        if ($result->getState() == 'approved') {
            **/* HERE I WANT TO INSERT WHEN PAYMENT IS APPROVED */**
//            \Session::put('success', 'Payment success, hours added to your account');
//            return Redirect::to('/planes');
        }
        \Session::put('error', 'Payment failed');
        return Redirect::to('/');
    }
}

【问题讨论】:

  • 我猜payWithpaypal() 是处理 POST 到URL::to('paypal') 的方法,而来自 Paypal 的 pingback/webhook 会命中您的 getPaymentStatus() 方法?如果是,那么 getPaymentStatus() 中的 $request 来自 Paypal,而不是您的表单,并且它不会包含您在表单中添加的字段。我认为更好的方法是在payWithpaypal() 的数据库中创建一个占位符记录,将status 之类的字段设置为pending,并将hours 值保存在那里。然后在getPaymentStatus() 中检索该记录,并更新status

标签: php laravel paypal eloquent paypal-rest-sdk


【解决方案1】:

您可以使用会话来存储该数据或本地存储,也可以将其保存在项目列表中
或者你可以做的是将时间视为产品并使用购物车系统

但项目列表是不错的选择

【讨论】:

    猜你喜欢
    • 2014-01-11
    • 1970-01-01
    • 2011-04-07
    • 2016-03-14
    • 2016-02-22
    • 2019-12-26
    • 2020-08-03
    • 2013-11-01
    • 2018-06-16
    相关资源
    最近更新 更多