From e9713439e2bfb46a033ae4f1f90c4c8b395da1a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20W=C3=B3jcik?= Date: Wed, 8 Jun 2016 14:33:45 +0200 Subject: [PATCH] fixed JS code for multiple checkout buttons on a single page --- StripeCheckoutCustom.php | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/StripeCheckoutCustom.php b/StripeCheckoutCustom.php index ff4f13b..ca70fee 100644 --- a/StripeCheckoutCustom.php +++ b/StripeCheckoutCustom.php @@ -57,6 +57,8 @@ class StripeCheckoutCustom extends StripeCheckout { */ public $closedFunction; + private static $handlerRegistered = false; + /** * @see Init extension default */ @@ -92,11 +94,18 @@ private function registerScripts() { $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); + if (!self::$handlerRegistered) { + $js = "var handler = StripeCheckout.configure({ + key: '" . Yii::$app->stripe->publicKey . "' + });"; + $view->registerJs($js); + $js = 'jQuery("window").on("popstate", function(e) { + handler.close(); + });'; + $view->registerJs($js); + + self::$handlerRegistered = true; + } $js = 'jQuery("#' . $this->buttonOptions['id'] . '").on("click", function(e) { handler.open({ @@ -109,17 +118,14 @@ private function registerScripts() { zipCode: "' . $this->validateZipCode . '", email: "' . $this->userEmail . '", allowRememberMe: "' . $this->allowRemember . '", + token: ' . $this->tokenFunction . ', opened: ' . $this->openedFunction . ', - closed: ' . $this->closedFunction . ', + closed: ' . $this->closedFunction . ' }); e.preventDefault(); });'; $view->registerJs($js); - $js = 'jQuery("window").on("popstate", function(e) { - handler.close(); - });'; - $view->registerJs($js); } /**