亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Laravel Paypal 付款。怎樣才能讓它完整呢?它返回 json 響應

Laravel Paypal 付款。怎樣才能讓它完整呢?它返回 json 響應

PHP
呼如林 2023-08-06 15:41:15
我已經使用他們的文檔創建了單批付款。這樣我就可以匯款給賣家。但我不知道之后我應該做什么。如何顯示付款表格,用戶可以在其中登錄 PayPal 并支付金額?這是我在控制器功能中的代碼?public function payViaPaypal(){? ?? ? ? ? $payouts = new \PayPal\Api\Payout();? ? ? ? $senderBatchHeader = new \PayPal\Api\PayoutSenderBatchHeader();? ? ? ? $senderBatchHeader->setSenderBatchId(uniqid('invoice-1qaqw23wdwdwew').microtime('true'))? ? ? ? ? ? ->setEmailSubject("You have a Payout!");? ? ? ? $senderItem = new \PayPal\Api\PayoutItem();? ? ? ? $senderItem->setRecipientType('Email')? ? ? ? ? ? ->setNote('Thanks for your patronage!')? ? ? ? ? ? ->setReceiver('[email protected]')? ? ? ? ? ? ->setSenderItemId(uniqid().microtime('true'))? ? ? ? ? ? ->setAmount(new \PayPal\Api\Currency('{? ? ? ? ? ? ? ? ? ? ? ? "value":"1.0",? ? ? ? ? ? ? ? ? ? ? ? "currency":"USD"? ? ? ? ? ? ? ? ? ? }'));? ? ? ? $payouts->setSenderBatchHeader($senderBatchHeader)? ? ? ? ? ? ->addItem($senderItem);? ? ? ? $request = clone $payouts;? ? ? ? $redirect_url = null;? ? ? ? try {? ? ? ? ? ? $output = $payouts->create(null, $this->api_context);? ? ? ? } catch (\Exception $e) {? ? ? ? ? ? dd('here',$this->errorDetails($e));? ? ? ? }//? ? ? ? dd("Created Single Synchronous Payout", "Payout", $output->getBatchHeader()->getPayoutBatchId(), $request, $output);? ? ? ? $redirect_url = null;? ? ? ? foreach($output->getLinks() as $link) {? ? ? ? ? ? if($link->getRel() == 'self') {? ? ? ? ? ? ? ? $redirect_url = $link->getHref();? ? ? ? ? ? ? ? break;? ? ? ? ? ? }? ? ? ? }? ? ? ? return $output;}當我點擊路由訪問此代碼時,我收到此 json 響應。{ "batch_header": { "payout_batch_id": "79CTFV2X5TS58", "batch_status": "PENDING", "sender_batch_header": { "sender_batch_id": "invoice-1qaqw23wdwdwew5f0f5003612091594839043.3978", "email_subject": "You have a Payout!" } }, "links": [ { "href": "https://api.sandbox.paypal.com/v1/payments/payouts/79CTFV2X5TS58", "rel": "self", "method": "GET", "enctype": "application/json" } ] }我希望用戶將被帶到 PayPal 付款頁面,用戶將在該頁面登錄并支付金額,然后 PayPal 會通知我有關付款的信息。但我試圖通過互聯網找到解決方案,但我找不到示例/解決方案。
查看完整描述

2 回答

?
守著一只汪

TA貢獻1872條經驗 獲得超4個贊

付款用于將資金從您的帳戶發送到另一個帳戶。沒有可顯示或登錄的表格。您是 API 調用者,系統會自動批準來自您帳戶的付款。


查看完整回答
反對 回復 2023-08-06
?
互換的青春

TA貢獻1797條經驗 獲得超6個贊

解決方案是創建一個付款對象并在其中添加收款人(將作為賣家接收付款)電子郵件地址。

需要兩個函數。

1 創建付款對象 2 使用 API 從 paypal 獲取付款詳細信息,然后執行此付款,以便將金額轉入收款賬戶。

以下需要 3 條路線

  1. 創建支付對象

  2. 獲取詳細付款對象詳細信息(查看客戶是否已使用 paypal 結賬支付金額)并執行付款以匯款(這是成功 url)

  3. 取消網址(當客戶取消付款時)。它將客戶重定向回平臺(網站)

這是完整的代碼示例

路線

create payment object

Route::get('/invoices/process-payment','Vendor\PayPalController@processPaymentInvoiceViaCheckout');


when payment object is created then get its details and execute payment to send money.

Route::get('/invoices/response-success','Vendor\PayPalController@paypalResponseSuccess');


when cancel to pay?

Route::get('/invoices/response-cancel','Vendor\PayPalController@paypalResponseCancel');


控制器


<?php


namespace App\Http\Controllers\Vendor;


use App\Http\Controllers\Controller;

use Illuminate\Http\Request;

use PayPal\Api\Amount;

use PayPal\Api\Details;

use PayPal\Api\Item;

use PayPal\Api\ItemList;

use PayPal\Api\Payee;

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\Exception\PayPalConnectionException;

use PayPal\Rest\ApiContext;

use PHPUnit\TextUI\ResultPrinter;


class PayPalController extends Controller


{

? ? private $api_context;


? ? public function __construct()

? ? {

? ? ? ? $this->api_context = new ApiContext(

? ? ? ? ? ? new OAuthTokenCredential(config('paypal.client_id'), config('paypal.secret'))

? ? ? ? );

? ? ? ? $this->api_context->setConfig(config('paypal.settings'));

? ? }

? ? public function processPaymentInvoiceViaCheckout(){

? ? ? ? $payer = new Payer();

? ? ? ? $payer->setPaymentMethod("paypal");


? ? ? ? $item1 = new Item();

? ? ? ? $item1->setName('Ground Coffee 40 oz')

? ? ? ? ? ? ->setCurrency('USD')

? ? ? ? ? ? ->setQuantity(1)

//? ? ? ? ? ? ->setSku("123123") // Similar to `item_number` in Classic API

? ? ? ? ? ? ->setPrice(7.5);

? ? ? ? $item2 = new Item();

? ? ? ? $item2->setName('Granola bars')

? ? ? ? ? ? ->setCurrency('USD')

? ? ? ? ? ? ->setQuantity(5)

//? ? ? ? ? ? ->setSku("321321") // Similar to `item_number` in Classic API

? ? ? ? ? ? ->setPrice(2);


? ? ? ? $itemList = new ItemList();

? ? ? ? $itemList->setItems(array($item1, $item2));

? ? ? ? $details = new Details();

? ? ? ? $details->setShipping(1.2)

? ? ? ? ? ? ->setTax(1.3)

? ? ? ? ? ? ->setSubtotal(17.50);

? ? ? ? $amount = new Amount();

? ? ? ? $amount->setCurrency("USD")

? ? ? ? ? ? ->setTotal(20)

? ? ? ? ? ? ->setDetails($details);

? ? ? ? $payee = new Payee();


? ? ? ? //this is the email id of the seller who will receive this amount


? ? ? ? $payee->setEmail("[email protected]");

? ? ? ? $transaction = new Transaction();

? ? ? ? $transaction->setAmount($amount)

? ? ? ? ? ? ->setItemList($itemList)

? ? ? ? ? ? ->setDescription("Payment description")

? ? ? ? ? ? ->setPayee($payee)

? ? ? ? ? ? ->setInvoiceNumber(uniqid());

? ? ? ? $redirectUrls = new RedirectUrls();

? ? ? ? $redirectUrls->setReturnUrl(url('/invoices/response-success'))

? ? ? ? ? ? ->setCancelUrl(url('/invoices/response-cancel'));

? ? ? ? $payment = new Payment();

? ? ? ? $payment->setIntent("sale")

? ? ? ? ? ? ->setPayer($payer)

? ? ? ? ? ? ->setRedirectUrls($redirectUrls)

? ? ? ? ? ? ->setTransactions(array($transaction));

? ? ? ? $request = clone $payment;

? ? ? ? try {

? ? ? ? ? ? //create payment object

? ? ? ? ? ? $createdPayment = $payment->create($this->api_context);

? ? ? ? ? ? //get payment details to get payer id so that payment can be executed and transferred to seller.

? ? ? ? ? ? $paymentDetails = Payment::get($createdPayment->getId(), $this->api_context);

? ? ? ? ? ? $execution = new PaymentExecution();

? ? ? ? ? ? $execution->setPayerId($paymentDetails->getPayer());

? ? ? ? ? ? $paymentResult = $paymentDetails->execute($execution,$this->api_context);

? ? ? ? } catch (\Exception $ex) {

? ? ? ? ? ? //handle exception here

? ? ? ? }

? ? ? ? //Get redirect url

? ? ? ? //The API response provides the url that you must redirect the buyer to. Retrieve the url from the $payment->getApprovalLink() method

? ? ? ? $approvalUrl = $payment->getApprovalLink();


? ? ? ? return redirect($approvalUrl);

? ? }


? ? public function paypalResponseCancel(Request $request)

? ? {


? ? ? ? //normally you will just redirect back customer to platform

? ? ? ? return redirect('invoices')->with('error','You can cancelled payment');

? ? }


? ? public function paypalResponseSuccess(Request $request)

? ? {

? ? ? ? if (empty($request->query('paymentId')) || empty($request->query('PayerID')) || empty($request->query('token'))){

? ? ? ? ? ? //payment was unsuccessful

? ? ? ? ? ? //send failure response to customer

? ? ? ? }

? ? ? ? $payment = Payment::get($request->query('paymentId'), $this->api_context);

? ? ? ? $execution = new PaymentExecution();

? ? ? ? $execution->setPayerId($request->query('PayerID'));


? ? ? ? // Then we execute the payment.

? ? ? ? $result = $payment->execute($execution, $this->api_context);



? ? ? ? dd($request->all(),$result);

? ? ? ?//payment is received,? send response to customer that payment is made.

? ? }

}

查看完整回答
反對 回復 2023-08-06
  • 2 回答
  • 0 關注
  • 154 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號