1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Canvas\Models; |
5
|
|
|
|
6
|
|
|
use Phalcon\Di; |
7
|
|
|
use Stripe\Token as StripeToken; |
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
|
|
|
[ |
107
|
|
|
Di::getDefault()->getUserData()->getId(), |
108
|
|
|
Di::getDefault()->getUserData()->getDefaultCompany()->getId(), |
109
|
|
|
Di::getDefault()->getApp()->getId() |
110
|
|
|
], |
111
|
|
|
"order"=> "id DESC" |
112
|
|
|
]); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Create a new record from Stripe Token |
117
|
|
|
* |
118
|
|
|
* @param string $token |
119
|
|
|
* @return return self |
|
|
|
|
120
|
|
|
*/ |
121
|
|
|
public static function createByStripeToken(string $token): self |
122
|
|
|
{ |
123
|
|
|
$ccinfo = self::getCardInfoFromStripe($token); |
124
|
|
|
|
125
|
|
|
$paymentMethodCred = new self(); |
126
|
|
|
$paymentMethodCred->users_id = Di::getDefault()->getUserData()->getId(); |
127
|
|
|
$paymentMethodCred->companies_id = Di::getDefault()->getUserData()->getDefaultCompany()->getId(); |
128
|
|
|
$paymentMethodCred->apps_id = Di::getDefault()->getApp()->getId(); |
129
|
|
|
$paymentMethodCred->payment_methods_id = PaymentMethods::getDefault()->getId(); |
130
|
|
|
$paymentMethodCred->payment_ending_numbers = $ccinfo->card->last4; |
131
|
|
|
$paymentMethodCred->expiration_date = $ccinfo->card->exp_year . '-' . $ccinfo->card->exp_month . '-' . '01'; |
132
|
|
|
$paymentMethodCred->zip_code = $ccinfo->card->address_zip; |
133
|
|
|
$paymentMethodCred->created_at = date('Y-m-d H:m:s'); |
134
|
|
|
$paymentMethodCred->saveOrFail(); |
135
|
|
|
|
136
|
|
|
return $paymentMethodCred; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Get Credit Card information from Stripe |
141
|
|
|
* |
142
|
|
|
* @param string $token |
143
|
|
|
* @return return StripeToken |
|
|
|
|
144
|
|
|
*/ |
145
|
|
|
private function getCardInfoFromStripe(string $token): StripeToken |
146
|
|
|
{ |
147
|
|
|
return StripeToken::retrieve($token,[ |
148
|
|
|
'api_key' => Di::getDefault()->getConfig()->stripe->secret |
149
|
|
|
]); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
|
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.