|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Shop\PaymentMethods\Stripe; |
|
4
|
|
|
|
|
5
|
|
|
use App\Shop\Checkout\CheckoutRepository; |
|
6
|
|
|
use App\Shop\Couriers\Courier; |
|
7
|
|
|
use App\Shop\Couriers\Repositories\CourierRepository; |
|
8
|
|
|
use App\Shop\Customers\Customer; |
|
9
|
|
|
use App\Shop\Customers\Repositories\CustomerRepository; |
|
10
|
|
|
use App\Shop\PaymentMethods\Stripe\Exceptions\StripeChargingErrorException; |
|
11
|
|
|
use Gloudemans\Shoppingcart\Facades\Cart; |
|
12
|
|
|
use Ramsey\Uuid\Uuid; |
|
13
|
|
|
use Stripe\Charge; |
|
14
|
|
|
|
|
15
|
|
|
class StripeRepository |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var Customer |
|
19
|
|
|
*/ |
|
20
|
|
|
private $customer; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* StripeRepository constructor. |
|
24
|
|
|
* @param Customer $customer |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct(Customer $customer) |
|
27
|
|
|
{ |
|
28
|
|
|
$this->customer = $customer; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param array $data Cart data |
|
33
|
|
|
* @param $total float Total items in the cart |
|
34
|
|
|
* @param $tax float The tax applied to the cart |
|
35
|
|
|
* @return Charge Stripe charge object |
|
36
|
|
|
* @throws StripeChargingErrorException |
|
37
|
|
|
*/ |
|
38
|
|
|
public function execute(array $data, $total, $tax) : Charge |
|
39
|
|
|
{ |
|
40
|
|
|
try { |
|
41
|
|
|
$courierRepo = new CourierRepository(new Courier); |
|
42
|
|
|
$courierId = $data['courier']; |
|
43
|
|
|
$courier = $courierRepo->findCourierById($courierId); |
|
44
|
|
|
|
|
45
|
|
|
$totalComputed = $total + $courier->cost; |
|
46
|
|
|
|
|
47
|
|
|
$customerRepo = new CustomerRepository($this->customer); |
|
48
|
|
|
$options['source'] = $data['stripeToken']; |
|
|
|
|
|
|
49
|
|
|
$options['currency'] = config('cart.currency'); |
|
50
|
|
|
|
|
51
|
|
|
if ($charge = $customerRepo->charge($totalComputed, $options)) { |
|
|
|
|
|
|
52
|
|
|
$checkoutRepo = new CheckoutRepository; |
|
53
|
|
|
$checkoutRepo->buildCheckoutItems([ |
|
54
|
|
|
'reference' => Uuid::uuid4()->toString(), |
|
55
|
|
|
'courier_id' => $courierId, |
|
56
|
|
|
'customer_id' => $this->customer->id, |
|
57
|
|
|
'address_id' => $data['billing_address'], |
|
58
|
|
|
'order_status_id' => 1, |
|
59
|
|
|
'payment' => strtolower(config('stripe.name')), |
|
60
|
|
|
'discounts' => 0, |
|
61
|
|
|
'total_products' => $total, |
|
62
|
|
|
'total' => $totalComputed, |
|
63
|
|
|
'total_paid' => $totalComputed, |
|
64
|
|
|
'tax' => $tax |
|
65
|
|
|
]); |
|
66
|
|
|
|
|
67
|
|
|
Cart::destroy(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
return $charge; |
|
71
|
|
|
} catch (\Exception $e) { |
|
72
|
|
|
throw new StripeChargingErrorException($e); |
|
73
|
|
|
} |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|