This repository was archived by the owner on Mar 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 26
Expand file tree
/
Copy pathpaymentmethod.php
More file actions
211 lines (168 loc) · 6.42 KB
/
Copy pathpaymentmethod.php
File metadata and controls
211 lines (168 loc) · 6.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
/**
* Authorize.net gateway payment method class.
*/
class Gateway_Authorizenet_Paymentmethod extends Gateway_Core_Paymentmethod
{
/**
* Finds a single instance.
*
* @param int|array $options Instance identifier or filter data.
*
* @return array|null
*/
public function find_one($options = array())
{
$customer_gateway_id = Service_Customer_Gateway::external_id($this->driver->customer, $this->driver->gateway);
if (!$customer_gateway_id) {
return null;
}
if (is_numeric($options)) {
$payment_method_id = $options;
}
$request = new AuthorizeNetCIM();
$response = $request->getCustomerPaymentProfile($customer_gateway_id, $payment_method_id);
if (!$response->isOk()) {
Log::error('Unable to retrieve Authorize.net payment method.');
return null;
}
$credit_card = $response->xml->paymentProfile->payment->creditCard;
return array(
'id' => $response->getPaymentProfileId(),
'customer_id' => $customer_gateway_id,
'account' => '****' . substr($credit_card->cardNumber, -4),
);
}
/**
* Creates a new payment method.
*
* @param array $data The data to use to create the payment method.
*
* @return int|bool
*/
public function create(array $data)
{
$customer_gateway_id = Service_Customer_Gateway::external_id($this->driver->customer, $this->driver->gateway);
if (!$customer_gateway_id) {
return false;
}
if (!$credit_card = Arr::get($data, 'account')) {
return false;
}
if (!$contact = Arr::get($data, 'contact')) {
return false;
}
$credit_card['expiration_month'] = (strlen($credit_card['expiration_month']) == 2) ? $credit_card['expiration_month'] : '0' . $credit_card['expiration_month'];
$credit_card['expiration_year'] = (strlen($credit_card['expiration_year']) == 4) ? $credit_card['expiration_year'] : '20' . $credit_card['expiration_year'];
if (!$this->auth($credit_card)) {
return false;
}
$request = new AuthorizeNetCIM();
$payment_profile = new AuthorizeNetPaymentProfile();
$payment_profile->payment->creditCard->cardNumber = preg_replace('/\D+/', '', $credit_card['number']);
$payment_profile->payment->creditCard->expirationDate = $credit_card['expiration_year'] . '-' . $credit_card['expiration_month'];
//$payment_profile->payment->creditCard->cardCode = $credit_card['cvv_code'];
$payment_profile->billTo->firstName = Arr::get($contact, 'first_name', '');
$payment_profile->billTo->lastName = Arr::get($contact, 'last_name', '');
$payment_profile->billTo->address = Arr::get($contact, 'address', '') . Arr::get($contact, 'address2', '');
$payment_profile->billTo->city = Arr::get($contact, 'city', '');
$payment_profile->billTo->state = Arr::get($contact, 'state', '');
$payment_profile->billTo->zip = Arr::get($contact, 'zip', '');
$payment_profile->billTo->country = Arr::get($contact, 'country', '');
$payment_profile->billTo->phoneNumber = Arr::get($contact, 'phone', '');
$response = $request->createCustomerPaymentProfile($customer_gateway_id, $payment_profile);
if (!$response->isOk()) {
Log::error('Unable to create Authorize.net payment method.');
return false;
}
return $response->getPaymentProfileId();
}
/**
* Updates a payment method.
*
* @param array $data The data to use to update the payment method.
*
* @return bool
*/
public function update(array $data)
{
if (!$id = $this->id()) {
return false;
}
if (!$customer_gateway_id = $this->data('customer_id')) {
return false;
}
if (!$credit_card = Arr::get($data, 'account')) {
return false;
}
if (!$contact = Arr::get($data, 'contact')) {
return false;
}
$credit_card['expiration_month'] = (strlen($credit_card['expiration_month']) == 2) ? $credit_card['expiration_month'] : '0' . $credit_card['expiration_month'];
$credit_card['expiration_year'] = (strlen($credit_card['expiration_year']) == 4) ? $credit_card['expiration_year'] : '20' . $credit_card['expiration_year'];
if (!$this->auth($credit_card)) {
return false;
}
$request = new AuthorizeNetCIM();
$payment_profile = new AuthorizeNetPaymentProfile();
$payment_profile->payment->creditCard->cardNumber = preg_replace('/\D+/', '', $credit_card['number']);
$payment_profile->payment->creditCard->expirationDate = $credit_card['expiration_year'] . '-' . $credit_card['expiration_month'];
//$payment_profile->payment->creditCard->cardCode = $credit_card['cvv_code'];
$payment_profile->billTo->firstName = Arr::get($contact, 'first_name', '');
$payment_profile->billTo->lastName = Arr::get($contact, 'last_name', '');
$payment_profile->billTo->address = Arr::get($contact, 'address', '') . Arr::get($contact, 'address2', '');
$payment_profile->billTo->city = Arr::get($contact, 'city', '');
$payment_profile->billTo->state = Arr::get($contact, 'state', '');
$payment_profile->billTo->zip = Arr::get($contact, 'zip', '');
$payment_profile->billTo->country = Arr::get($contact, 'country', '');
$payment_profile->billTo->phoneNumber = Arr::get($contact, 'phone', '');
$response = $request->updateCustomerPaymentProfile($customer_gateway_id, $id, $payment_profile);
if (!$response->isOk()) {
Log::error('Unable to update Authorize.net payment method.');
return false;
}
return true;
}
/**
* Deletes a payment method.
*
* @return bool
*/
public function delete()
{
if (!$id = $this->id()) {
return false;
}
if (!$customer_gateway_id = $this->data('customer_id')) {
return false;
}
$request = new AuthorizeNetCIM();
$response = $request->deleteCustomerPaymentProfile($customer_gateway_id, $id);
if (!$response->isOk()) {
Log::error('Unable to delete Authorize.net payment method.');
return false;
}
return true;
}
/**
* Validates the provided credit card data by posting a temporary $1.00 authorization charge.
*
* @param array $data The credit card data to use to verify.
*
* @return bool
*/
public function auth(array $data)
{
$authorize_aim = new AuthorizeNetAIM();
$authorize_aim->amount = 1.00;
$authorize_aim->card_num = $data['number'];
$authorize_aim->exp_date = $data['expiration_month'] . '/' . $data['expiration_year'];
$authorize_aim->allow_partial_auth = true;
$response = $authorize_aim->authorizeOnly();
if ($response->approved == true) {
return true;
}
Log::error('Authorize.net auth transaction failed.');
return false;
}
}