Skip to content
This repository has been archived by the owner on Mar 22, 2023. It is now read-only.

Commit

Permalink
feat: Allow Apple Pay to be auto configured (#55)
Browse files Browse the repository at this point in the history
* feat: Allow Apple Pay to be auto configured

* chore: Bump package version

* fix: Fix test

* chore: Add link to Apple Pay fixture in nav.js
  • Loading branch information
rensftw authored Feb 9, 2022
1 parent 1b8c690 commit 957cdfa
Show file tree
Hide file tree
Showing 10 changed files with 139 additions and 18 deletions.
20 changes: 10 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@rebilly/framepay-react",
"version": "1.1.5",
"version": "1.2.0",
"description": "A React wrapper for Rebilly's FramePay offering out-of-the-box support for Redux and other common React features",
"main": "build/index.js",
"author": "Rebilly",
Expand Down Expand Up @@ -123,13 +123,13 @@
"**/*.spec.js"
]
},
"resolutions": {
"yargs-parser": "^13.1.2",
"dot-prop": "^4.2.1",
"node-notifier": "^8.0.1",
"glob-parent": "^5.1.2",
"trim-newlines": "^3.0.1",
"node-forge": "^0.10.0",
"postcss": "^7.0.36"
}
"resolutions": {
"yargs-parser": "^13.1.2",
"dot-prop": "^4.2.1",
"node-notifier": "^8.0.1",
"glob-parent": "^5.1.2",
"trim-newlines": "^3.0.1",
"node-forge": "^0.10.0",
"postcss": "^7.0.36"
}
}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { SUPPORTED_CARD_BRANDS } from './lib/constants';

