Paypal付款失败

时间:2019-07-21 05:51:58

标签: laravel paypal

我在我的电子商务网站中使用Paypal付款网关集成,但每次都无法付款。我遇到错误,错误为“返回商家并尝试其他付款方式”,请解决。我正在添加沙箱帐户的客户ID和秘密密钥。我

控制器:  公共功能__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('paypalnew');
}
public function payWithpaypal(Request $request)
{

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

    $item_1 = new Item();

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

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

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

    $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());

    if (isset($redirect_url)) {

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

    }

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

}

public function getPaymentStatus()
{
    /** 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(Input::get('PayerID')) || empty(Input::get('token'))) {

        \Session::put('error', 'Payment failed');
        return Redirect::to('/');

    }

    $payment = Payment::get($payment_id, $this->_api_context);
    $execution = new PaymentExecution();
    $execution->setPayerId(Input::get('PayerID'));

    /**Execute the payment **/
    $result = $payment->execute($execution, $this->_api_context);

    if ($result->getState() == 'approved') {

        \Session::put('success', 'Payment success');
        return Redirect::to('/');

    }

    \Session::put('error', 'Payment failed');
    return Redirect::to('/');

}

刀片文件:

<form class="w3-container w3-display-middle w3-card-4 w3-padding-16" method="POST" id="payment-form"
      action="{!! URL::to('paypal') !!}">
      <div class="w3-container w3-teal w3-padding-16">Paywith Paypal</div>
      {{ csrf_field() }}
      <h2 class="w3-text-blue">Payment Form</h2>
      <p>Demo PayPal form - Integrating paypal in laravel</p>
      <label class="w3-text-blue"><b>Enter Amount</b></label>
      <input class="w3-input w3-border" id="amount" type="text" name="amount"></p>
      <button class="w3-btn w3-blue">Pay with PayPal</button>
    </form>

0 个答案:

没有答案