Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.63 KB | None | 0 0
  1. public function onConfirm() {
  2. if ( !Auth::check() ) {
  3. $redirectUrl = $this->controller->pageUrl( 'account/login' );
  4.  
  5. return Redirect::guest( $redirectUrl );
  6. } else {
  7. $user = Auth::getUser();
  8. }
  9.  
  10. if ( !$this->checkSession() ) {
  11. return Redirect::to( '/' );
  12. }
  13.  
  14. $post = collect( post() );
  15. if ( !$post->has( 'payment-method' ) || is_null( $post->get( 'payment-method' ) ) ) {
  16. Flash::error( 'Seleziona un metodo di pagamento per continuare.' );
  17.  
  18. return;
  19. }
  20. $paymentMethod = $post->get( 'payment-method' );
  21. $gateway = \PaymentGateway::create( $paymentMethod );
  22.  
  23. /** @var \Truckpooling\Common\Models\Shipping $shipment */
  24. $shipment = Utils::arrayToShippingModel( Session::get( 'shipment' ), true );
  25.  
  26. // Validate privacy checks.
  27. $couValidator = Validator::make(
  28. [
  29. 'cou' => $post->get( 'cou' ),
  30. 'privacy' => $post->get( 'privacy' ),
  31. 'transport_cou' => $post->get( 'transport_cou' )
  32. ],
  33. $this->onConfirmRules,
  34. [
  35. 'required' => Lang::get( 'truckpooling.frontend::lang.cou.missing_check' )
  36. ]
  37. );
  38. if ( $couValidator->fails() ) {
  39. $this->page['validationMessages'] = $couValidator->messages();
  40.  
  41. return [
  42. '#error-message' => $this->renderPartial( '@message' )
  43. ];
  44. } else {
  45. $shipment->setCou( true );
  46. $shipment->setPrivacy( true );
  47. $shipment->setTransportCou( true );
  48. }
  49.  
  50. $shipment->getFromAddress()->save();
  51. $shipment->getToAddress()->save();
  52. $shipment->setUser( $user );
  53. $shipment->priceListId = Session::get( 'priceListId' );
  54. $service = $shipment->getService();
  55. $shipment->created_at = Argon::now();
  56. $shipment->save();
  57.  
  58. $order = new Order();
  59.  
  60. if ( Session::has( 'couponCode' ) ) {
  61. /** @var Coupon $coupon */
  62. $coupon = Coupon::where( 'code', Session::get( 'couponCode' ) )->first();
  63.  
  64. $order->setCoupon( $coupon );
  65. if ( Session::has( 'couponDiscount' ) ) {
  66. $order->setCouponDiscount( Session::get( 'couponDiscount' ) );
  67. }
  68. }
  69.  
  70. $order->setPaymentMethod( $paymentMethod );
  71. $order->setReference( $shipment->getReference() );
  72. $order->setUser( $user );
  73.  
  74. try {
  75. $order->save();
  76. } catch ( \Exception $e ) {
  77. /** @var Order $fromDb */
  78. $fromDb = Order::findByReference( $order->getReference() )->withTrashed()->first();
  79. if ( !is_null( $fromDb ) ) {
  80. $fromDb->items()->forceDelete();
  81. $fromDb->forceDelete();
  82. }
  83. $order->save();
  84. }
  85.  
  86. $value = Session::has( 'discountedListValue' ) ?
  87. Session::get( 'discountedListValue' ) :
  88. Session::get( 'listValue' );
  89.  
  90. $order->addItem(
  91. OrderItem::make(
  92. [
  93. 'name' => $service->getCarrier()->getName() . ' ' . $service->getName(),
  94. 'price' => round( Price::withTax( $value ), 2 ),
  95. 'tax' => round( Price::getTax( $value ), 2 ),
  96. 'shipping_id' => $shipment->id
  97. ]
  98. )
  99. );
  100.  
  101. $additionalServiceValues = [];
  102. if ( Session::has( 'additionalServiceValues' ) ) {
  103. $additionalServiceValues = Session::get( 'additionalServiceValues' );
  104. }
  105.  
  106. foreach ( $additionalServiceValues as $code => $value ) {
  107. if ( $code === 'cod' ) {
  108. $cod = $shipment->getCod();
  109. if ( is_null( $cod ) ) {
  110. continue;
  111. }
  112.  
  113. switch ( $cod->getPaymentType() ) {
  114. case Cod::TYPE_MANUAL:
  115. $code = 'codContanti';
  116. break;
  117. case Cod::TYPE_POS:
  118. $code = 'codPos';
  119. break;
  120. case Cod::TYPE_MANUAL_POS:
  121. $code = 'codContantiPos';
  122. break;
  123. default:
  124. continue;
  125. }
  126. }
  127. /** @var AdditionalService $additionalService */
  128. $additionalService = AdditionalService::where( 'code_name', $code )->first();
  129.  
  130. if ( is_null( $additionalService ) ) {
  131. continue;
  132. }
  133.  
  134. $order->addItem( OrderItem::make( [
  135. 'name' => $additionalService->getName(),
  136. 'price' => round( Price::withTax( $value ), 2 ),
  137. 'tax' => round( Price::getTax( $value ), 2 )
  138. ] ) );
  139. }
  140.  
  141. /** @noinspection PhpUndefinedMethodInspection */
  142. $surpluses = $user->surpluses()->waiting()->get();
  143.  
  144. /** @noinspection PhpUndefinedMethodInspection */
  145. $surplus = $surpluses->sum( function ( Surplus $surplus ) {
  146. return $surplus->totalSurplus - $surplus->totalPaid;
  147. } );
  148.  
  149. if ( $surplus > 0 ) {
  150. $order->addItem( OrderItem::make( [
  151. 'name' => 'Eccedenze',
  152. 'price' => round( $surplus, 2 ),
  153. 'tax' => round( Price::getTax( $surplus ), 2 )
  154. ] ) );
  155. }
  156.  
  157. $data = [];
  158.  
  159. if ( $paymentMethod == PayPalGateway::NAME ) {
  160. // PayPal request data.
  161. $data['items'] = $order->getItems()->map(
  162. function ( OrderItemContract $item ) {
  163. return [
  164. 'name' => $item->hasShipping() ? $item->getShipping()->getReference() : $item->getName(),
  165. 'price' => round( $item->getPrice(), 2 ),
  166. 'qty' => $item->getQuantity()
  167. ];
  168. }
  169. )->all();
  170.  
  171. $data['total'] = round( $order->getTotal(), 2 );
  172.  
  173. // TODO: Take PayPal return and cancel URLs from Settings.
  174. $data['return_url'] = url( '/paypal/complete/' . $order->getReference() );
  175. $data['cancel_url'] = url( '/checkout-failure/' . $order->getReference() );
  176.  
  177. $data['invoice_id'] = $order->getReference();
  178. $data['invoice_description'] = $order->getReference();
  179.  
  180. } else if ( $paymentMethod == PagonlineGateway::NAME ) {
  181. $data = [
  182. 'shop_id' => $order->getReference(),
  183. 'notify_url' => url( '/pagonline/verify' ),
  184. 'error_url' => url( '/checkout-failure/' . $order->getReference() ),
  185. // NOTE: see docs.
  186. 'total' => round( round( $order->getTotal(), 2 ) * 100, 0 )
  187. ];
  188. } else if ( $paymentMethod == BorsinoGateway::NAME ) {
  189. $data = [
  190. 'order' => $order
  191. ];
  192.  
  193. Log::info('----- ORDER BORSINO ----');
  194. Log::info($order);
  195. } else if ( $paymentMethod == BankTransferGateway::NAME ) {
  196. $order->setStatus( 'awaiting_bank_transfer_payment' );
  197. $order->save();
  198. }
  199. else if( $paymentMethod == FidoGateway::NAME)
  200. {
  201. $data = [
  202. 'order' => $order
  203. ];
  204. }
  205.  
  206. try {
  207. $response = $gateway->purchase( $data )->send();
  208.  
  209. } catch ( \Exception $e ) {
  210. Log::error( 'Payment error: ' . $e->getMessage() );
  211. Flash::error( 'Si è verificato un errore inatteso. Si prega di riprovare tra qualche istante.' );
  212.  
  213. return;
  214. }
  215.  
  216. if ( $response->isFailed() ) {
  217. throw new ApplicationException( 'Error in payment request: ' . $response->getMessage() );
  218. }
  219.  
  220. if ( $paymentMethod != BankTransferGateway::NAME ) {
  221. $transaction = Transaction::make(
  222. [
  223. 'reference' => $response->getTransactionReference(),
  224. 'gateway' => $gateway->getShortName(),
  225. 'value' => $order->getTotal()
  226. ]
  227. );
  228.  
  229. $order->transactions()->save( $transaction );
  230. }
  231.  
  232. if ( isset( $coupon ) && !is_null( $coupon ) ) {
  233. /** @var Coupon $userCoupon */
  234. $userCoupon = $user->coupons()->find( $coupon->id );
  235. if ( !is_null( $userCoupon ) ) {
  236. $user->coupons()->updateExistingPivot(
  237. $userCoupon->id,
  238. [ 'usages' => ( $userCoupon->pivot->usages - 1 ) ]
  239. );
  240. }
  241. }
  242.  
  243. if ( $response->isRedirect() ) {
  244.  
  245. Log::info($response->redirect());
  246. Utils::removeSessionShipmentValues();
  247.  
  248. return $response->redirect();
  249. }
  250.  
  251. if ( $response->isSuccessful() ) {
  252. if ( $paymentMethod == BankTransferGateway::NAME ) {
  253. $details = Settings::get( 'payment_details' );
  254. $details = str_replace( '[:total]', Price::format( $order->getTotal() ), $details );
  255. Session::put( 'payment_details', $details );
  256. }
  257.  
  258.  
  259. Utils::removeSessionShipmentValues();
  260.  
  261. return Redirect::to( '/checkout-success/' . $order->getReference() );
  262. }
  263.  
  264. Utils::removeSessionShipmentValues();
  265.  
  266. return Redirect::to( '/checkout-success/' . $order->getReference() );
  267. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement