gpt4 book ai didi

php - 如何在使用 laravel 进行 PayPal 付款后插入数据

转载 作者:太空宇宙 更新时间:2023-11-03 16:21:04 24 4
gpt4 key购买 nike

我正在使用 paypal 进行支付,一切都是正确的,但是我想在支付成功后插入我的表用户,我的列“小时”中的数据并保存交易数据,我正在尝试传递根据计划,表单输入相应小时的值,但是在提出请求时不会带来该数据。我正在使用“paypal/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>

支付 Controller .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('/');
}
}

最佳答案

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

但是项目列表是个不错的选择

关于php - 如何在使用 laravel 进行 PayPal 付款后插入数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57882511/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com