Skip to content

Commit

Permalink
PaymentControl uses whole payment element instead of just card element
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinMystikJonas committed Mar 19, 2024
1 parent 4352bb1 commit 9f9348d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/MetisFW/Stripe/UI/PaymentControl.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function getTemplateFilePath() {
}

public function handleSuccess() {
$paymentIntentId = $this->getPresenter()->getParameter('paymentIntentId');
$paymentIntentId = $this->getPresenter()->getParameter('paymentIntentId') ?? $this->getPresenter()->getParameter('payment_intent');

try {
$paidPayment = $this->operation->handleSuccess($paymentIntentId);
Expand Down
45 changes: 38 additions & 7 deletions src/MetisFW/Stripe/UI/templates/PaymentControl.latte
Original file line number Diff line number Diff line change
@@ -1,9 +1,40 @@
<div n:attr="(expand)$attrs">
<input id="card-name" type="text">
<div id="card-element"></div>
<button id="card-button" data-secret="{$clientSecret}">{$text}</button>
</div>
<div id="stripe-payment-el-{$elementId}"></div>
<button id="stripe-payment-button-{$elementId}" data-secret="{$clientSecret}">{$text}</button>

<script>
var stripePaymentId = "stripe-payment-el-" + {$elementId};
var stripeButtonId = "stripe-payment-button-" + {$elementId};
var stripePaymentEl = document.getElementById(stripePaymentId);
var stripeButtonEl = document.getElementById(stripeButtonId);
var stripe = Stripe({$publicApiKey});
var elements = stripe.elements();
</script>
var stripeElements = stripe.elements({ clientSecret: {$clientSecret} });
var stripePayment = stripeElements.create("payment");
stripePayment.mount("#"+stripePaymentId);
stripeButtonEl.addEventListener('click', function(ev) {
ev.preventDefault();
stripeElements.submit();
stripe.confirmPayment({
elements: stripeElements,
clientSecret: {$clientSecret},
confirmParams: {
return_url: {$successLink}
}
}).then(function(result) {
if (result.error) {
console.log("Stripe error:" + result.error.message);
} else {
console.log("Stripe result: Payment " + result.paymentIntent.status)
if (result.paymentIntent.status === 'succeeded') {
window.location.href = {$successLink} + "&paymentIntentId=" + result.paymentIntent.id;
} else {
console.log("payment error")
}
}
});
});
</script>

0 comments on commit 9f9348d

Please sign in to comment.