Php Laravel Paypal创建订单

Php Laravel Paypal创建订单,php,laravel,paypal,Php,Laravel,Paypal,(拉雷维尔新成员) 好的,我在我的网站上使用paypal编写了这个代码,它工作正常,但问题是当我点击按钮进入paypal页面时,我总是创建订单,但我只想在paypal成功时创建订单 这是我的密码: Ps:我试图在if($result->getState()=='approved')上使用订单,但它不起作用 公共函数payWithpaypal(Request$Request){$apiContext=/) } 公共函数getPaymentStatus(请求$Request) { $apiCo

(拉雷维尔新成员)

好的,我在我的网站上使用paypal编写了这个代码,它工作正常,但问题是当我点击按钮进入paypal页面时,我总是创建订单,但我只想在paypal成功时创建订单

这是我的密码:

Ps:我试图在if($result->getState()=='approved')上使用订单,但它不起作用

公共函数payWithpaypal(Request$Request){$apiContext=/)

}

公共函数getPaymentStatus(请求$Request) { $apiContext=// ) ))

}

tks

$order = Order::create([
    'user_id' => Auth::user()->id,
    'name' => Auth::user()->name,
    'email' => Auth::user()->email,
    'fName' => $request->fName,            
    'lName' => $request->lName,
    'phone' => $request->phone,
    'address' => $request->address,
    'city' => $request->city,
    'country' => $request->country,
    'zip' => $request->zip,
    'payment' => $request->payment,
    'total' => $request->total,
]); 

$cartItems = Cart::getContent();

foreach($cartItems as $item) {

    $orderproduct = OrderProduct::create([
        'order_id' => $order->id,
        'product_id' => $item->id,
        'product_name' => $item->name,
        'quantity' => $item->quantity,
        'price' => $item->price,
        'total' => $item->getPriceSum(),
        'obs' => $item->attributes->obs,
        'custom_text' => $item->attributes->custom_text,
        'file' => $item->attributes->file,
        'wood' => $item->attributes->wood
    ]);
}

//Mail::send(new OrderPlaced($order));
//Mail::send(new OrderPlacedAdmin($order));

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

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

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

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

// Add NO SHIPPING OPTION
$inputFields = new InputFields();
$inputFields->setNoShipping(1);

$webProfile = new WebProfile();
$webProfile->setName('test' . uniqid())->setInputFields($inputFields);

$webProfileId = $webProfile->create($apiContext)->getId();

$payment = new Payment();
$payment->setExperienceProfileId($webProfileId); // no shipping
$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('pagamento');

    } else {

        \Session::put('error', 'Some error occur, sorry for inconvenient');
        return Redirect::to('pagamento');
    }
}

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('pagamento');
/** 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->get('PayerID')) || empty($request->get('token'))) {

    //\Session::put('error', 'O pagamento não foi efectuado');
    return Redirect::to('pagamento');
}

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

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

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

    Cart::clear();
    
    return Redirect::to('obrigado');
}

\Session::put('error', 'O pagamento não foi efectuado');
return Redirect::to('pagamento');