Paypal Sandbox交易/活动未显示在买方/商人仪表板上

时间:2018-10-16 10:53:58

标签: php laravel paypal-rest-sdk

我正在使用paypal/rest-api-sdk-php进行Paypal集成。

下面是我的PaymentController.php文件。金额一经确认即立即从买方转移到商户帐户,但交易/活动未显示在Paypal Sandbox买方或商户帐户的任何位置。我需要怎么做才能分别显示Paypal Sandbox买方和/或商户帐户上的交易/活动。谢谢!

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Session;
use Input;
use Illuminate\Support\Facades\Redirect;
use App\Model\Orders;
use App\Model\Booking;
use PayPal\Rest\ApiContext;
use PayPal\Auth\OAuthTokenCredential;
use PayPal\Api\Payer;
use PayPal\Api\Item;
use PayPal\Api\ItemList;
use PayPal\Api\Amount;
use PayPal\Api\Transaction;
use PayPal\Api\RedirectUrls;
use PayPal\Api\Payment;
use PayPal\Exception\PayPalConnectionException;

class PaymentController extends Controller {

    public function __construct() {
        $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 paypal_payment(Request $request) {
        $booking_no = $request->booking_no;
        $order_no = $request->order_no;
        $item_group_id = $request->item_group_id;

        $paypal_payment = url('/buy3/' . $item_group_id . '/' . $booking_no);

        $payer = new Payer();
        $payer->setPaymentMethod('paypal');
        $item_1 = new Item();
        $item_1->setName('Item 1') /** item name * */
                ->setCurrency($request->currency)
                ->setQuantity(1)
                ->setSku('testSKU')
                ->setDescription('Test description')
                ->setTax(0)
                ->setUrl('http://www.google.com/?q=abc')
                ->setPrice($request->get('amount'));/** unit price * */
        $item_list = new ItemList();
        $item_list->setItems(array($item_1));
        $amount = new Amount();
        $amount->setCurrency($request->currency)
                ->setTotal($request->get('amount'))
                ->setDetails(array(
                    'subtotal' => $request->get('amount'),
                    'tax' => 0,
                    'shipping' => 0
        ));
        $transaction = new Transaction();
        $transaction->setAmount($amount)
                ->setItemList($item_list)
                ->setDescription('Booking purchase description goes here.');
        $redirect_urls = new RedirectUrls();
        $redirect_urls->setReturnUrl(url('/paypal_status/' . $request->booking_no . '/' . $request->order_no)) /** Specify return URL * */
                ->setCancelUrl(url('/'));
        $payment = new Payment();
        $payment->setIntent('Sale')
                ->setPayer($payer)
                ->setRedirectUrls($redirect_urls)
                ->setTransactions(array($transaction));
//        dump($payment->create($this->_api_context));
//        exit;
        try {
            $payment->create($this->_api_context);
        } catch (\PayPal\Exception\PPConnectionException $ex) {
            if (\Config::get('app.debug')) {
                $request->session()->flash('message', 'Connection timedout');
                return redirect($paypal_payment);
            } else {
                $request->session()->flash('message', 'Some error occur, sorry for inconvenient');
                return redirect($paypal_payment);
            }
        }
        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);
        }
        $request->session()->flash('message', 'Unknown error occurred');
        return redirect($paypal_payment);
    }

    public function paypal_status($booking_no, $order_no, Request $request) {
        $booking = Booking::where("booking_no", $booking_no)->first();
        if ($booking == null) {
            return redirect("/");
        }
        $order = Orders::where("order_no", $order_no)->first();
        if ($order == null) {
            return redirect("/");
        }
        if ($order->order_status_id != '1') {
            return redirect("/");
        }
        $currency = \App\Model\Currency::where("id", $booking->total_amount_currency_id)->first();

        /** Get the payment ID before session clear * */
        $payment_id = Session::get('paypal_payment_id');
        /** clear the session payment ID * */
        Session::forget('paypal_payment_id');
        if (empty($request->PayerID) || empty($request->token)) {
            $request->session()->flash('message', 'Payment failed');
            return redirect('/buy3/' . $booking->item_group_id . '/' . $booking->booking_no);
        }

        $order->payment_method_id = 3;
        $order->payment_id = $payment_id;
        $order->save();
        return redirect('/paypal_success/' . $booking->booking_no . '/' . $booking->order_order_no);
    }

}

0 个答案:

没有答案