Completed
Push — master ( d8e2a0...679457 )
by Mahmoud
04:23
created

ChargeWithStripeTask::run()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 30
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 30
rs 8.5806
cc 4
eloc 16
nc 4
nop 3
1
<?php
2
3
namespace App\Containers\Stripe\Tasks;
4
5
use App\Containers\Stripe\Exceptions\StripeApiErrorException;
6
use App\Containers\User\Models\User;
7
use App\Port\Task\Abstracts\Task;
8
use App\Containers\Payments\Contracts\Chargeable;
9
use Cartalyst\Stripe\Stripe;
10
use Exception;
11
use Illuminate\Support\Facades\Config;
12
13
/**
14
 * Class ChargeWithStripeTask.
15
 *
16
 * @author Mahmoud Zalt <[email protected]>
17
 */
18
class ChargeWithStripeTask extends Task implements Chargeable
19
{
20
21
    public $stripe;
22
23
    /**
24
     * StripeApi constructor.
25
     *
26
     * @param \Cartalyst\Stripe\Stripe $stripe
27
     */
28
    public function __construct(Stripe $stripe)
29
    {
30
        $this->stripe = $stripe->make(Config::get('services.stripe.secret'), Config::get('services.stripe.version'));
31
    }
32
33
    /**
34
     * @param \App\Containers\User\Models\User $user
35
     * @param                                  $amount
36
     * @param string                           $currency
37
     *
38
     * @return  array|null
39
     */
40
    public function run(User $user, $amount, $currency = 'USD')
41
    {
42
        $customerStripeId = $user->stripeAccount->customer_id;
0 ignored issues
show
Documentation introduced by
The property stripeAccount does not exist on object<App\Containers\User\Models\User>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
43
44
        try {
45
46
            $response = $this->stripe->charges()->create([
47
                'customer' => $customerStripeId,
48
                'currency' => $currency,
49
                'amount'   => $amount,
50
            ]);
51
52
        } catch (Exception $e) {
53
            throw (new StripeApiErrorException('Stripe API error (chargeCustomer)'))->debug($e->getMessage(), true);
54
        }
55
56
        if ($response['status'] != 'succeeded') {
57
            throw new StripeApiErrorException('Stripe response status not succeeded (chargeCustomer)');
58
        }
59
60
        if ($response['paid'] !== true) {
61
            return null;
62
        }
63
64
        // this data will be stored on the pivot table (user credits)
65
        return [
66
            'payment_method' => 'stripe',
67
            'description'    => $response['id'] // the charge id
68
        ];
69
    }
70
71
    /**
72
     * @param \App\Containers\User\Models\User $user
73
     * @param                                  $amount
74
     * @param string                           $currency
75
     *
76
     * @return  array|null
77
     */
78
    public function charge(User $user, $amount, $currency = 'USD')
79
    {
80
        return $this->run($user, $amount, $currency);
81
    }
82
83
}
84