1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Containers\Stripe\Tasks; |
4
|
|
|
|
5
|
|
|
use App\Containers\Payment\Contracts\ChargeableInterface; |
6
|
|
|
use App\Containers\Payment\Contracts\PaymentChargerInterface; |
7
|
|
|
use App\Containers\Payment\Models\AbstractPaymentAccount; |
8
|
|
|
use App\Containers\Payment\Models\PaymentTransaction; |
9
|
|
|
use App\Containers\Stripe\Exceptions\StripeAccountNotFoundException; |
10
|
|
|
use App\Containers\Stripe\Exceptions\StripeApiErrorException; |
11
|
|
|
use App\Ship\Parents\Tasks\Task; |
12
|
|
|
use Cartalyst\Stripe\Stripe; |
13
|
|
|
use Exception; |
14
|
|
|
use Illuminate\Support\Facades\Config; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class ChargeWithStripeTask. |
18
|
|
|
* |
19
|
|
|
* @author Mahmoud Zalt <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class ChargeWithStripeTask extends Task implements PaymentChargerInterface |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
private $stripe; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* ChargeWithStripeTask constructor. |
28
|
|
|
* |
29
|
|
|
* @param \Cartalyst\Stripe\Stripe $stripe |
30
|
|
|
*/ |
31
|
|
|
public function __construct(Stripe $stripe) |
32
|
|
|
{ |
33
|
|
|
$this->stripe = $stripe->make(Config::get('settings.stripe.secret'), Config::get('settings.stripe.version')); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param \App\Containers\Payment\Contracts\ChargeableInterface $user |
38
|
|
|
* @param \App\Containers\Payment\Models\AbstractPaymentAccount $account |
39
|
|
|
* @param float $amount |
40
|
|
|
* @param string $currency |
41
|
|
|
* |
42
|
|
|
* @return PaymentTransaction |
43
|
|
|
* @throws StripeAccountNotFoundException |
44
|
|
|
* @throws StripeApiErrorException |
45
|
|
|
*/ |
46
|
|
|
public function charge(ChargeableInterface $user, AbstractPaymentAccount $account, $amount, $currency = 'USD') : PaymentTransaction |
47
|
|
|
{ |
48
|
|
|
// NOTE: you should not call this function directly. Instead use the Payment Gateway in the Payment container. |
49
|
|
|
// Or even better to use the charge function in the ChargeableTrait. |
50
|
|
|
|
51
|
|
|
$valid = $account->checkIfPaymentDataIsSet(['customer_id', 'card_id', 'card_funding', 'card_last_digits', 'card_fingerprint']); |
52
|
|
|
|
53
|
|
|
if ($valid == false) { |
|
|
|
|
54
|
|
|
throw new StripeAccountNotFoundException('We could not find your credit card information. |
55
|
|
|
For security reasons, we do not store your credit card information on our server. |
56
|
|
|
So please login to our Web App and enter your credit card information directly into Stripe, |
57
|
|
|
then try to purchase the credits again. |
58
|
|
|
Thanks.'); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
try { |
62
|
|
|
$response = $this->stripe->charges()->create([ |
63
|
|
|
'customer' => $account->customer_id, |
64
|
|
|
'currency' => $currency, |
65
|
|
|
'amount' => $amount, |
66
|
|
|
]); |
67
|
|
|
|
68
|
|
|
} catch (Exception $e) { |
69
|
|
|
throw (new StripeApiErrorException('Stripe API error (chargeCustomer)'))->debug($e->getMessage(), true); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if ($response['status'] != 'succeeded') { |
73
|
|
|
throw new StripeApiErrorException('Stripe response status not succeeded (chargeCustomer)'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
if ($response['paid'] !== true) { |
77
|
|
|
throw new StripeApiErrorException('Payment was not set to PAID.'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// this data will be stored on the pivot table (user credits) |
81
|
|
|
$transaction = new PaymentTransaction([ |
82
|
|
|
'transaction_id' => $response['id'], |
83
|
|
|
'status' => $response['status'], |
84
|
|
|
'is_successful' => true, |
85
|
|
|
'data' => $response, |
86
|
|
|
]); |
87
|
|
|
|
88
|
|
|
return $transaction; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
} |
92
|
|
|
|
When comparing two booleans, it is generally considered safer to use the strict comparison operator.