forked from ruskid/yii2-stripe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StripeForm.php
324 lines (295 loc) · 10.9 KB
/
StripeForm.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
<?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
*
* @author Victor Demin <[email protected]>
*/
class StripeForm 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;
//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
if (!isset($this->stripeRequestHandler)) {
$this->stripeRequestHandler = 'jQuery(function($) {
$("#' . $this->options['id'] . '").submit(function(event) {
var $form = $(this);
$form.find("button").prop("disabled", true);
Stripe.card.createToken($form, stripeResponseHandler);
return false;
});
});';
}
}
/**
* Will show the Stripe's simple form modal
*/
public function run() {
$this->registerFormScripts();
if ($this->applyJqueryPaymentFormat || $this->applyJqueryPaymentValidation) {
$this->registerJqueryPaymentScripts();
}
}
/**
* 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);
}
if ($this->applyJqueryPaymentValidation) {
/* $js = "jQuery(function($) {
$.fn.toggleInputError = function(erred) {
this.parent('.form-group').toggleClass('has-error', erred);
return this;
};
$('form').submit(function(e) {
e.preventDefault();
var cardType = $.payment.cardType($('.cc-number').val());
$('.cc-number').toggleInputError(!$.payment.validateCardNumber($('.cc-number').val()));
$('.cc-exp').toggleInputError(!$.payment.validateCardExpiry($('.cc-exp').payment('cardExpiryVal')));
$('.cc-cvc').toggleInputError(!$.payment.validateCardCVC($('.cc-cvc').val(), cardType));
$('.cc-brand').text(cardType);
$('.validation').removeClass('text-danger text-success');
$('.validation').addClass($('.has-error').length ? 'text-danger' : 'text-success');
});
});"; */
}
}
/**
* Will generate card number input
* @param array $options
* @return string genetared input tag
*/
public function numberInput($options = []) {
if (empty($options)) {
$options = [
'id' => self::NUMBER_ID,
'class' => 'form-control',
'autocomplete' => self::AUTO_CC_ATTR,
'placeholder' => '•••• •••• •••• ••••',
'required' => true,
'type' => 'tel',
'size' => 20
];
} else {
StripeHelper::secCheck($options);
}
$options['data-stripe'] = self::NUMBER_ID;
return Html::input('text', null, null, $options);
}
/**
* Will generate cvc input
* @param array $options
* @return string genetared input tag
*/
public function cvcInput($options = []) {
if (empty($options)) {
$options = [
'id' => self::CVC_ID,
'class' => 'form-control',
'autocomplete' => 'off',
'placeholder' => '•••',
'required' => true,
'type' => 'tel',
'size' => 4
];
} else {
StripeHelper::secCheck($options);
}
$options['data-stripe'] = self::CVC_ID;
return Html::input('text', null, null, $options);
}
/**
* Will generate year input
* @param array $options
* @return string genetared input tag
*/
public function yearInput($options = []) {
if (empty($options)) {
$options = [
'id' => self::YEAR_ID,
'class' => 'form-control',
'autocomplete' => self::AUTO_YEAR_ATTR,
'placeholder' => '••••',
'required' => true,
'type' => 'tel',
'maxlength' => 4,
'size' => 4
];
} else {
StripeHelper::secCheck($options);
}
$options['data-stripe'] = self::YEAR_ID;
return Html::input('text', null, null, $options);
}
/**
* Will generate month input
* @param array $options
* @return string genetared input tag
*/
public function monthInput($options = []) {
if (empty($options)) {
$options = [
'id' => self::MONTH_ID,
'class' => 'form-control',
'autocomplete' => self::AUTO_MONTH_ATTR,
'placeholder' => '••',
'required' => true,
'type' => 'tel',
'maxlength' => 2,
'size' => 2
];
} else {
StripeHelper::secCheck($options);
}
$options['data-stripe'] = self::MONTH_ID;
return Html::input('text', null, null, $options);
}
/**
* Will generate month and year input. Like in Jquery Payment example.
* @param array $options
* @return string genetared input tag
*/
public function monthAndYearInput($options = []) {
if (empty($options)) {
$options = [
'id' => self::MONTH_YEAR_ID,
'class' => 'form-control',
'autocomplete' => self::AUTO_EXP_ATTR,
'placeholder' => '•• / ••',
'required' => true,
'type' => 'tel',
];
} else {
StripeHelper::secCheck($options);
}
$options['data-stripe'] = self::MONTH_YEAR_ID;
return Html::input('text', null, null, $options);
}
}