-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathStripeForm.php
163 lines (137 loc) · 4.81 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
<?php
/**
* @copyright Copyright Victor Demin, 2019
* @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\helpers\Json;
/**
* @author Victor Demin <[email protected]>
*/
class StripeForm extends \yii\widgets\ActiveForm {
/**
* @var string
*/
public $stripeJs = 'https://js.stripe.com/v3/';
/**
* @var array
*/
public $formEvents = [];
/**
* @var array
*/
public $jsVars = [
'stripe' => 'stripe',
'elements' => 'elements',
'card' => 'card',
'form' => 'form',
];
/**
* @var array
*/
public $elementsOptions = [];
/**
* @return string
* @author Victor Demin
*/
public function run() {
$this->registerStripeScripts();
$this->registerFormScripts();
return parent::run();
}
/**
* @author Victor Demin
*/
protected function registerStripeScripts() {
$view = $this->getView();
$view->registerJsFile($this->stripeJs, ['position' => \yii\web\View::POS_HEAD]);
$js = "var {$this->jsVars['stripe']} = Stripe('" . Yii::$app->stripe->publicKey . "');";
if (!empty($this->elementsOptions)) {
$js .= "var {$this->jsVars['elements']} = stripe.elements(" . Json::encode($this->elementsOptions) . ");";
} else {
$js .= "var {$this->jsVars['elements']} = stripe.elements();";
}
$view->registerJs($js, \yii\web\View::POS_BEGIN);
}
/**
* @author Victor Demin
*/
protected function registerFormScripts() {
$js = "var {$this->jsVars['form']} = document.getElementById('" . $this->id . "');";
foreach ($this->formEvents as $eventName => $handler) {
$js .= "{$this->jsVars['form']}.addEventListener('{$eventName}', $handler);";
}
$this->view->registerJs($js, \yii\web\View::POS_END);
}
/**
* @param array $elementOptions
* @param array $eventHandlers
* @param array $divOptions
* @return string
* @author Victor Demin
*/
public function cardInput(array $elementOptions = [], array $eventHandlers = [], $divOptions = []) {
$divOptions['id'] = $divOptions['id'] ?? $this->id . '_' . '_card_input';
$this->mountElement('card', $divOptions['id'], $elementOptions, $eventHandlers);
return Html::tag('div', '', $divOptions);
}
/**
* @param array $elementOptions
* @param array $eventHandlers
* @param array $divOptions
* @return string
* @author Victor Demin
*/
public function cardNumber(array $elementOptions = [], array $eventHandlers = [], $divOptions = []) {
$divOptions['id'] = $divOptions['id'] ?? $this->id . '_' . '_card_number';
$this->mountElement('cardNumber', $divOptions['id'], $elementOptions, $eventHandlers);
return Html::tag('div', '', $divOptions);
}
/**
* @param array $elementOptions
* @param array $eventHandlers
* @param array $divOptions
* @return string
* @author Victor Demin
*/
public function cardExpiry(array $elementOptions = [], array $eventHandlers = [], $divOptions = []) {
$divOptions['id'] = $divOptions['id'] ?? $this->id . '_' . '_card_expiry';
$this->mountElement('cardExpiry', $divOptions['id'], $elementOptions, $eventHandlers);
return Html::tag('div', '', $divOptions);
}
/**
* @param array $elementOptions
* @param array $eventHandlers
* @param array $divOptions
* @return string
* @author Victor Demin
*/
public function cardCvc(array $elementOptions = [], array $eventHandlers = [], $divOptions = []) {
$divOptions['id'] = $divOptions['id'] ?? $this->id . '_' . '_card_cvc';
$this->mountElement('cardCvc', $divOptions['id'], $elementOptions, $eventHandlers);
return Html::tag('div', '', $divOptions);
}
/**
* @param string $elementName
* @param $mountID
* @param array $elementOptions
* @param array $events
* @author Victor Demin
*/
public function mountElement(string $elementName, $mountID, array $elementOptions = [], array $events = []) {
$jsVarName = $this->jsVars[$elementName];
if (!empty($elementOptions)) {
$js = "var {$jsVarName} = elements.create('" . $elementName . "', " . Json::encode($elementOptions) . ");";
} else {
$js = "var {$jsVarName} = elements.create('" . $elementName . "');";
}
$js .= "{$jsVarName}.mount('#" . $mountID . "');";
foreach ($events as $eventName => $handler) {
$js .= "{$jsVarName}.addEventListener('{$eventName}', $handler);";
}
$this->view->registerJs($js, \yii\web\View::POS_END);
}
}