Skip to content

Commit

Permalink
feat: add locale props for stripe initialization (#87)
Browse files Browse the repository at this point in the history
* feat: add locale props for stripe initialization

* feat: locale property documentation

* feat: add tests for locale property

Co-authored-by: Lucas Robin <lucas@wearestudium.com>
  • Loading branch information
k4lu-0p and Lucas Robin committed Jun 21, 2022
1 parent 536ad46 commit 44317a4
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 2 deletions.
11 changes: 11 additions & 0 deletions custom-elements.json
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,17 @@
"attribute": "invalid",
"reflects": true
},
{
"kind": "field",
"name": "locale",
"type": {
"text": "Stripe.StripeElementLocale"
},
"default": "auto",
"description": "Stripe locale to use (use browser locale by default)",
"attribute": "locale",
"reflects": true
},
{
"kind": "method",
"name": "createPaymentMethod",
Expand Down
9 changes: 7 additions & 2 deletions src/StripeBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { BreadcrumbController } from './breadcrumb-controller.js';
import { readonly } from './lib/read-only.js';
import { notify } from './lib/notify.js';
import { loadStripe } from '@stripe/stripe-js/pure.js';
import { StripeElementLocale } from '@stripe/stripe-js';

export const enum SlotName {
'stripe-elements' = 'stripe-elements-slot',
Expand Down Expand Up @@ -143,6 +144,10 @@ export class StripeBase extends LitElement {
@property({ type: String, attribute: 'stripe-account' })
stripeAccount: string;

/** Stripe locale to use */
@property({ type: String, attribute: 'locale' })
locale: StripeElementLocale = 'auto';

/* READ-ONLY FIELDS */

/* PAYMENT REPRESENTATIONS */
Expand Down Expand Up @@ -392,12 +397,12 @@ export class StripeBase extends LitElement {
* Initializes Stripe and elements.
*/
private async initStripe(): Promise<void> {
const { publishableKey, stripeAccount } = this;
const { publishableKey, stripeAccount, locale } = this;
if (!publishableKey)
readonly.set<StripeBase>(this, { elements: null, element: null, stripe: null });
else {
try {
const options = { stripeAccount };
const options = { stripeAccount, locale };
const stripe =
(window.Stripe) ? window.Stripe(publishableKey, options) : await loadStripe(publishableKey, options);
const elements = stripe?.elements();
Expand Down
14 changes: 14 additions & 0 deletions test/stripe-elements.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,20 @@ describe('<stripe-elements>', function() {
});
});
});

describe('with no `locale` property set', function() {
it('should have `auto` by default', function() {
expect(element.locale).to.be.equal('auto');
});
});

describe('with `locale` property set', function() {
const LOCALE = 'en';
beforeEach(Helpers.setProps({ locale: LOCALE }));
it(`should have ${LOCALE}`, function() {
expect(element.locale).to.be.equal('en');
});
});
});

describe('with a card that will be declined', function() {
Expand Down
1 change: 1 addition & 0 deletions test/test-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export const BASE_DEFAULT_PROPS = Object.freeze({
source: null,
stripe: null,
token: null,
locale: 'auto',
});

export const BASE_READ_ONLY_PROPS = Object.freeze([
Expand Down

0 comments on commit 44317a4

Please sign in to comment.