Skip to content

Commit

Permalink
allow passing of extra checkout session params from a template
Browse files Browse the repository at this point in the history
  • Loading branch information
i-just committed Apr 25, 2024
1 parent e761365 commit 65cae61
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/services/Checkout.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public function getCheckoutUrl(
string|User|null $user = null,
?string $successUrl = null,
?string $cancelUrl = null,
?array $params = null,
): string {
$customer = null;

Expand All @@ -68,7 +69,7 @@ public function getCheckoutUrl(
}
}

return $this->startCheckoutSession(array_values($lineItems), $customer, $successUrl, $cancelUrl);
return $this->startCheckoutSession(array_values($lineItems), $customer, $successUrl, $cancelUrl, $params);
}

/**
Expand Down Expand Up @@ -130,6 +131,7 @@ private function startCheckoutSession(
Customer|string|null $customer = null,
?string $successUrl = null,
?string $cancelUrl = null,
?array $params = null,
): ?string {
$stripe = Plugin::getInstance()->getApi()->getClient();

Expand All @@ -139,12 +141,17 @@ private function startCheckoutSession(
'cancel_url' => $cancelUrl,
'mode' => $this->getCheckoutMode($lineItems),
];

if ($customer instanceof Customer) {
$data['customer'] = $customer->stripeId;
} else {
$data['customer_email'] = $customer;
}

if ($params !== null) {
$data += $params;
}

// Trigger a 'beforeStartCheckoutSession' event
$event = new CheckoutSessionEvent([
'sessionData' => $data,
Expand Down
3 changes: 2 additions & 1 deletion src/web/twig/CraftVariableBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ public function stripeCheckoutUrl(
?string $customer = null,
?string $successUrl = null,
?string $cancelUrl = null,
?array $params = null,
): string {
return Plugin::getInstance()->getCheckout()->getCheckoutUrl($lineItems, $customer, $successUrl, $cancelUrl);
return Plugin::getInstance()->getCheckout()->getCheckoutUrl($lineItems, $customer, $successUrl, $cancelUrl, $params);
}
}

0 comments on commit 65cae61

Please sign in to comment.