-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(PAYMENTS-17861): add saved methods component
- Loading branch information
Showing
15 changed files
with
488 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
20.10.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta | ||
name="viewport" | ||
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0" | ||
/> | ||
<meta http-equiv="X-UA-Compatible" content="ie=edge" /> | ||
<title>Document</title> | ||
<!-- | ||
Link the SDK bundle. | ||
NOTE: In this example, we use a local build just for convenience purposes. | ||
--> | ||
<script src="../../dist/main.js"></script> | ||
</head> | ||
|
||
<body> | ||
<h1>Pay Station SDK</h1> | ||
|
||
<!-- | ||
Define a specific Saved methods component in the HTML markup, e.g., `psdk-saved-methods`. | ||
The `country` attribute indicates the country for which the saved methods are being loaded. | ||
--> | ||
<psdk-saved-methods not-found="Methods not found"></psdk-saved-methods> | ||
|
||
<!-- Initialization script --> | ||
<script> | ||
if (typeof PayStationSdk === 'undefined') { | ||
alert(` | ||
It seems SDK library is undefined. | ||
Please, link CDN source or create local build (recommended to test purposes only). | ||
`); | ||
throw new Error('PayStationSdk not found'); | ||
} | ||
/** | ||
* To learn more about creating tokens, | ||
* please read https://developers.xsolla.com/api/pay-station/operation/create-token/ | ||
*/ | ||
const accessToken = ''; | ||
|
||
if (!accessToken) { | ||
alert(`No token provided. Please, check the documentation`); | ||
throw new Error('No token provided'); | ||
} | ||
|
||
async function initPayStationSdk() { | ||
/** | ||
* The SDK is available under the PayStationSdk namespace. | ||
* To begin initialization, obtain a reference to the headlessCheckout object. | ||
*/ | ||
const { headlessCheckout } = PayStationSdk; | ||
|
||
/** | ||
* Call the `init()` method with the provided environment object. | ||
* The isWebView parameter is required and indicates whether your | ||
* integration type is a webview or not. | ||
* Please note that this command executes asynchronously. | ||
*/ | ||
await headlessCheckout.init({ | ||
isWebView: false, | ||
sandbox: false, | ||
}); | ||
|
||
/** | ||
* After the Payments SDK has been initialized, the next step is setting the token. | ||
* To learn more about creating tokens, | ||
* please read https://developers.xsolla.com/api/pay-station/operation/create-token/ | ||
*/ | ||
await headlessCheckout.setToken(accessToken); | ||
|
||
/** | ||
* To gain more control over Pay Station components, | ||
* you can access them as regular HTML elements. | ||
*/ | ||
const savedMethods = document.querySelector('psdk-saved-methods'); | ||
|
||
/** | ||
* To add extra logic to a component, you can subscribe to | ||
* its events and place your code in a callback function. | ||
*/ | ||
savedMethods.addEventListener('savedMethodSelected', (event) => { | ||
console.log(event.detail); | ||
}); | ||
} | ||
|
||
// run initialization script | ||
initPayStationSdk(); | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
src/features/headless-checkout/web-components/saved-methods/pipes/get-expire-date.pipe.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export function getExpireDate(date?: { month: string; year: string }): string { | ||
if (!date) { | ||
return ''; | ||
} | ||
|
||
return `${date.month}/${date.year.slice(2)}`; | ||
} |
41 changes: 41 additions & 0 deletions
41
src/features/headless-checkout/web-components/saved-methods/pipes/name-cutter.pipe.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { SavedMethod } from '../../../../../core/saved-method.interface'; | ||
|
||
const replacerChar = '*'; | ||
|
||
function findFirstPositionOfNotReplacedChar(value: string): number { | ||
const replacedCharLastPosition = value.split('').lastIndexOf(replacerChar); | ||
return replacedCharLastPosition > -1 | ||
? replacedCharLastPosition + 1 | ||
: replacedCharLastPosition; | ||
} | ||
|
||
function getTextForCard(cardNumber: string): string { | ||
const notReplacedCharFirstPosition = | ||
findFirstPositionOfNotReplacedChar(cardNumber); | ||
return notReplacedCharFirstPosition === -1 | ||
? `${cardNumber}` | ||
: `•• ${cardNumber.slice(notReplacedCharFirstPosition).trim()}`; | ||
} | ||
|
||
function getTextForEmail(email: string): string { | ||
const firstPositionChar = email.indexOf('*'); | ||
const notReplacedCharFirstPosition = | ||
findFirstPositionOfNotReplacedChar(email); | ||
return ( | ||
email.slice(0, firstPositionChar).trim() + | ||
'••' + | ||
email.slice(notReplacedCharFirstPosition).trim() | ||
); | ||
} | ||
|
||
export function getCutterName(method: SavedMethod): string { | ||
if (method.type === 'card') { | ||
return getTextForCard(method.name); | ||
} | ||
|
||
if (method.name.includes('@')) { | ||
return getTextForEmail(method.name); | ||
} | ||
|
||
return method.name.replace(/\*/g, '•'); | ||
} |
4 changes: 4 additions & 0 deletions
4
src/features/headless-checkout/web-components/saved-methods/saved-method-attributes.enum.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export enum SavedMethodAttributes { | ||
paymentMethodId = 'data-payment-method-id', | ||
savedMethodId = 'data-saved-method-id', | ||
} |
16 changes: 16 additions & 0 deletions
16
src/features/headless-checkout/web-components/saved-methods/saved-method.template.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { getSavedMethodTemplate } from './saved-method.template'; | ||
import { SavedMethod } from '../../../../core/saved-method.interface'; | ||
|
||
const mockQiwi = { | ||
name: '4476 24** **** 9527', | ||
psName: 'Mastercard', | ||
iconName: 'mastercard.svg', | ||
id: 112233, | ||
} as SavedMethod; | ||
|
||
describe('getSavedMethodTemplate', () => { | ||
it('Should draw template', () => { | ||
expect(getSavedMethodTemplate(mockQiwi)).toContain('img'); | ||
expect(getSavedMethodTemplate(mockQiwi)).toContain(mockQiwi.name); | ||
}); | ||
}); |
31 changes: 31 additions & 0 deletions
31
src/features/headless-checkout/web-components/saved-methods/saved-method.template.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { cdnUrl } from '../../environment'; | ||
import { SavedMethod } from '../../../../core/saved-method.interface'; | ||
import { getExpireDate } from './pipes/get-expire-date.pipe'; | ||
import { getCutterName } from './pipes/name-cutter.pipe'; | ||
|
||
const iconsPath = `${cdnUrl}/paystation4/brand-logos`; | ||
|
||
export const getSavedMethodTemplate = (method: SavedMethod): string => { | ||
const expireDate = getExpireDate(method.cardExpiryDate); | ||
const name = getCutterName(method); | ||
let iconName: string; | ||
|
||
if (!method.iconName) { | ||
iconName = 'default.svg'; | ||
} else { | ||
iconName = method.iconName; | ||
} | ||
|
||
return `<li class='saved-method' data-payment-method-id='${ | ||
method.pid | ||
}' data-saved-method-id='${method.id}'> | ||
<a tabindex='0' href='${method.id}'> | ||
<span class='icon'> | ||
<img src='${iconsPath}/${iconName}' alt='${method.name}'> | ||
</span> | ||
<span class='payment-method-name'>${method.psName}</span> | ||
<span class='name'>${name}</span> | ||
${expireDate ? `<span class='expire-date'>${expireDate}</span>` : ''} | ||
</a> | ||
</li>`; | ||
}; |
3 changes: 3 additions & 0 deletions
3
src/features/headless-checkout/web-components/saved-methods/saved-methods-attributes.enum.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export enum SavedMethodsAttributes { | ||
notFound = 'not-found', | ||
} |
3 changes: 3 additions & 0 deletions
3
src/features/headless-checkout/web-components/saved-methods/saved-methods-events.enum.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export enum SavedMethodsEvents { | ||
savedMethodSelected = 'savedMethodSelected', | ||
} |
Oops, something went wrong.