import {
withFramePay,
withFramePayApplePayComponent,
withFramePayBankComponent,
withFramePayCardComponent,
withFramePayIBANComponent
Expand All @@ -25,6 +26,7 @@ export {
withFramePayCardComponent,
withFramePayBankComponent,
withFramePayIBANComponent,
withFramePayApplePayComponent,
FramePayComponentProps,
FramePayCardProps,
FramePayBankProps,
Expand Down
7 changes: 2 additions & 5 deletions src/lib/components/elements/applepay-element.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default class ApplePayElement extends BaseElement<
ApplePayState
> {
setupElement() {
const { Rebilly, onTokenReady, extraData, form } = this.props;
const { Rebilly, onTokenReady } = this.props;

const makeElement = () => {
// elementNode already checked in BaseElement.handleSetupElement
Expand All @@ -20,10 +20,7 @@ export default class ApplePayElement extends BaseElement<
}

try {
return Rebilly.applePay.mount(this.elementNode, {
extraData,
form
});
return Rebilly.applePay.mount(this.elementNode);
} catch (e) {
throw FramePayError({
code: FramePayError.codes.elementMountError,
Expand Down
35 changes: 34 additions & 1 deletion src/lib/components/injector.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import DigitalWalletElementComponent from './elements/digitalwallet-element';
import IBANElementComponent from './elements/iban-element';

import {
FramePayApplePayProps,
FramePayBankProps,
FramePayCardProps,
FramePayComponentProps,
Expand Down Expand Up @@ -201,7 +202,7 @@ const elementsFabric = (type: PaymentElements): object => {
(data: FramePayContext) =>
({
Rebilly: makeRebillyProps(data)
} as DigitalWalletProps)
} as ApplePayProps)
);

return {
Expand Down Expand Up @@ -371,3 +372,35 @@ export function withFramePayIBANComponent<OriginalProps extends object>(
}
};
}

export function withFramePayApplePayComponent<OriginalProps extends object>(
WrappedComponent: React.ComponentType<OriginalProps & FramePayApplePayProps>
) {
const elements = elementsFabric('applePay');
return class extends React.Component<
OriginalProps & FramePayApplePayProps,
{}
> {
static readonly displayName = `withFramePayApplePayComponent${name}(${WrappedComponent.displayName ||
WrappedComponent.name ||
'Component'})`;

render() {
return (
<ContextConsumer>
{(data: FramePayContext) => {
return (
<WrappedComponent
{...{
...this.props,
...elements,
Rebilly: makeRebillyProps(data)
}}
/>
);
}}
</ContextConsumer>
);
}
};
}
15 changes: 15 additions & 0 deletions test/e2e/fixtures/apple-pay.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8" />
<title>Test Apple Pay</title>
</head>

<body>
<div id="app"></div>
<script src="apple-pay.js"></script>
<script src="nav.js"></script>
</body>

</html>
65 changes: 65 additions & 0 deletions test/e2e/fixtures/apple-pay.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import React, { Component } from 'react';
import ReactDOM from 'react-dom';

import { FramePayProvider, withFramePayApplePayComponent } from '../../../build';
import { ReactVersion } from './util';
import './style.css';

const params = {
publishableKey: 'pk_sandbox_S95ATjj4hXZs-T9QpZq1ENl2tDSrUkCGv98utc9',
organizationId: '5977150c-1c97-4dd4-9860-6bb2bab070b4',
websiteId: 'demo.com',
transactionData: {
amount: 10,
currency: 'USD',
label: 'Purchase label 1',
},
};

class ApplePayElementComponent extends Component {

constructor(props) {
super(props);
this.state = {
token: {
error: null,
data: null
}
};
}

render() {
return (<div>
<h2>{this.props.title}</h2>
<h3>FramePay version: {this.props.Rebilly.version}</h3>
<div className="flex-wrapper">
<div className="example-2">
<this.props.ApplePayElement />
</div>
</div>
</div>);
}
}

const ApplePayElement = withFramePayApplePayComponent(ApplePayElementComponent);

class App extends Component {

render() {
return (<FramePayProvider injectStyle
{...params}
onReady={() => {
console.log('FramePayProvider.onReady');
}}
onError={(err) => {
console.log('FramePayProvider.onError', err);
}}>
<div>
{ReactVersion()}
<ApplePayElement />
</div>
</FramePayProvider>);
}
}

ReactDOM.render(<App/>, document.getElementById('app'));
1 change: 1 addition & 0 deletions test/e2e/fixtures/nav.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
setTimeout(() => {
const node = document.createElement('ul');
[
'apple-pay',
'bank-separate',
'card-separate',
'card-separate-rebilly-fields',
Expand Down
1 change: 1 addition & 0 deletions test/unit/specs/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const exportKeys: ReadonlyArray<string> = [
'SUPPORTED_CARD_BRANDS',
'FramePayProvider',
'withFramePay',
'withFramePayApplePayComponent',
'withFramePayCardComponent',
'withFramePayBankComponent',
'withFramePayIBANComponent'
Expand Down
2 changes: 0 additions & 2 deletions types/elements.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ interface ApplePayProps extends RebillyProps {
readonly Rebilly: RebillyProps;
readonly id?: string;
readonly onTokenReady?: (data: string) => void;
readonly form: HTMLElement | React.Component;
readonly extraData: Object;
}

interface DigitalWalletProps extends PaymentComponentProps {
Expand Down
9 changes: 9 additions & 0 deletions types/rebilly/settings.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,21 @@ interface FramePayCardSettings {
readonly brands?: FramePayCardBrands;
}

interface ApplePayDisplayOptions {
readonly buttonType: string;
readonly buttonColor: string;
readonly buttonLanguage: string;
}

interface FramePaySettings {
readonly publishableKey: string;
readonly organizationId: string;
readonly websiteId: string;
readonly locale?: supportedLocales;
readonly icon?: FramePaySettingsIcon;
readonly placeholders?: FramePaySettingsPlaceholders;
readonly style?: FramePaySettingsStyles;
readonly classes?: FramePaySettingsClasses;
readonly card?: FramePayCardSettings;
readonly applePay?: ApplePayDisplayOptions;
}

0 comments on commit 957cdfa

Please sign in to comment.