贝宝快递结帐回调

时间:2020-02-03 12:17:47

标签: php laravel rest paypal e-commerce

我正在尝试在Laravel 6中进行Paypal Express Checkout集成。

我收到付款后的付款URL,但没有收到与之前生成的付款ID相同的回叫。我需要知道付款是否成功会减少产品数量。另外,我需要知道哪个人付款。

我的问题是payment_id和回调payment id不同。

    <?php
    namespace App\Http\Controllers;

    use App\Paypal;
    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 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)
    {
        $price = 100;
        $payer = new Payer();
        $payer->setPaymentMethod('paypal');

        $item_1 = new Item();

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

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

        $amount = new Amount();
        $amount->setCurrency('USD')
            ->setTotal($price);

        $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());
        $paypal = new Paypal();
        $paypal->payment_id = $payment->getId();
        $paypal->save();
        if (isset($redirect_url)) {

            /** redirect to paypal **/
            return Redirect::away($redirect_url);

        }

        \Session::put('error', 'Unknown error occurred');
        return Redirect::to('/');

    }



    public function callback(Request $request){
        $data = json_encode($request->all());
        $resource = $request->resource;
        $event_type = $request->event_type;
        $paypal = Paypal::where('payment_id','=',$resource["id"]);
        $paypal->test = json_encode($resource);
        $paypal->event_type = $event_type;
        $paypal->save();
    }

}

0 个答案:

没有答案