Completed
Push — master ( e93f60...2f787f )
by Mahmoud
08:56 queued 02:41
created

ChargeWithStripeTask::run()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 34
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 34
rs 8.439
cc 5
eloc 18
nc 5
nop 3
1
<?php
2
3
namespace App\Containers\Stripe\Tasks;
4
5
use App\Containers\Stripe\Exceptions\StripeAccountNotFoundException;
6
use App\Containers\Stripe\Exceptions\StripeApiErrorException;
7
use App\Containers\User\Models\User;
8
use App\Port\Task\Abstracts\Task;
9
use App\Containers\Payment\Contracts\Chargeable;
10
use Cartalyst\Stripe\Stripe;
11
use Exception;
12
use Illuminate\Support\Facades\Config;
13
14
/**
15
 * Class ChargeWithStripeTask.
16
 *
17
 * @author Mahmoud Zalt <[email protected]>
18
 */
19
class ChargeWithStripeTask extends Task implements Chargeable
20
{
21
22
    public $stripe;
23
24
    /**
25
     * StripeApi constructor.
26
     *
27
     * @param \Cartalyst\Stripe\Stripe $stripe
28
     */
29
    public function __construct(Stripe $stripe)
30
    {
31
        $this->stripe = $stripe->make(Config::get('services.stripe.secret'), Config::get('services.stripe.version'));
32
    }
33
34
    /**
35
     * @param \App\Containers\User\Models\User $user
36
     * @param                                  $amount
37
     * @param string                           $currency
38
     *
39
     * @return  array|null
40
     * @throws \App\Containers\Stripe\Exceptions\StripeAccountNotFoundException
41
     */
42
    public function run(User $user, $amount, $currency = 'USD')
43
    {
44
        $stripeAccount = $user->stripeAccount;
45
46
        if(!$stripeAccount){
47
            throw new StripeAccountNotFoundException('You need to enter your payment information first (Credit Card), by calling another endpoint.');
48
        }
49
50
        try {
51
52
            $response = $this->stripe->charges()->create([
53
                'customer' => $user->stripeAccount->customer_id,
54
                'currency' => $currency,
55
                'amount'   => $amount,
56
            ]);
57
58
        } catch (Exception $e) {
59
            throw (new StripeApiErrorException('Stripe API error (chargeCustomer)'))->debug($e->getMessage(), true);
60
        }
61
62
        if ($response['status'] != 'succeeded') {
63
            throw new StripeApiErrorException('Stripe response status not succeeded (chargeCustomer)');
64
        }
65
66
        if ($response['paid'] !== true) {
67
            return null;
68
        }
69
70
        // this data will be stored on the pivot table (user credits)
71
        return [
72
            'payment_method' => 'stripe',
73
            'description'    => $response['id'] // the charge id
74
        ];
75
    }
76
77
    /**
78
     * @param \App\Containers\User\Models\User $user
79
     * @param                                  $amount
80
     * @param string                           $currency
81
     *
82
     * @return  array|null
83
     */
84
    public function charge(User $user, $amount, $currency = 'USD')
85
    {
86
        return $this->run($user, $amount, $currency);
87
    }
88
89
}
90