Skip to content

Commit

Permalink
cleaned up stories and code
Browse files Browse the repository at this point in the history
  • Loading branch information
ribeiroguilherme committed Nov 25, 2024
1 parent 697fae5 commit 0b5a750
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 81 deletions.
17 changes: 9 additions & 8 deletions packages/lib/src/components/PayPalFastlane/FastlaneSDK.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { resolveEnvironments } from '../../core/Environment';
import requestFastlaneToken from './services/request-fastlane-token';
import { convertAdyenLocaleToFastlaneLocale } from './utils';
import Script from '../../utils/Script';

import type { Fastlane, AuthenticatedCustomerResult, ShowShippingAddressSelectorResult } from './types';
import type { Fastlane, FastlaneAuthenticatedCustomerResult, FastlaneShippingAddressSelectorResult } from './types';
import type { FastlaneTokenData } from './services/request-fastlane-token';
import type { CoreConfiguration } from '../../core/types';

export interface FastlaneSDKConfiguration {
clientKey: string;
locale?: 'en_us' | 'es_us' | 'fr_rs' | 'zh_us';
locale?: 'en-US' | 'es-US' | 'fr-RS' | 'zh-US';
environment?: CoreConfiguration['environment'];
}

Expand All @@ -24,7 +25,7 @@ class FastlaneSDK {

this.checkoutShopperURL = apiUrl;
this.clientKey = configuration.clientKey;
this.locale = configuration.locale || 'en_us';
this.locale = convertAdyenLocaleToFastlaneLocale(configuration.locale || 'en-US');
}

