Skip to content

Commit db5b5ec

Browse files
committed
Merge pull request #14 from bartoffw/fix_multiple_checkouts
fixed JS code for multiple checkout buttons on a single page
2 parents fb7fde4 + e971343 commit db5b5ec

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

StripeCheckoutCustom.php

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ class StripeCheckoutCustom extends StripeCheckout {
5757
*/
5858
public $closedFunction;
5959

60+
private static $handlerRegistered = false;
61+
6062
/**
6163
* @see Init extension default
6264
*/
@@ -92,11 +94,18 @@ private function registerScripts() {
9294

9395
$view->registerJsFile($this->stripeJs, ['position' => \yii\web\View::POS_END]);
9496

95-
$js = "var handler = StripeCheckout.configure({
96-
key: '" . Yii::$app->stripe->publicKey . "',
97-
token: " . $this->tokenFunction . "
98-
});";
99-
$view->registerJs($js);
97+
if (!self::$handlerRegistered) {
98+
$js = "var handler = StripeCheckout.configure({
99+
key: '" . Yii::$app->stripe->publicKey . "'
100+
});";
101+
$view->registerJs($js);
102+
$js = 'jQuery("window").on("popstate", function(e) {
103+
handler.close();
104+
});';
105+
$view->registerJs($js);
106+
107+
self::$handlerRegistered = true;
108+
}
100109

101110
$js = 'jQuery("#' . $this->buttonOptions['id'] . '").on("click", function(e) {
102111
handler.open({
@@ -109,17 +118,14 @@ private function registerScripts() {
109118
zipCode: "' . $this->validateZipCode . '",
110119
email: "' . $this->userEmail . '",
111120
allowRememberMe: "' . $this->allowRemember . '",
121+
token: ' . $this->tokenFunction . ',
112122
opened: ' . $this->openedFunction . ',
113-
closed: ' . $this->closedFunction . ',
123+
closed: ' . $this->closedFunction . '
114124
});
115125
e.preventDefault();
116126
});';
117127
$view->registerJs($js);
118128

119-
$js = 'jQuery("window").on("popstate", function(e) {
120-
handler.close();
121-
});';
122-
$view->registerJs($js);
123129
}
124130

125131
/**

0 commit comments

Comments
 (0)