首页 > 解决方案 > 管理员通过 paypal laravel 向卖家付款

问题描述

我正在实施一项功能,管理员通过他们的 PAYPAL 电子邮件帐户向卖家付款。但由于某种原因,我在实现以下代码时遇到了这个错误。我相信函数processPaymentInvoiceViaCheckout返回时会出现错误redirect($approvalUrl)

传递给 Symfony\Component\HttpFoundation\Response::setContent() 的参数 1 必须是 string 或 null 类型,给定对象,调用

路线

//create payment object
Route::get('/invoices/process-payment','PaypalController@processPaymentInvoiceViaCheckout');

//when payment object is created then get its details and execute payment to send money.
Route::get('/invoices/response-success','PaypalController@paypalResponseSuccess');

//when cancel to pay
Route::get('/invoices/response-cancel','PaypalController@paypalResponseCancel');

控制器

<?php

namespace App\Http\Controllers;

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.
    }
}

标签: phplaravelpaypal

解决方案


推荐阅读