Laravel Guest-checkout is not working in paypal using "paypal/rest-api-sdk-php": "*&q

Posted

技术标签:

【中文标题】Laravel Guest-checkout is not working in paypal using "paypal/rest-api-sdk-php": "*" this package also success response is not getting on it【英文标题】:Laravel Guest-checkout is not Working in paypal using "paypal/rest-api-sdk-php": "*" this package also success response is not getting on it 【发布时间】:2021-08-30 02:26:26 【问题描述】:
namespace App\Http\Controllers;

use Exception;
use Illuminate\Http\Request;
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;


class PayPalController extends Controller

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    private $_api_context;
    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 getPaymentStatus(Request $request)
    
        try 
            $payment_id = Session::get('paypal_payment_id');

            Session::forget('paypal_payment_id');
            if (empty($request->input('PayerID')) || empty($request->input('token'))) 
                \Session::put('error', 'Payment failed');
                dd($request->all());
                return Redirect::route('paywithpaypal');
            
            $payment = Payment::get($payment_id, $this->_api_context);
            $execution = new PaymentExecution();
            $execution->setPayerId($request->input('PayerID'));
            $result = $payment->execute($execution, $this->_api_context);

            if ($result->getState() == 'approved') 
                dd("Payment Success");
                \Session::put('success', 'Payment success !!');
                return Redirect::route('paywithpaypal');
            
           
            \Session::put('error', 'Payment failed !!');
            return Redirect::route('paywithpaypal');
         catch (Exception $e) 
            dd($e);
        
    


   
    public function postPaymentWithpaypal(Request $request)
    
        

        $payer = new Payer();
        $payer->setPaymentMethod('paypal');

        $item_1 = new Item();

        $item_1->setName('Item 1')
            /** item name **/
            ->setCurrency('INR')
            ->setQuantity(1)
            ->setPrice('50');
        /** unit price **/

        $item_list = new ItemList();
        $item_list->setItems(array($item_1));

        $amount = new Amount();
        $amount->setCurrency('INR')
            ->setTotal('50');

        $transaction = new Transaction();
        $transaction->setAmount($amount)
            ->setItemList($item_list)
            ->setDescription('Your transaction description');

        $redirect_urls = new RedirectUrls();
        $redirect_urls->setReturnUrl(URL::to('payment_status'))
            /** Specify return URL **/
            ->setCancelUrl(URL::to('payment_status'));

        $payment = new Payment();
        $payment->setIntent('Sale')
            ->setPayer($payer)
            ->setRedirectUrls($redirect_urls)
            ->setTransactions(array($transaction));
        
        try 

            $payment->create($this->_api_context);
         catch (Exception $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('/');
    


以上代码是整合paypal整合的,请帮我解决 作为代码显示,我已经添加了所有需要的包,使用了所有类,还添加了代码来集成,但是由于一些问题,paypal scrren 显示在输出端,但成功消息没有出现,我无法处理它

【问题讨论】:

【参考方案1】:

该 SDK 已弃用,请勿使用。

使用当前的 SDK,在您的服务器上创建两条路由,一条用于“创建订单”,一条用于“捕获订单”,documented here。这些路由应该只返回 JSON 数据(没有 html 或文本)。后者应该(成功时)在返回之前将付款详细信息存储在您的数据库中(特别是purchase_units[0].payments.captures[0].id,PayPal 交易 ID)

将这两条路线与以下批准流程配对:https://developer.paypal.com/demo/checkout/#/pattern/server

【讨论】:

以上是关于Laravel Guest-checkout is not working in paypal using "paypal/rest-api-sdk-php": "*&q的主要内容,如果未能解决你的问题,请参考以下文章

What tmd is Laravel

Uncaught ReferenceError: $ is not defined - Laravel 5.5 + Laravel Mix

Laravel 5.1 报错:[AppHttpRequestsRequest] is not instantiable

@if (request::is("/")) 不起作用 laravel

laravel项目报错DecryptException:The MAC is invalid.

如何在 Laravel 查询中执行 IS NOT NULL