I was able to get the Stripe card input to work, but NOT able to get the stripe.createPaymentMethod
method to respond.
I believe this is due to locker service or CORS.
Failing Solution
Steps:
I started by downloading the JavaScript: https://js.stripe.com/v3/
Added it as a Static Resource
called stripe
create LWC:
LWC JavaScript
import { LightningElement } from "lwc";
import { loadScript } from "lightning/platformResourceLoader";
import StripeJS from "@salesforce/resourceUrl/stripe";
export default class StripeTest extends LightningElement {
initialised = false;
stripe;
cardElement;
async renderedCallback() {
if (this.initialised) {
return;
}
this.initialised = true;
await Promise.all([loadScript(this, StripeJS)]).catch(error => console.error(error));
const cardWrapper = this.template.querySelector("div.cardWrapper");
// setup Stripe.js and Elements
this.stripe = Stripe("pk_test_XXXXXXXXXXXXXXXXXXXXXXXXXXXX");
console.log(this.stripe);
const elements = this.stripe.elements();
// setup card Element
this.cardElement = elements.create("card", {});
this.cardElement.mount(cardWrapper);
}
async handlePay() {
const payload = await this.stripe.createPaymentMethod({
type: "card",
card: this.cardElement
}); // THIS DOES NOT WORK
console.log("[PaymentMethod]", payload); // THIS NEVER RUNS
}
}
LWC template
<template>
<div class="cardWrapper" lwc:dom="manual"></div>
<button onclick={handlePay}>Pay</button>
</template>
Working Solution
In the end I used a Visualforce Page
, example below:
<apex:page applyBodyTag="false" applyHtmlTag="false" showHeader="false" sidebar="false" standardStylesheets="false" lightningStyleSheets="true">
<html>
<head>
<script type="text/javascript" src="//js.stripe.com/v3/"></script>
<apex:slds />
</head>
<body>
<script>
window.onload = onLoad;
async function onLoad() {
const cardWrapper = document.querySelector("div.cardWrapper");
const button = document.querySelector("button");
const stripe = await Stripe('pk_test_XXXXXXXXXXXXXXXXXXXX');
const elements = stripe.elements();
this.cardElement = elements.create("card", {});
this.cardElement.mount(cardWrapper);
button.addEventListener('click', async () => {
const payload = await stripe.createPaymentMethod({
type: 'card',
card: cardElement,
});
console.log('[PaymentMethod]', payload);
}, false);
}
</script>
<div class="slds-scope">
<div class="slds-p-around_medium">
<div class="cardWrapper"></div>
</div>
<button class="slds-button slds-button_brand">Pay</button>
</div>
</body>
</html>
</apex:page>