1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* "Abstract" class for a number of different payment |
4
|
|
|
* types allowing a user to pay for something on a site. |
5
|
|
|
* |
6
|
|
|
* |
7
|
|
|
* This can't be an abstract class because sapphire doesn't |
8
|
|
|
* support abstract DataObject classes. |
9
|
|
|
* |
10
|
|
|
* @package payment |
11
|
|
|
*/ |
12
|
|
|
class EcommercePayment_Stripe extends EcommercePayment |
|
|
|
|
13
|
|
|
{ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
private static $api_key_public = ""; |
|
|
|
|
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
private static $api_key_private = ""; |
|
|
|
|
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* set the required privacy link as you see fit... |
27
|
|
|
* also see: https://www.paymentexpress.com/About/Artwork_Downloads |
28
|
|
|
* also see: https://www.paymentexpress.com/About/About_DPS/Privacy_Policy |
29
|
|
|
* @var String |
30
|
|
|
*/ |
31
|
|
|
private static $stripe_logo_and_link = ' |
|
|
|
|
32
|
|
|
<div>Stripe Logos go here...</div> |
33
|
|
|
'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* we use yes / no as this is more reliable than a boolean value |
37
|
|
|
* for configs |
38
|
|
|
* @var String |
39
|
|
|
*/ |
40
|
|
|
private static $is_test = "yes"; |
|
|
|
|
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* we use yes / no as this is more reliable than a boolean value |
44
|
|
|
* for configs |
45
|
|
|
* @var boolean |
46
|
|
|
*/ |
47
|
|
|
private static $is_live = "no"; |
|
|
|
|
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Incomplete (default): Payment created but nothing confirmed as successful |
51
|
|
|
* Success: Payment successful |
52
|
|
|
* Failure: Payment failed during process |
53
|
|
|
* Pending: Payment awaiting receipt/bank transfer etc |
54
|
|
|
*/ |
55
|
|
|
private static $db = array( |
|
|
|
|
56
|
|
|
"StripeID" => "Varchar(64)", |
57
|
|
|
"CardNumber" => "Varchar(19)", |
58
|
|
|
"NameOnCard" => "Varchar(40)", |
59
|
|
|
"ExpiryDate" => "Varchar(4)", |
60
|
|
|
"CVVNumber" => "Varchar(3)", |
61
|
|
|
"Request" => "Text", |
62
|
|
|
"Response" => "Text", |
63
|
|
|
"IdemPotencyKey" => "Text" |
64
|
|
|
); |
65
|
|
|
|
66
|
|
|
private static $casting = array( |
|
|
|
|
67
|
|
|
"RequestDetails" => "HTMLText", |
68
|
|
|
"ResponseDetails" => "HTMLText" |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
private static $indexes = array( |
|
|
|
|
72
|
|
|
"StripeID" => true |
73
|
|
|
); |
74
|
|
|
|
75
|
|
|
public function getCMSFields() |
76
|
|
|
{ |
77
|
|
|
$fields = parent::getCMSFields(); |
78
|
|
|
$fields->addFieldToTab("Root.Debug", new ReadonlyField("ClassName")); |
79
|
|
|
$fields->addFieldToTab("Root.Debug", new LiteralField("Request", "<h2>Request</h2>".$this->getRequestDetails())); |
80
|
|
|
$fields->addFieldToTab("Root.Debug", new LiteralField("SeparatorForRequest", "<hr />")); |
81
|
|
|
$fields->addFieldToTab("Root.Debug", new LiteralField("Response", "<h2>Response</h2>".$this->myResponseDetails())); |
82
|
|
|
return $fields; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Return the payment form fields that should |
87
|
|
|
* be shown on the checkout order form for the |
88
|
|
|
* payment type. Example: for {@link DPSPayment}, |
89
|
|
|
* this would be a set of fields to enter your |
90
|
|
|
* credit card details. |
91
|
|
|
* |
92
|
|
|
* @return FieldList |
93
|
|
|
*/ |
94
|
|
|
public function getPaymentFormFields() |
95
|
|
|
{ |
96
|
|
|
$formHelper = $this->ecommercePaymentFormSetupAndValidationObject(); |
97
|
|
|
$fieldList = $formHelper->getCreditCardPaymentFormFields($this); |
98
|
|
|
$fieldList->insertBefore( |
99
|
|
|
new LiteralField("Stripe_Logo", $this->Config()->get("stripe_logo_and_link")), |
100
|
|
|
"EcommercePayment_Stripe_CreditCard" |
|
|
|
|
101
|
|
|
); |
102
|
|
|
return $fieldList; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Define what fields defined in {@link Order->getPaymentFormFields()} |
107
|
|
|
* should be required. |
108
|
|
|
* |
109
|
|
|
* @see DPSPayment->getPaymentFormRequirements() for an example on how |
110
|
|
|
* this is implemented. |
111
|
|
|
* |
112
|
|
|
* @return array |
113
|
|
|
*/ |
114
|
|
|
public function getPaymentFormRequirements() |
115
|
|
|
{ |
116
|
|
|
$formHelper = $this->ecommercePaymentFormSetupAndValidationObject(); |
117
|
|
|
return $formHelper->getCreditCardPaymentFormFieldsRequired($this); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* returns true if all the data is correct. |
122
|
|
|
* |
123
|
|
|
* @param array $data The form request data - see OrderForm |
124
|
|
|
* @param OrderForm $form The form object submitted on |
125
|
|
|
* |
126
|
|
|
* @return Boolean |
127
|
|
|
*/ |
128
|
|
|
public function validatePayment($data, $form) |
129
|
|
|
{ |
130
|
|
|
$formHelper = $this->ecommercePaymentFormSetupAndValidationObject(); |
131
|
|
|
return $formHelper->validateAndSaveCreditCardInformation($data, $form, $this); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Perform payment processing for the type of |
136
|
|
|
* payment. For example, if this was a credit card |
137
|
|
|
* payment type, you would perform the data send |
138
|
|
|
* off to the payment gateway on this function for |
139
|
|
|
* your payment subclass. |
140
|
|
|
* |
141
|
|
|
* This is used by {@link OrderForm} when it is |
142
|
|
|
* submitted. |
143
|
|
|
* |
144
|
|
|
* @param array $data The form request data - see OrderForm |
145
|
|
|
* @param OrderForm $form The form object submitted on |
146
|
|
|
* |
147
|
|
|
* @return EcommercePaymentResult |
|
|
|
|
148
|
|
|
*/ |
149
|
|
|
public function processPayment($data, $form) |
150
|
|
|
{ |
151
|
|
|
//get variables |
152
|
|
|
$this->retrieveVariables(); |
153
|
|
|
$this->instantiateAPI(); |
154
|
|
|
|
155
|
|
|
|
156
|
|
|
$requestData = array( |
157
|
|
|
'card' => $this->_processing_card, |
|
|
|
|
158
|
|
|
'amount' => $this->_processing_amount, |
159
|
|
|
'currency' => $this->_processing_currency, |
160
|
|
|
'statement_descriptor' => $this->_processing_statement_description, |
161
|
|
|
'metadata' => $this->_processing_metadata |
162
|
|
|
); |
163
|
|
|
|
164
|
|
|
//do stripe bit |
165
|
|
|
|
166
|
|
|
$responseData = \Stripe\Charge::create($requestData, $this->_processing_idempotency_key); |
|
|
|
|
167
|
|
|
|
168
|
|
|
//remove card for security reasons |
169
|
|
|
$this->removeCardDetails(); |
170
|
|
|
$allDetails["card"]["number"] = $this->_processing_truncated_card; |
|
|
|
|
171
|
|
|
|
172
|
|
|
//now we can save the details: |
173
|
|
|
$this->recordTransaction($requestData, $responseData); |
174
|
|
|
|
175
|
|
|
if ( |
176
|
|
|
$responseData && |
177
|
|
|
$responseData->status == "succeeded" |
178
|
|
|
) { |
179
|
|
|
$this->Status = "Success"; |
|
|
|
|
180
|
|
|
$returnObject = EcommercePayment_Success::create(); |
181
|
|
|
} else { |
182
|
|
|
$this->Status = "Failure"; |
|
|
|
|
183
|
|
|
$returnObject = EcommercePayment_Failure::create(); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
$this->write(); |
187
|
|
|
return $returnObject; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* |
192
|
|
|
* @return string (HTML) |
193
|
|
|
*/ |
194
|
|
|
public function getRequestDetails() |
195
|
|
|
{ |
196
|
|
|
return "<pre>".print_r(unserialize($this->Request), 1)."</pre>"; |
|
|
|
|
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* |
201
|
|
|
* @return string (HTML) |
202
|
|
|
*/ |
203
|
|
|
public function myResponseDetails() |
204
|
|
|
{ |
205
|
|
|
return "<pre>".print_r(unserialize($this->Response), 1)."</pre>"; |
|
|
|
|
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* @var Order |
210
|
|
|
*/ |
211
|
|
|
protected $_processing_order = null; |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* @var float |
215
|
|
|
*/ |
216
|
|
|
protected $_processing_amount = 0; |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @var string |
220
|
|
|
*/ |
221
|
|
|
protected $_processing_currency = 0; |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @var int |
225
|
|
|
*/ |
226
|
|
|
protected $_processing_year = 0; |
227
|
|
|
|
228
|
|
|
/** |
229
|
|
|
* @var int |
230
|
|
|
*/ |
231
|
|
|
protected $_processing_month = 0; |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* @var string |
235
|
|
|
*/ |
236
|
|
|
protected $_processing_statement_description = ""; |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* @var array |
240
|
|
|
*/ |
241
|
|
|
protected $_processing_metadata = array(); |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* |
245
|
|
|
* |
246
|
|
|
*/ |
247
|
|
|
protected function retrieveVariables() |
248
|
|
|
{ |
249
|
|
|
if (!$this->_processing_idempotency_key) { |
|
|
|
|
250
|
|
|
if ($this->ID) { |
251
|
|
|
$hash = hash("SHA1", $this->ID."_".$this->ClassName); |
252
|
|
|
} else { |
253
|
|
|
$hash = hash("SHA1", rand(0, 9999999999999999)); |
254
|
|
|
} |
255
|
|
|
$this->_processing_idempotency_key = array("idempotency_key" => $hash); |
|
|
|
|
256
|
|
|
$this->_processing_check_code = substr($hash, 0, 22); |
|
|
|
|
257
|
|
|
if ($this->OrderID) { |
|
|
|
|
258
|
|
|
$order = $this->Order(); |
|
|
|
|
259
|
|
|
} else { |
260
|
|
|
$order = ShoppingCart::current_order(); |
261
|
|
|
} |
262
|
|
|
$this->_processing_order = $order; |
263
|
|
|
$this->_processing_member = $this->_processing_order->Member(); |
|
|
|
|
264
|
|
|
$this->_processing_currency = strtolower($this->Amount->Currency); |
|
|
|
|
265
|
|
|
$this->_processing_amount = $this->Amount->Amount * 100; |
|
|
|
|
266
|
|
|
$this->_processing_month = substr($this->ExpiryDate, 0, 2); |
|
|
|
|
267
|
|
|
$this->_processing_year = substr($this->ExpiryDate, 2, 2); |
|
|
|
|
268
|
|
|
$this->_processing_metadata = array( |
269
|
|
|
"OrderID" => $this->_processing_order->ID, |
270
|
|
|
"EcommercePaymentID" => $this->ID |
271
|
|
|
); |
272
|
|
|
$this->_processing_statement_description = $this->_processing_check_code; |
|
|
|
|
273
|
|
|
if ($this->hasFullCardNumber()) { |
274
|
|
|
$this->_processing_card = array( |
|
|
|
|
275
|
|
|
'number' => $this->CardNumber, |
|
|
|
|
276
|
|
|
'exp_month' => $this->_processing_month, |
277
|
|
|
'exp_year' => $this->_processing_year, |
278
|
|
|
'name' => $this->NameOnCard |
|
|
|
|
279
|
|
|
); |
280
|
|
|
$this->_processing_truncated_card = substr($this->CardNumber, 12, 4); |
|
|
|
|
281
|
|
|
$this->_processing_card_description = |
|
|
|
|
282
|
|
|
"...".$this->_processing_truncated_card. |
|
|
|
|
283
|
|
|
"; exp: ".$this->_processing_month."-".$this->_processing_year. |
284
|
|
|
"; name: ".$this->NameOnCard; |
|
|
|
|
285
|
|
|
} |
286
|
|
|
} |
287
|
|
|
} |
288
|
|
|
|
289
|
|
|
/** |
290
|
|
|
* |
291
|
|
|
* |
292
|
|
|
*/ |
293
|
|
|
protected function instantiateAPI() |
294
|
|
|
{ |
295
|
|
|
$api = $this->Config()->get("api_key_private"); |
296
|
|
|
\Stripe\Stripe::setApiKey($api); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* remove the card details |
301
|
|
|
* for securityu reasons |
302
|
|
|
*/ |
303
|
|
|
protected function removeCardDetails() |
304
|
|
|
{ |
305
|
|
|
if ($this->hasFullCardNumber()) { |
306
|
|
|
$this->CardNumber = $this->_processing_truncated_card; |
|
|
|
|
307
|
|
|
$this->_processing_card = null; |
|
|
|
|
308
|
|
|
} |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* is the full credit card recorded? |
313
|
|
|
* @return boolean |
314
|
|
|
*/ |
315
|
|
|
protected function hasFullCardNumber() |
316
|
|
|
{ |
317
|
|
|
return strlen($this->CardNumber) > 12; |
|
|
|
|
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* @param mixed $requestData; |
|
|
|
|
322
|
|
|
* @param mixed $responseData; |
|
|
|
|
323
|
|
|
*/ |
324
|
|
|
protected function recordTransaction($requestData, $responseData) |
325
|
|
|
{ |
326
|
|
|
$this->Request = serialize($requestData); |
|
|
|
|
327
|
|
|
$this->Response = serialize($responseData); |
|
|
|
|
328
|
|
|
if ($responseData && isset($responseData->id)) { |
329
|
|
|
$this->StripeID = $responseData->id; |
|
|
|
|
330
|
|
|
//$this->IdemPotencyKey = $responseData->getLastReponse()->header["Idempotency-Key"]; |
|
|
|
|
331
|
|
|
} |
332
|
|
|
if ($this->_processing_amount) { |
333
|
|
|
//no idea why we need this!!! |
334
|
|
|
$this->Amount->Amount = $this->_processing_amount / 100; |
|
|
|
|
335
|
|
|
} |
336
|
|
|
$this->Message = _t("EcommercePayment_Stripe", "TRANSACTION_CODE").": ".$this->_processing_check_code; |
|
|
|
|
337
|
|
|
} |
338
|
|
|
} |
339
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.