Skip to content

Commit

Permalink
Merge pull request #14 from bartoffw/fix_multiple_checkouts
Browse files Browse the repository at this point in the history
fixed JS code for multiple checkout buttons on a single page
  • Loading branch information
ruskid committed Jun 8, 2016
2 parents fb7fde4 + e971343 commit db5b5ec
Showing 1 changed file with 16 additions and 10 deletions.
26 changes: 16 additions & 10 deletions StripeCheckoutCustom.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ class StripeCheckoutCustom extends StripeCheckout {
*/
public $closedFunction;

private static $handlerRegistered = false;

/**
* @see Init extension default
*/
Expand Down Expand Up @@ -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({
Expand All @@ -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);
}

/**
Expand Down

0 comments on commit db5b5ec

Please sign in to comment.