PaymentMethodsCreds::createByStripeToken()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 1
eloc 12
c 2
b 0
f 1
nc 1
nop 1
dl 0
loc 16
rs 9.8666
1
<?php
2
declare(strict_types=1);
3
4
namespace Canvas\Models;
5
6
use Phalcon\Di;
7
use Stripe\Token as StripeToken;
0 ignored issues
show
Bug introduced by
The type Stripe\Token was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
8
use Canvas\Models\PaymentMethods;
9
10
class PaymentMethodsCreds extends AbstractModel
11
{
12
    /**
13
     *
14
     * @var integer
15
     */
16
    public $id;
17
18
    /**
19
     *
20
     * @var integer
21
     */
22
    public $users_id;
23
24
    /**
25
     *
26
     * @var integer
27
     */
28
    public $companies_id;
29
30
    /**
31
     *
32
     * @var integer
33
     */
34
    public $apps_id;
35
36
    /**
37
     *
38
     * @var integer
39
     */
40
    public $payment_methods_id;
41
42
    /**
43
     *
44
     * @var string
45
     */
46
    public $payment_ending_numbers;
47
48
    /**
49
     *
50
     * @var string
51
     */
52
    public $expiration_date;
53
54
    /**
55
     *
56
     * @var string
57
     */
58
    public $zip_code;
59
60
    /**
61
     *
62
     * @var string
63
     */
64
    public $created_at;
65
66
    /**
67
     *
68
     * @var string
69
     */
70
    public $updated_at;
71
72
    /**
73
     *
74
     * @var integer
75
     */
76
    public $is_deleted;
77
78
    /**
79
     * Initialize method for model.
80
     */
81
    public function initialize()
82
    {
83
        $this->setSource('payment_methods_creds');
84
    }
85
86
    /**
87
     * Returns table name mapped in the model.
88
     *
89
     * @return string
90
     */
91
    public function getSource(): string
92
    {
93
        return 'payment_methods_creds';
94
    }
95
96
    /**
97
     * Returns the current payment method credentials.
98
     *
99
     * @return string
100
     */
101
    public function getCurrentPaymentMethodCreds(): self
102
    {
103
        return self::findFirstOrFail([
104
            'conditions' => 'users_id = ?0 and companies_id = ?1 and apps_id = ?2 and is_deleted = 0',
105
            'bind' => [
106
                Di::getDefault()->getUserData()->getId(),
107
                Di::getDefault()->getUserData()->getDefaultCompany()->getId(),
108
                Di::getDefault()->getApp()->getId()
109
            ],
110
            'order' => 'id DESC'
111
        ]);
112
    }
113
114
    /**
115
     * Create a new record from Stripe Token.
116
     *
117
     * @param string $token
118
     * @return return self
0 ignored issues
show
Bug introduced by
The type Canvas\Models\return was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
119
     */
120
    public static function createByStripeToken(string $token): self
121
    {
122
        $ccInfo = self::getCardInfoFromStripe($token);
123
124
        $paymentMethodCred = new self();
125
        $paymentMethodCred->users_id = Di::getDefault()->getUserData()->getId();
126
        $paymentMethodCred->companies_id = Di::getDefault()->getUserData()->getDefaultCompany()->getId();
127
        $paymentMethodCred->apps_id = Di::getDefault()->getApp()->getId();
128
        $paymentMethodCred->payment_methods_id = PaymentMethods::getDefault()->getId();
129
        $paymentMethodCred->payment_ending_numbers = $ccInfo->card->last4;
130
        $paymentMethodCred->expiration_date = $ccInfo->card->exp_year . '-' . $ccInfo->card->exp_month . '-' . '01';
131
        $paymentMethodCred->zip_code = $ccInfo->card->address_zip;
132
        $paymentMethodCred->created_at = date('Y-m-d H:m:s');
133
        $paymentMethodCred->saveOrFail();
134
135
        return $paymentMethodCred;
136
    }
137
138
    /**
139
     * Get Credit Card information from Stripe.
140
     *
141
     * @param string $token
142
     * @return return StripeToken
143
     */
144
    private function getCardInfoFromStripe(string $token): StripeToken
145
    {
146
        return StripeToken::retrieve($token, [
147
            'api_key' => Di::getDefault()->getConfig()->stripe->secret
148
        ]);
149
    }
150
}
151