Passed
Push — master ( 4a884d...c9d1fc )
by Jeff
12:14
created

BankTransferController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 69
dl 0
loc 131
c 0
b 0
f 0
rs 10

3 Methods

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