为了账号安全,请及时绑定邮箱和手机立即绑定

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('sb-cwdzv2614448@business.example.com')            ->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条经验 获得超3个赞

付款用于将资金从您的帐户发送到另一个帐户。没有可显示或登录的表格。您是 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("seller-paypal-businness-account-email@business.example.com");

        $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 关注
  • 125 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信