BankTransferController.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. namespace App\Http\Controllers\Front\Payments;
  3. use App\Http\Controllers\Controller;
  4. use App\Shop\Carts\Repositories\Interfaces\CartRepositoryInterface;
  5. use App\Shop\Checkout\CheckoutRepository;
  6. use App\Shop\Orders\Repositories\OrderRepository;
  7. use App\Shop\OrderStatuses\OrderStatus;
  8. use App\Shop\OrderStatuses\Repositories\OrderStatusRepository;
  9. use App\Shop\Shipping\ShippingInterface;
  10. use Gloudemans\Shoppingcart\Facades\Cart;
  11. use Illuminate\Http\Request;
  12. use Illuminate\Support\Facades\Log;
  13. use Ramsey\Uuid\Uuid;
  14. use Shippo_Shipment;
  15. use Shippo_Transaction;
  16. class BankTransferController extends Controller
  17. {
  18. /**
  19. * @var CartRepositoryInterface
  20. */
  21. private $cartRepo;
  22. /**
  23. * @var int $shipping
  24. */
  25. private $shippingFee;
  26. private $rateObjectId;
  27. private $shipmentObjId;
  28. private $billingAddress;
  29. private $carrier;
  30. /**
  31. * BankTransferController constructor.
  32. *
  33. * @param Request $request
  34. * @param CartRepositoryInterface $cartRepository
  35. * @param ShippingInterface $shippingRepo
  36. */
  37. public function __construct(
  38. Request $request,
  39. CartRepositoryInterface $cartRepository,
  40. ShippingInterface $shippingRepo
  41. )
  42. {
  43. $this->cartRepo = $cartRepository;
  44. $fee = 0;
  45. $rateObjId = null;
  46. $shipmentObjId = null;
  47. $billingAddress = $request->input('billing_address');
  48. if ($request->has('rate')) {
  49. if ($request->input('rate') != '') {
  50. $rate_id = $request->input('rate');
  51. $rates = $shippingRepo->getRates($request->input('shipment_obj_id'));
  52. $rate = collect($rates->results)->filter(function ($rate) use ($rate_id) {
  53. return $rate->object_id == $rate_id;
  54. })->first();
  55. $fee = $rate->amount;
  56. $rateObjId = $rate->object_id;
  57. $shipmentObjId = $request->input('shipment_obj_id');
  58. $this->carrier = $rate;
  59. }
  60. }
  61. $this->shippingFee = $fee;
  62. $this->rateObjectId = $rateObjId;
  63. $this->shipmentObjId = $shipmentObjId;
  64. $this->billingAddress = $billingAddress;
  65. }
  66. /**
  67. * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
  68. */
  69. public function index()
  70. {
  71. return view('front.bank-transfer-redirect', [
  72. 'subtotal' => $this->cartRepo->getSubTotal(),
  73. 'shipping' => $this->shippingFee,
  74. 'tax' => $this->cartRepo->getTax(),
  75. 'total' => $this->cartRepo->getTotal(2, $this->shippingFee),
  76. 'rateObjectId' => $this->rateObjectId,
  77. 'shipmentObjId' => $this->shipmentObjId,
  78. 'billingAddress' => $this->billingAddress
  79. ]);
  80. }
  81. /**
  82. * @param Request $request
  83. *
  84. * @return \Illuminate\Http\RedirectResponse
  85. * @throws \Exception
  86. */
  87. public function store(Request $request)
  88. {
  89. $checkoutRepo = new CheckoutRepository;
  90. $orderStatusRepo = new OrderStatusRepository(new OrderStatus);
  91. $os = $orderStatusRepo->findByName('ordered');
  92. $order = $checkoutRepo->buildCheckoutItems([
  93. 'reference' => Uuid::uuid4()->toString(),
  94. 'courier_id' => 1, // @deprecated
  95. 'customer_id' => $request->user()->id,
  96. 'address_id' => $request->input('billing_address'),
  97. 'order_status_id' => $os->id,
  98. 'payment' => strtolower(config('bank-transfer.name')),
  99. 'discounts' => 0,
  100. 'total_products' => $this->cartRepo->getSubTotal(),
  101. 'total' => $this->cartRepo->getTotal(2, $this->shippingFee),
  102. 'total_shipping' => $this->shippingFee,
  103. 'total_paid' => 0,
  104. 'tax' => $this->cartRepo->getTax()
  105. ]);
  106. if (env('ACTIVATE_SHIPPING') == 1) {
  107. $shipment = Shippo_Shipment::retrieve($this->shipmentObjId);
  108. $details = [
  109. 'shipment' => [
  110. 'address_to' => json_decode($shipment->address_to, true),
  111. 'address_from' => json_decode($shipment->address_from, true),
  112. 'parcels' => [json_decode($shipment->parcels[0], true)]
  113. ],
  114. 'carrier_account' => $this->carrier->carrier_account,
  115. 'servicelevel_token' => $this->carrier->servicelevel->token
  116. ];
  117. $transaction = Shippo_Transaction::create($details);
  118. if ($transaction['status'] != 'SUCCESS'){
  119. Log::error($transaction['messages']);
  120. return redirect()->route('checkout.index')->with('error', 'There is an error in the shipment details. Check logs.');
  121. }
  122. $orderRepo = new OrderRepository($order);
  123. $orderRepo->updateOrder([
  124. 'courier' => $this->carrier->provider,
  125. 'label_url' => $transaction['label_url'],
  126. 'tracking_number' => $transaction['tracking_number']
  127. ]);
  128. }
  129. Cart::destroy();
  130. return redirect()->route('accounts', ['tab' => 'orders'])->with('message', 'Order successful!');
  131. }
  132. }