Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Investigating an idea how to reduce amount of duplications in paypal commerce strategies #2735

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
/packages/paypal-commerce-integration @bigcommerce/team-paypal
/packages/paypal-commerce-utils @bigcommerce/team-paypal
/packages/paypal-pro-integration @bigcommerce/team-paypal
/packages/paypal-utils @bigcommerce/team-paypal


## Shared
Expand Down
18 changes: 18 additions & 0 deletions packages/paypal-utils/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"extends": ["../../.eslintrc.json"],
"ignorePatterns": ["!**/*"],
"overrides": [
{
"files": ["*.ts", "*.tsx", "*.js", "*.jsx"],
"rules": {}
},
{
"files": ["*.ts", "*.tsx"],
"rules": {}
},
{
"files": ["*.js", "*.jsx"],
"rules": {}
}
]
}
11 changes: 11 additions & 0 deletions packages/paypal-utils/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# paypal-utils

This library was generated with [Nx](https://nx.dev).

## Running unit tests

Run `nx test paypal-utils` to execute the unit tests via [Jest](https://jestjs.io).

## Running lint

Run `nx lint paypal-utils` to execute the lint via [ESLint](https://eslint.org/).
14 changes: 14 additions & 0 deletions packages/paypal-utils/jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module.exports = {
displayName: 'paypal-utils',
preset: '../../jest.preset.js',
globals: {
'ts-jest': {
tsconfig: '<rootDir>/tsconfig.spec.json',
},
},
transform: {
'^.+\\.[tj]sx?$': 'ts-jest',
},
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx'],
coverageDirectory: '../../coverage/packages/paypal-utils',
};
23 changes: 23 additions & 0 deletions packages/paypal-utils/project.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"root": "packages/paypal-utils",
"sourceRoot": "packages/paypal-utils/src",
"projectType": "library",
"targets": {
"lint": {
"executor": "@nrwl/linter:eslint",
"outputs": ["{options.outputFile}"],
"options": {
"lintFilePatterns": ["packages/paypal-utils/**/*.ts"]
}
},
"test": {
"executor": "@nrwl/jest:jest",
"outputs": ["coverage/packages/paypal-utils"],
"options": {
"jestConfig": "packages/paypal-utils/jest.config.js",
"passWithNoTests": true
}
}
},
"tags": []
}
Empty file.
131 changes: 131 additions & 0 deletions packages/paypal-utils/src/paypal-button.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// Cart page / mini cart / add to cart modal -> default & payNowOptions
// PDP -> buyNowOptions & payNowOptions



export default class PayPalButton {
renderOrThrow(options): Promise<void> {
const {
containerId,
paymentMethodName, // (!) FundingSource
buttonStyles, // (?)
onEligibilityFailure, // (?)
onCancel, // (?)
onRenderButton, // (?)
buyNowOptions, // (?)
payNowOptions, // (?) -skip checkout
paypalSdk, // (!)
} = options;

let paypalConfig = {};

/**
*
* Get mapped FundingSource based on payment provider name
*
*/
paypalConfig.fundingSource = this.getFundingSourceByPaymentMethodName(paymentMethodName);


/**
*
* Styles
*
* */
if (buttonStyles) {
paypalConfig.style = this.getValidButtonStyle(paymentMethodName, buttonStyles);
}

/**
*
* Get PayPal callbacks
*
* */

const paypalCallbacks = this.getButtonCallbacks();

Object.assign(paypalConfig, paypalCallbacks);





// render wallet button based on config



// get paypal sdk
// get default callbacks -> will be always provided
// get buy now callbacks -> buyNow config should be provided via render options
// get pay now callbacks -> payNow config should be provided via render options
// handle on eligibility failure -> should be provided via render options

const paypalButton = paypal.Buttons(paypalConfig);

if (paypalButton.isEligible()) {
paypalButton.render(`#${containerId}`);
} else {
console.log(`Button is not eligible to render. Payment method name is ${paymentMethodName}`);

onEligibilityFailure();
}
}

/**
*
* Funding source mapper
*
*/
private getFundingSourceByPaymentMethodName(paymentMethodName: string): string {
// All mappings should be here
return '';
}


/**
*
* Buttons styles
*
*/
private getValidButtonStyle(paymentMethodName: string, buttonStyles: object): object {
// All styles calculations should be here
return {};
}


/**
*
* PayPal Config callbacks
*
* */
private getButtonCallbacks() {
const callbacks = {
createOrder: () => {},
onApprove: () => {},
};

if (buyNowOptions) {
// Should buyNowInitializeOptions be validated on higher level?

const buyNowCallbacks = {
onClick: () => {}, // only if buyNowInitializeOptions is provided
onCancel: () => {}, // load default checkout
};

Object.assign(callbacks, buyNowCallbacks);
}

if (payNowOptions) {
const payNowCallbacks = {
onShippingAddressChange: () => {},
onShippingOptionsChange: () => {},
onApprove: () => {},
};

Object.assign(callbacks, payNowCallbacks);
}

return callbacks;
}

}
Empty file.
Empty file.
Empty file.
3 changes: 3 additions & 0 deletions packages/paypal-utils/src/paypal-sdk.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default class PayPalSDK {

}
19 changes: 19 additions & 0 deletions packages/paypal-utils/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"extends": "../../tsconfig.base.json",
"files": [],
"include": [],
"references": [
{
"path": "./tsconfig.lib.json"
},
{
"path": "./tsconfig.spec.json"
}
],
"compilerOptions": {
"forceConsistentCasingInFileNames": true,
"strict": true,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true
}
}
10 changes: 10 additions & 0 deletions packages/paypal-utils/tsconfig.lib.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"declaration": true,
"types": []
},
"include": ["**/*.ts"],
"exclude": ["**/*.spec.ts"]
}
19 changes: 19 additions & 0 deletions packages/paypal-utils/tsconfig.spec.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "../../dist/out-tsc",
"module": "commonjs",
"types": ["jest", "node"]
},
"include": [
"**/*.test.ts",
"**/*.spec.ts",
"**/*.test.tsx",
"**/*.spec.tsx",
"**/*.test.js",
"**/*.spec.js",
"**/*.test.jsx",
"**/*.spec.jsx",
"**/*.d.ts"
]
}
1 change: 1 addition & 0 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@
"@bigcommerce/checkout-sdk/paypal-pro-integration": [
"packages/paypal-pro-integration/src/index.ts"
],
"@bigcommerce/checkout-sdk/paypal-utils": ["packages/paypal-utils/src/index.ts"],
"@bigcommerce/checkout-sdk/sagepay-integration": [
"packages/sagepay-integration/src/index.ts"
],
Expand Down
1 change: 1 addition & 0 deletions workspace.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"paypal-commerce-utils": "packages/paypal-commerce-utils",
"paypal-express-integration": "packages/paypal-express-integration",
"paypal-pro-integration": "packages/paypal-pro-integration",
"paypal-utils": "packages/paypal-utils",
"sagepay-integration": "packages/sagepay-integration",
"sezzle-integration": "packages/sezzle-integration",
"squarev2-integration": "packages/squarev2-integration",
Expand Down