-
Notifications
You must be signed in to change notification settings - Fork 18
/
StripeFormV2.php
381 lines (341 loc) · 13 KB
/
StripeFormV2.php
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
<?php
/**
* @copyright Copyright Victor Demin, 2014
* @license https://github.com/ruskid/yii2-stripe/LICENSE
* @link https://github.com/ruskid/yii2-stripe#readme
*/
namespace ruskid\stripe;
use Yii;
use yii\helpers\Html;
use yii\web\JsExpression;
/**
* Yii stripe custom form.
* https://stripe.com/docs/tutorials/forms
*
*
* !!! DEPRECIATION WARNING !!!
*
* STRONG CUSTOMER AUTHENTICATION
*
* Starting in September 2019, a new regulation called Strong Customer Authentication (SCA) requires businesses in Europe to request additional authentication for online payments. Stripe Checkout, Elements, and Terminal all fully support SCA—including exemption logic—and ensures that you only ask customers to provide additional authentication when strictly necessary.
*
* Learn more about SCA and how it might impact your business.
*
*
* @author Victor Demin <demmbox@gmail.com>
*/
class StripeFormV2 extends \yii\widgets\ActiveForm {
/**
* @see Stripe's javascript location
* @var string url to stripe's javascript
*/
public $stripeJs = 'https://js.stripe.com/v2/';
/**
* Js Expression that will handle the response.
*
* If not set the default behavior will be used:
* function stripeResponseHandler(status, response) {
* var $form = $('#payment-form');
* if (response.error) {
* // Show the errors on the form
* $form.find('.payment-errors').text(response.error.message);
* $form.find('button').prop('disabled', false);
* } else {
* // response contains id and card, which contains additional card details
* var token = response.id;
* // Insert the token into the form so it gets submitted to the server
* $form.append($('<input type="hidden" name="stripeToken" />').val(token));
* // and submit
* $form.get(0).submit();
* }
* }
*
* @var JsExpression
*/
public $stripeResponseHandler;
/**
* Js Expression that will handle the request.
*
* If not set the default behavior will be used:
* jQuery(function($) {
* $('#payment-form').submit(function(event) {
* var $form = $(this);
* // Disable the submit button to prevent repeated clicks
* $form.find('button').prop('disabled', true);
* Stripe.card.createToken($form, stripeResponseHandler);
* // Prevent the form from submitting with the default action
* return false;
* });
* });
*
* @var JsExpression
*/
public $stripeRequestHandler;
/**
* Input id and name tags of the hidden token input that will be sent to PayAction.
* @var string
*/
public $tokenInputName = 'stripeToken';
/**
* If the default behavior for the response is used, then you can set the id of error's container.
* Note! this property is useless if you set your own response handler.
* @var string
*/
public $errorContainerId = "payment-errors";
/**
* Apply Jquery Payment format to the inputs
* @see https://github.com/stripe/jquery.payment.
* @var boolean
*/
public $applyJqueryPaymentFormat = true;
/**
* Perform Jquery Payment client validation.
* @var boolean
*/
public $applyJqueryPaymentValidation = true;
/**
* Class applied to .form-group when Jquery Payment Validation didn't pass
* @var string
*/
public $errorClass = 'has-error';
/**
* Brand container used when Jquery Payment identify the brand by card number
* @var string
*/
public $brandContainerId = 'cc-brand';
/**
* Used if jquery payment validation is true. Called when card brand is identified
* @var JsExpression|string
*/
public $brandIdentificationHandler;
//Stripe constants
const NUMBER_ID = 'number';
const CVC_ID = 'cvc';
const MONTH_ID = 'exp-month';
const YEAR_ID = 'exp-year';
const MONTH_YEAR_ID = 'exp-month-year'; //actually not stripe =)
//Autofill spec. @see https://html.spec.whatwg.org/multipage/forms.html
const AUTO_CC_ATTR = 'cc-number';
const AUTO_EXP_ATTR = 'cc-exp';
const AUTO_MONTH_ATTR = 'cc-exp-month';
const AUTO_YEAR_ATTR = 'cc-exp-year';
/**
* @see Init extension default
*/
public function init() {
parent::init();
//Set default response behavior
if (!isset($this->stripeResponseHandler)) {
$this->stripeResponseHandler = 'function stripeResponseHandler(status, response) {
var $form = $("#' . $this->options['id'] . '");
if (response.error) {
$form.find("#' . $this->errorContainerId . '").text(response.error.message);
$form.find("button").prop("disabled", false);
} else {
var token = response.id;
$form.append($("<input type=\"hidden\" name=\"' . $this->tokenInputName . '\" id=\"' . $this->tokenInputName . '\" />").val(token));
$form.get(0).submit();
}
};';
}
//Set default request behavior when no client validation applied
if (!isset($this->stripeRequestHandler)) {
$this->stripeRequestHandler = 'jQuery(function($) {
$("#' . $this->options['id'] . '").submit(function(event) {
event.preventDefault();
});
});';
}
if($this->applyJqueryPaymentValidation && !isset($this->brandIdentificationHandler)){
$this->brandIdentificationHandler = 'function(cardType){
$("#' . $this->brandContainerId . '").text(cardType);
}';
}
}
/**
* Will show the Stripe's simple form modal
*/
public function run() {
$this->registerFormScripts();
if ($this->applyJqueryPaymentFormat || $this->applyJqueryPaymentValidation) {
$this->registerJqueryPaymentScripts();
}
return parent::run();
}
/**
* Will register mandatory javascripts to work
*/
public function registerFormScripts() {
$view = $this->getView();
$view->registerJsFile($this->stripeJs, ['position' => \yii\web\View::POS_HEAD]);
$js = "Stripe.setPublishableKey('" . Yii::$app->stripe->publicKey . "');";
$view->registerJs($js, \yii\web\View::POS_BEGIN);
//form scripts
$view->registerJs($this->stripeResponseHandler, \yii\web\View::POS_READY);
$view->registerJs($this->stripeRequestHandler, \yii\web\View::POS_READY);
}
/**
* Will register Jquery Payment scripts
*/
public function registerJqueryPaymentScripts() {
$view = $this->getView();
JqueryPaymentAsset::register($view);
if ($this->applyJqueryPaymentFormat) {
$js = "jQuery(function($) {
$('input[data-stripe=" . self::NUMBER_ID . "]').payment('formatCardNumber');
$('input[data-stripe=" . self::CVC_ID . "]').payment('formatCardCVC');
$('input[data-stripe=" . self::MONTH_YEAR_ID . "]').payment('formatCardExpiry');
$('input[data-stripe=" . self::MONTH_ID . "]').payment('restrictNumeric');
$('input[data-stripe=" . self::YEAR_ID . "]').payment('restrictNumeric');
});";
$view->registerJs($js);
}
//Jquery client validation submit
if ($this->applyJqueryPaymentValidation) {
$js = 'jQuery(function($) {
$.fn.toggleInputError = function(erred) {
this.closest(".form-group").toggleClass("' . $this->errorClass . '", erred);
return this;
};
$("#' . $this->options['id'] . ' :submit").on("click", function(e) {
var $form = $("#' . $this->options['id'] . '");
var $number = $("input[data-stripe=' . self::NUMBER_ID . ']");
var $cvc = $("input[data-stripe=' . self::CVC_ID . ']");
var $exp = $("input[data-stripe=' . self::MONTH_YEAR_ID . ']");
var $month = $("input[data-stripe=' . self::MONTH_ID . ']");
var $year = $("input[data-stripe=' . self::YEAR_ID . ']");
var cardType = $.payment.cardType($number.val());
(' . $this->brandIdentificationHandler .')(cardType);
var validCard = $.payment.validateCardNumber($number.val());
$number.toggleInputError(!validCard);
var validCVC = $.payment.validateCardCVC($cvc.val(), cardType);
$cvc.toggleInputError(!validCVC);
if ($exp.length) {
var validExpiry = $.payment.validateCardExpiry($exp.payment("cardExpiryVal"));
$exp.toggleInputError(!validExpiry);
var fullDate = $exp.val();
var res = fullDate.split(" / ", 2);
$month.val(res[0]);
$year.val(res[1]);
}else{
var validExpiry = $.payment.validateCardExpiry($month.val(), $year.val());
$month.toggleInputError(!validExpiry);
$year.toggleInputError(!validExpiry);
}
if(!validCard || !validCVC || !validExpiry){
e.preventDefault();
return false;
}else{
return true;
}
});
});';
$view->registerJs($js);
}
}
/**
* Will generate card number input
* @param array $options
* @return string genetared input tag
*/
public function numberInput($options = []) {
$defaultOptions = [
'id' => self::NUMBER_ID,
'class' => 'form-control',
'autocomplete' => self::AUTO_CC_ATTR,
'placeholder' => '•••• •••• •••• ••••',
'required' => true,
'type' => 'tel',
'size' => 20
];
$mergedOptions = array_merge($defaultOptions, $options);
StripeHelper::secCheck($mergedOptions);
$mergedOptions['data-stripe'] = self::NUMBER_ID;
return Html::input('text', null, null, $mergedOptions);
}
/**
* Will generate cvc input
* @param array $options
* @return string genetared input tag
*/
public function cvcInput($options = []) {
$defaultOptions = [
'id' => self::CVC_ID,
'class' => 'form-control',
'autocomplete' => 'off',
'placeholder' => '•••',
'required' => true,
'type' => 'tel',
'size' => 4
];
$mergedOptions = array_merge($defaultOptions, $options);
StripeHelper::secCheck($mergedOptions);
$mergedOptions['data-stripe'] = self::CVC_ID;
return Html::input('text', null, null, $mergedOptions);
}
/**
* Will generate year input
* @param array $options
* @return string genetared input tag
*/
public function yearInput($options = []) {
$defaultOptions = [
'id' => self::YEAR_ID,
'class' => 'form-control',
'autocomplete' => self::AUTO_YEAR_ATTR,
'placeholder' => '••••',
'required' => true,
'type' => 'tel',
'maxlength' => 4,
'size' => 4
];
$mergedOptions = array_merge($defaultOptions, $options);
StripeHelper::secCheck($mergedOptions);
$mergedOptions['data-stripe'] = self::YEAR_ID;
return Html::input('text', null, null, $mergedOptions);
}
/**
* Will generate month input
* @param array $options
* @return string genetared input tag
*/
public function monthInput($options = []) {
$defaultOptions = [
'id' => self::MONTH_ID,
'class' => 'form-control',
'autocomplete' => self::AUTO_MONTH_ATTR,
'placeholder' => '••',
'required' => true,
'type' => 'tel',
'maxlength' => 2,
'size' => 2
];
$mergedOptions = array_merge($defaultOptions, $options);
StripeHelper::secCheck($mergedOptions);
$mergedOptions['data-stripe'] = self::MONTH_ID;
return Html::input('text', null, null, $mergedOptions);
}
/**
* Will generate month and year input with 2 hidden inputs for month and year values.
* @param array $options
* @return string genetared input tag
*/
public function monthAndYearInput($options = []) {
$defaultOptions = [
'id' => self::MONTH_YEAR_ID,
'class' => 'form-control',
'autocomplete' => self::AUTO_EXP_ATTR,
'placeholder' => '•• / ••',
'required' => true,
'type' => 'tel',
];
$mergedOptions = array_merge($defaultOptions, $options);
StripeHelper::secCheck($mergedOptions);
$mergedOptions['data-stripe'] = self::MONTH_YEAR_ID;
$inputs = Html::input('text', null, null, $mergedOptions);
//Append hidden year and month inputs that will get value from mixed and send to stripe
$inputs = $inputs . $this->monthInput(['type' => 'hidden']);
$inputs = $inputs . $this->yearInput(['type' => 'hidden']);
return $inputs;
}
}