Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public function onConfirm() {
- if ( !Auth::check() ) {
- $redirectUrl = $this->controller->pageUrl( 'account/login' );
- return Redirect::guest( $redirectUrl );
- } else {
- $user = Auth::getUser();
- }
- if ( !$this->checkSession() ) {
- return Redirect::to( '/' );
- }
- $post = collect( post() );
- if ( !$post->has( 'payment-method' ) || is_null( $post->get( 'payment-method' ) ) ) {
- Flash::error( 'Seleziona un metodo di pagamento per continuare.' );
- return;
- }
- $paymentMethod = $post->get( 'payment-method' );
- $gateway = \PaymentGateway::create( $paymentMethod );
- /** @var \Truckpooling\Common\Models\Shipping $shipment */
- $shipment = Utils::arrayToShippingModel( Session::get( 'shipment' ), true );
- // Validate privacy checks.
- $couValidator = Validator::make(
- [
- 'cou' => $post->get( 'cou' ),
- 'privacy' => $post->get( 'privacy' ),
- 'transport_cou' => $post->get( 'transport_cou' )
- ],
- $this->onConfirmRules,
- [
- 'required' => Lang::get( 'truckpooling.frontend::lang.cou.missing_check' )
- ]
- );
- if ( $couValidator->fails() ) {
- $this->page['validationMessages'] = $couValidator->messages();
- return [
- '#error-message' => $this->renderPartial( '@message' )
- ];
- } else {
- $shipment->setCou( true );
- $shipment->setPrivacy( true );
- $shipment->setTransportCou( true );
- }
- $shipment->getFromAddress()->save();
- $shipment->getToAddress()->save();
- $shipment->setUser( $user );
- $shipment->priceListId = Session::get( 'priceListId' );
- $service = $shipment->getService();
- $shipment->created_at = Argon::now();
- $shipment->save();
- $order = new Order();
- if ( Session::has( 'couponCode' ) ) {
- /** @var Coupon $coupon */
- $coupon = Coupon::where( 'code', Session::get( 'couponCode' ) )->first();
- $order->setCoupon( $coupon );
- if ( Session::has( 'couponDiscount' ) ) {
- $order->setCouponDiscount( Session::get( 'couponDiscount' ) );
- }
- }
- $order->setPaymentMethod( $paymentMethod );
- $order->setReference( $shipment->getReference() );
- $order->setUser( $user );
- try {
- $order->save();
- } catch ( \Exception $e ) {
- /** @var Order $fromDb */
- $fromDb = Order::findByReference( $order->getReference() )->withTrashed()->first();
- if ( !is_null( $fromDb ) ) {
- $fromDb->items()->forceDelete();
- $fromDb->forceDelete();
- }
- $order->save();
- }
- $value = Session::has( 'discountedListValue' ) ?
- Session::get( 'discountedListValue' ) :
- Session::get( 'listValue' );
- $order->addItem(
- OrderItem::make(
- [
- 'name' => $service->getCarrier()->getName() . ' ' . $service->getName(),
- 'price' => round( Price::withTax( $value ), 2 ),
- 'tax' => round( Price::getTax( $value ), 2 ),
- 'shipping_id' => $shipment->id
- ]
- )
- );
- $additionalServiceValues = [];
- if ( Session::has( 'additionalServiceValues' ) ) {
- $additionalServiceValues = Session::get( 'additionalServiceValues' );
- }
- foreach ( $additionalServiceValues as $code => $value ) {
- if ( $code === 'cod' ) {
- $cod = $shipment->getCod();
- if ( is_null( $cod ) ) {
- continue;
- }
- switch ( $cod->getPaymentType() ) {
- case Cod::TYPE_MANUAL:
- $code = 'codContanti';
- break;
- case Cod::TYPE_POS:
- $code = 'codPos';
- break;
- case Cod::TYPE_MANUAL_POS:
- $code = 'codContantiPos';
- break;
- default:
- continue;
- }
- }
- /** @var AdditionalService $additionalService */
- $additionalService = AdditionalService::where( 'code_name', $code )->first();
- if ( is_null( $additionalService ) ) {
- continue;
- }
- $order->addItem( OrderItem::make( [
- 'name' => $additionalService->getName(),
- 'price' => round( Price::withTax( $value ), 2 ),
- 'tax' => round( Price::getTax( $value ), 2 )
- ] ) );
- }
- /** @noinspection PhpUndefinedMethodInspection */
- $surpluses = $user->surpluses()->waiting()->get();
- /** @noinspection PhpUndefinedMethodInspection */
- $surplus = $surpluses->sum( function ( Surplus $surplus ) {
- return $surplus->totalSurplus - $surplus->totalPaid;
- } );
- if ( $surplus > 0 ) {
- $order->addItem( OrderItem::make( [
- 'name' => 'Eccedenze',
- 'price' => round( $surplus, 2 ),
- 'tax' => round( Price::getTax( $surplus ), 2 )
- ] ) );
- }
- $data = [];
- if ( $paymentMethod == PayPalGateway::NAME ) {
- // PayPal request data.
- $data['items'] = $order->getItems()->map(
- function ( OrderItemContract $item ) {
- return [
- 'name' => $item->hasShipping() ? $item->getShipping()->getReference() : $item->getName(),
- 'price' => round( $item->getPrice(), 2 ),
- 'qty' => $item->getQuantity()
- ];
- }
- )->all();
- $data['total'] = round( $order->getTotal(), 2 );
- // TODO: Take PayPal return and cancel URLs from Settings.
- $data['return_url'] = url( '/paypal/complete/' . $order->getReference() );
- $data['cancel_url'] = url( '/checkout-failure/' . $order->getReference() );
- $data['invoice_id'] = $order->getReference();
- $data['invoice_description'] = $order->getReference();
- } else if ( $paymentMethod == PagonlineGateway::NAME ) {
- $data = [
- 'shop_id' => $order->getReference(),
- 'notify_url' => url( '/pagonline/verify' ),
- 'error_url' => url( '/checkout-failure/' . $order->getReference() ),
- // NOTE: see docs.
- 'total' => round( round( $order->getTotal(), 2 ) * 100, 0 )
- ];
- } else if ( $paymentMethod == BorsinoGateway::NAME ) {
- $data = [
- 'order' => $order
- ];
- Log::info('----- ORDER BORSINO ----');
- Log::info($order);
- } else if ( $paymentMethod == BankTransferGateway::NAME ) {
- $order->setStatus( 'awaiting_bank_transfer_payment' );
- $order->save();
- }
- else if( $paymentMethod == FidoGateway::NAME)
- {
- $data = [
- 'order' => $order
- ];
- }
- try {
- $response = $gateway->purchase( $data )->send();
- } catch ( \Exception $e ) {
- Log::error( 'Payment error: ' . $e->getMessage() );
- Flash::error( 'Si è verificato un errore inatteso. Si prega di riprovare tra qualche istante.' );
- return;
- }
- if ( $response->isFailed() ) {
- throw new ApplicationException( 'Error in payment request: ' . $response->getMessage() );
- }
- if ( $paymentMethod != BankTransferGateway::NAME ) {
- $transaction = Transaction::make(
- [
- 'reference' => $response->getTransactionReference(),
- 'gateway' => $gateway->getShortName(),
- 'value' => $order->getTotal()
- ]
- );
- $order->transactions()->save( $transaction );
- }
- if ( isset( $coupon ) && !is_null( $coupon ) ) {
- /** @var Coupon $userCoupon */
- $userCoupon = $user->coupons()->find( $coupon->id );
- if ( !is_null( $userCoupon ) ) {
- $user->coupons()->updateExistingPivot(
- $userCoupon->id,
- [ 'usages' => ( $userCoupon->pivot->usages - 1 ) ]
- );
- }
- }
- if ( $response->isRedirect() ) {
- Log::info($response->redirect());
- Utils::removeSessionShipmentValues();
- return $response->redirect();
- }
- if ( $response->isSuccessful() ) {
- if ( $paymentMethod == BankTransferGateway::NAME ) {
- $details = Settings::get( 'payment_details' );
- $details = str_replace( '[:total]', Price::format( $order->getTotal() ), $details );
- Session::put( 'payment_details', $details );
- }
- Utils::removeSessionShipmentValues();
- return Redirect::to( '/checkout-success/' . $order->getReference() );
- }
- Utils::removeSessionShipmentValues();
- return Redirect::to( '/checkout-success/' . $order->getReference() );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement