forked from ruskid/yii2-stripe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
StripeCheckoutCustom.php
133 lines (115 loc) · 3.99 KB
/
StripeCheckoutCustom.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
<?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 checkout class.
* https://stripe.com/docs/checkout#integration-custom
*
* @author Victor Demin <[email protected]>
*/
class StripeCheckoutCustom extends StripeCheckout {
/**
* Custom button options
* @var array
*/
public $buttonOptions = ['class' => 'btn btn-lg btn-success'];
/**
* @var boolean whether the button label should be HTML-encoded.
*/
public $encodeLabel = true;
/**
* @see Stripe
* The callback to invoke when the Checkout process is complete. function(token) token is the token object created.
* token.id can be used to create a charge or customer. token.email contains the email address entered by the user.
*
* new JsExpression('function(token) {
* alert(token.id);
* alert(token.email);
* }');
*
* @var JsExpression
*/
public $tokenFunction;
/**
* @see Stripe. function() The callback to invoke when Checkout is opened (not supported in IE6 and IE7).
* @var JsExpression
*/
public $openedFunction;
/**
* @see Stripe. function() The callback to invoke when Checkout is closed (not supported in IE6 and IE7).
* @var JsExpression
*/
public $closedFunction;
/**
* @see Init extension default
*/
public function init() {
if (!isset($this->buttonOptions['id'])) {
$this->buttonOptions['id'] = $this->getId();
}
if (!isset($this->tokenFunction)) {
$this->tokenFunction = new JsExpression('function(token) { alert("Define your token handler"); }');
}
if (!isset($this->openedFunction)) {
$this->openedFunction = new JsExpression('function() { }');
}
if (!isset($this->closedFunction)) {
$this->closedFunction = new JsExpression('function() { }');
}
parent::init();
}
/**
* Will show the Stripe's simple form modal
*/
public function run() {
$this->registerScripts();
echo $this->generateButton();
}
/**
* Will register the scripts.
*/
private function registerScripts() {
$view = $this->getView();
$view->registerJsFile($this->stripeJs, ['position' => \yii\web\View::POS_END]);
$js = "var handler = StripeCheckout.configure({
key: '" . Yii::$app->stripe->publicKey . "',
token: " . $this->tokenFunction . "
});";
$view->registerJs($js);
$js = 'jQuery("#' . $this->buttonOptions['id'] . '").on("click", function(e) {
handler.open({
name: "' . $this->name . '",
description: "' . $this->description . '",
amount: ' . $this->amount . ',
image: "' . $this->image . '",
currency: "' . $this->currency . '",
panelLabel: "' . $this->panelLabel . '",
zipCode: "' . $this->validateZipCode . '",
email: "' . $this->userEmail . '",
allowRememberMe: "' . $this->allowRemember . '",
opened: ' . $this->openedFunction . ',
closed: ' . $this->closedFunction . ',
});
e.preventDefault();
});';
$view->registerJs($js);
$js = 'jQuery("window").on("popstate", function(e) {
handler.close();
});';
$view->registerJs($js);
}
/**
* Will generate the pay button
* @return string
*/
private function generateButton() {
return Html::tag('button', $this->encodeLabel ? Html::encode($this->label) : $this->label, $this->buttonOptions);
}
}