public async initialize(): Promise<FastlaneSDK> {
Expand All @@ -34,7 +35,7 @@ class FastlaneSDK {
return this;
}

public async authenticate(email: string): Promise<AuthenticatedCustomerResult> {
public async authenticate(email: string): Promise<FastlaneAuthenticatedCustomerResult> {
const { customerContextId } = await this.fastlaneSdk.identity.lookupCustomerByEmail(email);

if (customerContextId) {
Expand All @@ -47,12 +48,12 @@ class FastlaneSDK {
}
}

public showShippingAddressSelector(): Promise<ShowShippingAddressSelectorResult> {
public showShippingAddressSelector(): Promise<FastlaneShippingAddressSelectorResult> {
if (!this.fastlaneSdk.profile) return null;
return this.fastlaneSdk.profile.showShippingAddressSelector();
}

public async mountWatermark(container: HTMLElement | string, options?) {
public async mountWatermark(container: HTMLElement | string, options = { includeAdditionalInfo: false }): Promise<void> {
const component = await this.fastlaneSdk.FastlaneWatermarkComponent(options);
component.render(container);
}
Expand All @@ -61,7 +62,7 @@ class FastlaneSDK {
return requestFastlaneToken(this.checkoutShopperURL, this.clientKey);
}

private async fetchSdk(clientToken: string, clientId: string) {
private async fetchSdk(clientToken: string, clientId: string): Promise<void> {
const url = `https://www.paypal.com/sdk/js?client-id=${clientId}&components=buttons,fastlane`;
const script = new Script(url, 'body', {}, { sdkClientToken: clientToken });

Expand All @@ -72,7 +73,7 @@ class FastlaneSDK {
}
}

private async initializeFastlane() {
private async initializeFastlane(): Promise<void> {
this.fastlaneSdk = await window.paypal.Fastlane({});
this.fastlaneSdk.setLocale(this.locale);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function requestFastlaneToken(url: string, clientKey: string): Promise<FastlaneT
id: '2747bd08-783a-45c6-902b-3efbda5497b7',
clientId: 'AXy9hIzWB6h_LjZUHjHmsbsiicSIbL4GKOrcgomEedVjduUinIU4C2llxkW5p0OG0zTNgviYFceaXEnj',
merchantId: 'C3UCKQHMW4948',
value: 'eyJraWQiOiJkMTA2ZTUwNjkzOWYxMWVlYjlkMTAyNDJhYzEyMDAwMiIsInR5cCI6IkpXVCIsImFsZyI6IkVTMjU2In0.eyJpc3MiOiJodHRwczovL2FwaS5zYW5kYm94LnBheXBhbC5jb20iLCJhdWQiOlsiaHR0cHM6Ly9hcGkuYnJhaW50cmVlZ2F0ZXdheS5jb20iLCJjaGVja291dC1wbGF5Z3JvdW5kLm5ldGxpZnkuYXBwIl0sInN1YiI6Ik02VE5BRVNaNUZHTk4iLCJhY3IiOlsiY2xpZW50Il0sInNjb3BlIjpbIkJyYWludHJlZTpWYXVsdCJdLCJvcHRpb25zIjp7fSwiYXoiOiJjY2cxOC5zbGMiLCJleHRlcm5hbF9pZCI6WyJQYXlQYWw6QzNVQ0tRSE1XNDk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JVNWtRZyJ9.sRKQXrQjRn2CEOIeBwXsspd8Z7axRdMYjY95ga6mfYemcQrKZxBe3ASDCuWrCpbUvm04VTxXs80bR4V7hDAHoQ',
expiresAt: '2024-11-01T13:34:01.804+00:00'
});
// return httpPost<FastlaneTokenData>({ loadingContext: url, path, errorLevel: 'fatal' });
Expand Down
29 changes: 13 additions & 16 deletions packages/lib/src/components/PayPalFastlane/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,16 @@ export interface FastlaneOptions {}
export interface Fastlane {
identity: {
lookupCustomerByEmail: (email: string) => Promise<{ customerContextId: string }>;
triggerAuthenticationFlow: (customerContextId: string, options?: AuthenticationFlowOptions) => Promise<AuthenticatedCustomerResult>;
triggerAuthenticationFlow: (customerContextId: string, options?: AuthenticationFlowOptions) => Promise<FastlaneAuthenticatedCustomerResult>;
};
profile: {
showShippingAddressSelector: () => Promise<ShowShippingAddressSelectorResult>;
showShippingAddressSelector: () => Promise<FastlaneShippingAddressSelectorResult>;
showCardSelector: () => ShowCardSelectorResult;
};
setLocale: (locale: string) => void;
FastlaneWatermarkComponent: (options: FastlaneWatermarkOptions) => Promise<FastlaneWatermarkComponent>;
FastlaneWatermarkComponent: (options: { includeAdditionalInfo: boolean }) => Promise<FastlaneWatermarkComponent>;
}

interface FastlaneWatermarkOptions {
includeAdditionalInfo: boolean;
}
interface FastlaneWatermarkComponent {
render: (container) => null;
}
Expand All @@ -36,10 +33,6 @@ interface AuthenticationFlowOptions {}
/**
* The AuthenticatedCustomerResult object type is returned from the identity.triggerAuthenticationFlow() call.
*/
export interface AuthenticatedCustomerResult {
authenticationState: 'succeeded' | 'failed' | 'canceled' | 'not_found';
profileData: FastlaneProfile;
}
interface FastlaneProfile {
name: Name;
shippingAddress: FastlaneShipping;
Expand Down Expand Up @@ -89,15 +82,19 @@ interface CardPaymentSource {
billingAddress: FastlaneAddress;
}

interface ShowCardSelectorResult {
selectionChanged: boolean;
selectedCard: PaymentToken;
}

/**
* Profile method reference types
* External types
*/
export interface ShowShippingAddressSelectorResult {
export interface FastlaneShippingAddressSelectorResult {
selectionChanged: boolean;
selectedAddress: FastlaneShipping;
}

interface ShowCardSelectorResult {
selectionChanged: boolean;
selectedCard: PaymentToken;
export interface FastlaneAuthenticatedCustomerResult {
authenticationState: 'succeeded' | 'failed' | 'canceled' | 'not_found';
profileData: FastlaneProfile;
}
3 changes: 3 additions & 0 deletions packages/lib/src/components/PayPalFastlane/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function convertAdyenLocaleToFastlaneLocale(locale: string) {
return locale.replace('-', '_').toLowerCase();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { h } from 'preact';
import { useState } from 'preact/hooks';
import { GlobalStoryProps } from '../../types';

import Dropin from '../../../../src/components/Dropin';
import Card from '../../../../src/components/Card';
Expand All @@ -8,9 +9,6 @@ import PayPal from '../../../../src/components/PayPal';
import { Checkout } from '../../Checkout';
import { ComponentContainer } from '../../ComponentContainer';
import { GuestShopperForm } from './components/GuestShopperForm';
import './components/FastlaneStory.scss';

import { GlobalStoryProps } from '../../types';

interface Props {
checkoutConfig: GlobalStoryProps;
Expand All @@ -19,7 +17,8 @@ interface Props {
export const FastlaneInSinglePageApp = ({ checkoutConfig }: Props) => {
const [fastlaneData, setFastlaneData] = useState<any>(null);

const handleOnCheckoutStep = (fastlaneSdk, fastlaneData) => {
const handleOnCheckoutStep = (fastlaneSdk, fastlaneData, shippingAddress) => {
console.log('FastlaneInSinglePageApp', fastlaneSdk, fastlaneData, shippingAddress);
setFastlaneData(fastlaneData);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import { h } from 'preact';
import { useEffect, useState } from 'preact/hooks';

import FastlaneSDK from '../../../../../src/components/PayPalFastlane/FastlaneSDK';
import type { AuthenticatedCustomerResult } from '../../../../../src/components/PayPalFastlane/types';
import type { FastlaneAuthenticatedCustomerResult } from '../../../../../src/components/PayPalFastlane/types';

interface CollectEmailProps {
fastlaneSdk: FastlaneSDK;
onFastlaneLookup: (authResult: AuthenticatedCustomerResult) => void;
onFastlaneLookup: (authResult: FastlaneAuthenticatedCustomerResult) => void;
onEditEmail: () => void;
}

Expand All @@ -15,9 +15,7 @@ export const CollectEmail = ({ fastlaneSdk, onFastlaneLookup, onEditEmail }: Col
const [viewOnly, setViewOnly] = useState<boolean>(false);

const renderWatermark = async () => {
await fastlaneSdk.mountWatermark('#watermark-container', {
includeAdditionalInfo: true
});
await fastlaneSdk.mountWatermark('#watermark-container');
};

const handleEmailInput = event => {
Expand All @@ -40,7 +38,7 @@ export const CollectEmail = ({ fastlaneSdk, onFastlaneLookup, onEditEmail }: Col
}, []);

return (
<>
<section>
<div className="section_header">
<h3>Customer</h3>
{viewOnly && (
Expand All @@ -53,7 +51,7 @@ export const CollectEmail = ({ fastlaneSdk, onFastlaneLookup, onEditEmail }: Col
<div className="email-input-wrapper">
<input
disabled={viewOnly}
className="email-input"
className="input-field"
value={email}
name="email"
type="email"
Expand All @@ -71,6 +69,6 @@ export const CollectEmail = ({ fastlaneSdk, onFastlaneLookup, onEditEmail }: Col
)}
</div>
{!viewOnly && <button onClick={() => setEmail('[email protected]')}>Fill in valid email</button>}
</>
</section>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
align-self: end;
}

.email-input {
.input-field {
background-color: white;
border: #aab2bc 1px solid;
border-radius: 4px;
Expand All @@ -23,6 +23,17 @@
margin-right: 12px;
}

.shipping-section {
.input-field {
margin-bottom: 12px;
}

.shipping-checkbox {
margin-bottom: 20px;
}
}


.form-container {
background-color: white;
padding: 20px;
Expand All @@ -39,6 +50,7 @@
border: 1px solid #2d7fec;
background-color: #0551b5;
color: white;
margin-bottom: 12px;
}

.button:hover {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
import { h } from 'preact';
import { useEffect, useState } from 'preact/hooks';
import './FastlaneStory.scss';

import initializeFastlane from '../../../../../src/components/PayPalFastlane/initializeFastlane';
import FastlaneSDK from '../../../../../src/components/PayPalFastlane/FastlaneSDK';
import type { AuthenticatedCustomerResult } from '../../../../../src/components/PayPalFastlane/types';

import { CollectEmail } from './CollectEmail';
import { Shipping } from './Shipping';
import { ShippingWithFastlane } from './ShippingWithFastlane';
import './FastlaneStory.scss';

import initializeFastlane from '../../../../../src/components/PayPalFastlane/initializeFastlane';
import FastlaneSDK from '../../../../../src/components/PayPalFastlane/FastlaneSDK';
import type { FastlaneAuthenticatedCustomerResult } from '../../../../../src/components/PayPalFastlane/types';

interface GuestShopperFormProps {
onCheckoutStep(fastlane: FastlaneSDK, data: any): void;
onCheckoutStep(fastlane: FastlaneSDK, fastlaneData: any, shippingAddress: any): void;
}

export const GuestShopperForm = ({ onCheckoutStep }: GuestShopperFormProps) => {
const [fastlane, setFastlane] = useState<FastlaneSDK>(null);
const [fastlaneLookupData, setFastlaneLookupData] = useState<AuthenticatedCustomerResult>(null);
const [fastlaneLookupData, setFastlaneLookupData] = useState<FastlaneAuthenticatedCustomerResult>(null);

const loadFastlane = async () => {
const sdk = await initializeFastlane({
Expand All @@ -31,12 +31,11 @@ export const GuestShopperForm = ({ onCheckoutStep }: GuestShopperFormProps) => {
};

const handleFastlaneLookup = data => {
console.log(data);
setFastlaneLookupData(data);
};

const handleOnCheckoutClick = shippingAddress => {
onCheckoutStep(fastlane, fastlaneLookupData);
const handleOnCheckoutClick = (shippingAddress?: any) => {
onCheckoutStep(fastlane, fastlaneLookupData, shippingAddress);
};

useEffect(() => {
Expand All @@ -51,6 +50,7 @@ export const GuestShopperForm = ({ onCheckoutStep }: GuestShopperFormProps) => {

return (
<div className="form-container">
<h2>Merchant Checkout Page</h2>
<CollectEmail fastlaneSdk={fastlane} onFastlaneLookup={handleFastlaneLookup} onEditEmail={handleOnEditEmail} />
<hr />

Expand Down
Loading

0 comments on commit 0b5a750

Please sign in to comment.