Compare commits
16 Commits
Author | SHA1 | Date | |
---|---|---|---|
6f0952bc22 | |||
08c78bae05 | |||
406178d6d0 | |||
d6a0a9d438 | |||
12b6a771a2 | |||
ebf2796e0e | |||
fe62ecd0e1 | |||
4972b135d5 | |||
e15f0ff2b2 | |||
4bbcb9639c | |||
306453cde4 | |||
2e8b364add | |||
50f53f6596 | |||
8e74c00480 | |||
dc7c8b6568 | |||
49c3d5df5c |
@ -1,6 +1,7 @@
|
||||
{
|
||||
"npmci": {
|
||||
"npmGlobalTools": []
|
||||
"npmGlobalTools": [],
|
||||
"npmAccessLevel": "public"
|
||||
},
|
||||
"gitzone": {
|
||||
"projectType": "npm",
|
||||
|
74
package-lock.json
generated
74
package-lock.json
generated
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@mojoio/paypal",
|
||||
"version": "1.0.2",
|
||||
"version": "1.0.11",
|
||||
"lockfileVersion": 1,
|
||||
"requires": true,
|
||||
"dependencies": {
|
||||
@ -1378,7 +1378,6 @@
|
||||
"version": "4.0.17",
|
||||
"resolved": "https://verdaccio.lossless.one/@pushrocks%2flik/-/lik-4.0.17.tgz",
|
||||
"integrity": "sha512-K5dX3k3i7iVxFMJ+IYwJRljewukJCc2zgj6+88R18/8SajVAq7ITOl3/FTbmEPFCJv5rl/LQ9FtcMynWlwSlzQ==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"@pushrocks/smartdelay": "^2.0.10",
|
||||
"@pushrocks/smartmatch": "^1.0.7",
|
||||
@ -1484,7 +1483,6 @@
|
||||
"version": "2.0.10",
|
||||
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartdelay/-/smartdelay-2.0.10.tgz",
|
||||
"integrity": "sha512-JOFpEfYiKnqcQYoUrL/jDyBDZUmlgJNm5U30MQQ3f+yci+rlFnMY5VvBBEYn5WgoX0ilwU+E15mtpDWhXcmhzg==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"@pushrocks/smartpromise": "^3.0.6"
|
||||
}
|
||||
@ -1848,7 +1846,6 @@
|
||||
"version": "1.0.7",
|
||||
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartmatch/-/smartmatch-1.0.7.tgz",
|
||||
"integrity": "sha512-D+lK5HIKO4Kj1Jm/ycKvy1VzDJ3V6ucHqmf5DMBFdm18BrMj2Zb6M7wN8HUKtkfHvOI7ig85JMuANSEyO7kAPg==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"matcher": "^3.0.0"
|
||||
}
|
||||
@ -2008,7 +2005,6 @@
|
||||
"version": "2.0.18",
|
||||
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmartrx/-/smartrx-2.0.18.tgz",
|
||||
"integrity": "sha512-DqUmtS7mONO6bvWEZmSLy4M9hlxNWysMYc7XOZLr50BMaa0HUNYWI1myUFCMNcbUUOxuQNig01XqR1smfIN69g==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"@pushrocks/lik": "^4.0.14",
|
||||
"@pushrocks/smartpromise": "^3.0.6",
|
||||
@ -2045,7 +2041,6 @@
|
||||
"version": "3.0.24",
|
||||
"resolved": "https://verdaccio.lossless.one/@pushrocks%2fsmarttime/-/smarttime-3.0.24.tgz",
|
||||
"integrity": "sha512-c/n0Y3CUnVNa3TnuXZ5aRc0bWj4RX7+XSjPes1J4HidU1yBOd9UOt6l08W8aPH5jMjT9Ei2jH0GsheaTGoVEYA==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"@pushrocks/lik": "^4.0.13",
|
||||
"@pushrocks/smartdelay": "^2.0.10",
|
||||
@ -2363,8 +2358,7 @@
|
||||
"@types/minimatch": {
|
||||
"version": "3.0.3",
|
||||
"resolved": "https://verdaccio.lossless.one/@types%2fminimatch/-/minimatch-3.0.3.tgz",
|
||||
"integrity": "sha512-tHq6qdbT9U1IRSGf14CL0pUlULksvY9OZ+5eEgl1N7t+OA3tGvNpxJCzuKQlsNgCVwbAs670L1vcVQi8j9HjnA==",
|
||||
"dev": true
|
||||
"integrity": "sha512-tHq6qdbT9U1IRSGf14CL0pUlULksvY9OZ+5eEgl1N7t+OA3tGvNpxJCzuKQlsNgCVwbAs670L1vcVQi8j9HjnA=="
|
||||
},
|
||||
"@types/node": {
|
||||
"version": "14.6.0",
|
||||
@ -3930,8 +3924,7 @@
|
||||
"croner": {
|
||||
"version": "1.1.23",
|
||||
"resolved": "https://verdaccio.lossless.one/croner/-/croner-1.1.23.tgz",
|
||||
"integrity": "sha512-VsSyKBVtshU8qd2yPEWsBv5xxTLbRUuq6DX5bgUb4TTn/H6hmFGtfJtr0NKswu1UEsZZ6uhYc0kSOAvPlicQUA==",
|
||||
"dev": true
|
||||
"integrity": "sha512-VsSyKBVtshU8qd2yPEWsBv5xxTLbRUuq6DX5bgUb4TTn/H6hmFGtfJtr0NKswu1UEsZZ6uhYc0kSOAvPlicQUA=="
|
||||
},
|
||||
"cross-spawn": {
|
||||
"version": "6.0.5",
|
||||
@ -4291,8 +4284,7 @@
|
||||
"dayjs": {
|
||||
"version": "1.8.34",
|
||||
"resolved": "https://verdaccio.lossless.one/dayjs/-/dayjs-1.8.34.tgz",
|
||||
"integrity": "sha512-Olb+E6EoMvdPmAMq2QoucuyZycKHjTlBXmRx8Ada+wGtq4SIXuDCdtoaX4KkK0yjf1fJLnwXQURr8gQKWKaybw==",
|
||||
"dev": true
|
||||
"integrity": "sha512-Olb+E6EoMvdPmAMq2QoucuyZycKHjTlBXmRx8Ada+wGtq4SIXuDCdtoaX4KkK0yjf1fJLnwXQURr8gQKWKaybw=="
|
||||
},
|
||||
"deasync": {
|
||||
"version": "0.1.20",
|
||||
@ -4396,7 +4388,6 @@
|
||||
"version": "1.1.3",
|
||||
"resolved": "https://verdaccio.lossless.one/define-properties/-/define-properties-1.1.3.tgz",
|
||||
"integrity": "sha512-3MqfYKj2lLzdMSf8ZIZE/V+Zuy+BgD6f164e8K2w7dgnpKArBDerGYpM46IYYcjnkdPNMjPk9A6VFB8+3SKlXQ==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"object-keys": "^1.0.12"
|
||||
}
|
||||
@ -4810,8 +4801,7 @@
|
||||
"escape-string-regexp": {
|
||||
"version": "4.0.0",
|
||||
"resolved": "https://verdaccio.lossless.one/escape-string-regexp/-/escape-string-regexp-4.0.0.tgz",
|
||||
"integrity": "sha512-TtpcNJ3XAzx3Gq8sWRzJaVajRs0uVxA2YAkdb1jm2YkPz4G6egUFAyA3n5vtEIZefPk5Wa4UXbKuS5fKkJWdgA==",
|
||||
"dev": true
|
||||
"integrity": "sha512-TtpcNJ3XAzx3Gq8sWRzJaVajRs0uVxA2YAkdb1jm2YkPz4G6egUFAyA3n5vtEIZefPk5Wa4UXbKuS5fKkJWdgA=="
|
||||
},
|
||||
"escodegen": {
|
||||
"version": "1.9.1",
|
||||
@ -6816,7 +6806,6 @@
|
||||
"version": "1.3.0",
|
||||
"resolved": "https://verdaccio.lossless.one/is-nan/-/is-nan-1.3.0.tgz",
|
||||
"integrity": "sha512-z7bbREymOqt2CCaZVly8aC4ML3Xhfi0ekuOnjO2L8vKdl+CttdVoGZQhd4adMFAsxQ5VeRVwORs4tU8RH+HFtQ==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"define-properties": "^1.1.3"
|
||||
}
|
||||
@ -7390,7 +7379,6 @@
|
||||
"version": "3.0.0",
|
||||
"resolved": "https://verdaccio.lossless.one/matcher/-/matcher-3.0.0.tgz",
|
||||
"integrity": "sha512-OkeDaAZ/bQCxeFAozM55PKcKU0yJMPGifLwV4Qgjitu+5MoAfSQN4lsLJeXZ1b8w0x+/Emda6MZgXS1jvsapng==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"escape-string-regexp": "^4.0.0"
|
||||
}
|
||||
@ -7860,8 +7848,7 @@
|
||||
"object-keys": {
|
||||
"version": "1.1.1",
|
||||
"resolved": "https://verdaccio.lossless.one/object-keys/-/object-keys-1.1.1.tgz",
|
||||
"integrity": "sha512-NuAESUOUMrlIXOfHKzD6bpPu3tYt3xvjNdRIQ+FeT0lNb4K8WR70CaDxhuNguS2XG+GjkyMwOzsN5ZktImfhLA==",
|
||||
"dev": true
|
||||
"integrity": "sha512-NuAESUOUMrlIXOfHKzD6bpPu3tYt3xvjNdRIQ+FeT0lNb4K8WR70CaDxhuNguS2XG+GjkyMwOzsN5ZktImfhLA=="
|
||||
},
|
||||
"object-visit": {
|
||||
"version": "1.0.1",
|
||||
@ -9532,7 +9519,6 @@
|
||||
"version": "6.6.2",
|
||||
"resolved": "https://verdaccio.lossless.one/rxjs/-/rxjs-6.6.2.tgz",
|
||||
"integrity": "sha512-BHdBMVoWC2sL26w//BCu3YzKT4s2jip/WhwsGEDmeKYBhKDZeYezVUnHatYB7L85v5xs0BAQmg6BEYJEKxBabg==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"tslib": "^1.9.0"
|
||||
}
|
||||
@ -10339,8 +10325,7 @@
|
||||
"symbol-tree": {
|
||||
"version": "3.2.4",
|
||||
"resolved": "https://verdaccio.lossless.one/symbol-tree/-/symbol-tree-3.2.4.tgz",
|
||||
"integrity": "sha512-9QNk5KwDF+Bvz+PyObkmSYjI5ksVUYtjW7AU22r2NKcfLJcXp96hkDWU3+XndOsUb+AQ9QhfzfCT2O+CNWT5Tw==",
|
||||
"dev": true
|
||||
"integrity": "sha512-9QNk5KwDF+Bvz+PyObkmSYjI5ksVUYtjW7AU22r2NKcfLJcXp96hkDWU3+XndOsUb+AQ9QhfzfCT2O+CNWT5Tw=="
|
||||
},
|
||||
"systeminformation": {
|
||||
"version": "4.26.11",
|
||||
@ -10505,9 +10490,52 @@
|
||||
"tslib": {
|
||||
"version": "1.13.0",
|
||||
"resolved": "https://verdaccio.lossless.one/tslib/-/tslib-1.13.0.tgz",
|
||||
"integrity": "sha512-i/6DQjL8Xf3be4K/E6Wgpekn5Qasl1usyw++dAA35Ue5orEn65VIxOA+YvNNl9HV3qv70T7CNwjODHZrLwvd1Q==",
|
||||
"integrity": "sha512-i/6DQjL8Xf3be4K/E6Wgpekn5Qasl1usyw++dAA35Ue5orEn65VIxOA+YvNNl9HV3qv70T7CNwjODHZrLwvd1Q=="
|
||||
},
|
||||
"tslint": {
|
||||
"version": "6.1.3",
|
||||
"resolved": "https://verdaccio.lossless.one/tslint/-/tslint-6.1.3.tgz",
|
||||
"integrity": "sha512-IbR4nkT96EQOvKE2PW/djGz8iGNeJ4rF2mBfiYaR/nvUWYKJhLwimoJKgjIFEIDibBtOevj7BqCRL4oHeWWUCg==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"@babel/code-frame": "^7.0.0",
|
||||
"builtin-modules": "^1.1.1",
|
||||
"chalk": "^2.3.0",
|
||||
"commander": "^2.12.1",
|
||||
"diff": "^4.0.1",
|
||||
"glob": "^7.1.1",
|
||||
"js-yaml": "^3.13.1",
|
||||
"minimatch": "^3.0.4",
|
||||
"mkdirp": "^0.5.3",
|
||||
"resolve": "^1.3.2",
|
||||
"semver": "^5.3.0",
|
||||
"tslib": "^1.13.0",
|
||||
"tsutils": "^2.29.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"builtin-modules": {
|
||||
"version": "1.1.1",
|
||||
"resolved": "https://verdaccio.lossless.one/builtin-modules/-/builtin-modules-1.1.1.tgz",
|
||||
"integrity": "sha1-Jw8HbFpywC9bZaR9+Uxf46J4iS8=",
|
||||
"dev": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"tslint-config-prettier": {
|
||||
"version": "1.18.0",
|
||||
"resolved": "https://verdaccio.lossless.one/tslint-config-prettier/-/tslint-config-prettier-1.18.0.tgz",
|
||||
"integrity": "sha512-xPw9PgNPLG3iKRxmK7DWr+Ea/SzrvfHtjFt5LBl61gk2UBG/DB9kCXRjv+xyIU1rUtnayLeMUVJBcMX8Z17nDg==",
|
||||
"dev": true
|
||||
},
|
||||
"tsutils": {
|
||||
"version": "2.29.0",
|
||||
"resolved": "https://verdaccio.lossless.one/tsutils/-/tsutils-2.29.0.tgz",
|
||||
"integrity": "sha512-g5JVHCIJwzfISaXpXE1qvNalca5Jwob6FjI4AoPlqMusJ6ftFE7IkkFoMhVLRgK+4Kx3gkzb8UZK5t5yTTvEmA==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"tslib": "^1.8.1"
|
||||
}
|
||||
},
|
||||
"tty-browserify": {
|
||||
"version": "0.0.0",
|
||||
"resolved": "https://verdaccio.lossless.one/tty-browserify/-/tty-browserify-0.0.0.tgz",
|
||||
|
11
package.json
11
package.json
@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "@mojoio/paypal",
|
||||
"private": "false",
|
||||
"version": "1.0.2",
|
||||
"private": false,
|
||||
"version": "1.0.11",
|
||||
"description": "mojoio PayPal API abstraction",
|
||||
"main": "dist_ts/index.js",
|
||||
"typings": "dist_ts/index.d.ts",
|
||||
@ -15,11 +15,14 @@
|
||||
"@gitzone/tsbuild": "^2.1.25",
|
||||
"@gitzone/tstest": "^1.0.44",
|
||||
"@pushrocks/qenv": "^4.0.10",
|
||||
"@pushrocks/tapbundle": "^3.2.9"
|
||||
"@pushrocks/tapbundle": "^3.2.9",
|
||||
"tslint": "^6.1.3",
|
||||
"tslint-config-prettier": "^1.18.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"@pushrocks/smartrequest": "^1.1.47",
|
||||
"@pushrocks/smartstring": "^3.0.18"
|
||||
"@pushrocks/smartstring": "^3.0.18",
|
||||
"@pushrocks/smarttime": "^3.0.24"
|
||||
},
|
||||
"browserslist": [
|
||||
"last 1 chrome versions"
|
||||
|
@ -27,7 +27,6 @@ Platform support | [ or [contribute monthly](https://lossless.link/contribute). :)
|
||||
|
14
test/test.ts
14
test/test.ts
@ -3,19 +3,25 @@ import * as paypal from '../ts/index';
|
||||
|
||||
import { Qenv } from '@pushrocks/qenv';
|
||||
const testQenv = new Qenv('./', './.nogit/');
|
||||
import * as smarttime from '@pushrocks/smarttime';
|
||||
|
||||
let testPayPalInstance: paypal.PayPal;
|
||||
let testPayPalInstance: paypal.PayPalAccount;
|
||||
|
||||
tap.test('should create a valid paypal instance', async () => {
|
||||
testPayPalInstance = new paypal.PayPal({
|
||||
testPayPalInstance = new paypal.PayPalAccount({
|
||||
clientId: testQenv.getEnvVarOnDemand('PAYPAL_CLIENT_ID'),
|
||||
clientSecret: testQenv.getEnvVarOnDemand('PAYPAL_CLIENT_SECRET'),
|
||||
accountOwner: 'sample corp'
|
||||
});
|
||||
expect(testPayPalInstance).to.be.instanceOf(paypal.PayPal);
|
||||
expect(testPayPalInstance).to.be.instanceOf(paypal.PayPalAccount);
|
||||
});
|
||||
|
||||
tap.test('should get an access token', async () => {
|
||||
await testPayPalInstance.request();
|
||||
const transactions = await testPayPalInstance.getTransactionsFromTo(
|
||||
smarttime.ExtendedDate.fromHyphedDate('2020-01-01').getTime(),
|
||||
smarttime.ExtendedDate.fromHyphedDate('2020-08-01').getTime()
|
||||
);
|
||||
console.log(transactions);
|
||||
});
|
||||
|
||||
tap.start();
|
||||
|
@ -1 +1,2 @@
|
||||
export * from './paypal.classes.paypal';
|
||||
export * from './paypal.classes.account';
|
||||
export * from './paypal.classes.transaction';
|
||||
|
153
ts/paypal.classes.account.ts
Normal file
153
ts/paypal.classes.account.ts
Normal file
@ -0,0 +1,153 @@
|
||||
import * as plugins from './paypal.plugins';
|
||||
import { PayPalTransaction } from './paypal.classes.transaction';
|
||||
|
||||
export interface IPayPalOptions {
|
||||
clientId: string;
|
||||
clientSecret: string;
|
||||
accountOwner: string;
|
||||
}
|
||||
|
||||
export class PayPalAccount {
|
||||
public apiBaseUrl: string = 'https://api.paypal.com';
|
||||
public options: IPayPalOptions;
|
||||
|
||||
private apiToken: string;
|
||||
private apiTokenExpirationTime: number;
|
||||
|
||||
constructor(optionsArg: IPayPalOptions) {
|
||||
this.options = optionsArg;
|
||||
}
|
||||
|
||||
public async getTransactionsFromTo(fromTimeMillisArg: number, toTimeMillisArg: number) {
|
||||
let allTransactions: PayPalTransaction[] = [];
|
||||
// lets note the time from one month before. We need that for accurate transactions pools.
|
||||
const monthBeforeStartMillis =
|
||||
fromTimeMillisArg - plugins.smarttime.getMilliSecondsFromUnits({ days: 30 });
|
||||
do {
|
||||
const transactions = await PayPalTransaction.getTransactionFor30days(this, fromTimeMillisArg);
|
||||
allTransactions = allTransactions.concat(transactions);
|
||||
fromTimeMillisArg =
|
||||
fromTimeMillisArg + plugins.smarttime.getMilliSecondsFromUnits({ days: 30 });
|
||||
} while (fromTimeMillisArg < toTimeMillisArg);
|
||||
|
||||
const invoiceIds: string[] = [];
|
||||
const transactionPools: PayPalTransaction[][] = [];
|
||||
// lets get all invoiceids
|
||||
allTransactions.forEach((transactionArg) => {
|
||||
const invoiceId = transactionArg.data.originApiObject.transaction_info.invoice_id;
|
||||
if (!invoiceIds.includes(invoiceId)) {
|
||||
invoiceIds.push(invoiceId);
|
||||
}
|
||||
});
|
||||
|
||||
// lets get all transactions per invoiceId
|
||||
invoiceIds.forEach((invoiceIdArg) => {
|
||||
const transactionPool: PayPalTransaction[] = [];
|
||||
allTransactions.forEach((transactionArg) => {
|
||||
if (transactionArg.data.originApiObject.transaction_info.invoice_id === invoiceIdArg) {
|
||||
transactionPool.push(transactionArg);
|
||||
}
|
||||
});
|
||||
transactionPools.push(transactionPool);
|
||||
});
|
||||
|
||||
const previousMonthTransactions = await PayPalTransaction.getTransactionFor30days(
|
||||
this,
|
||||
monthBeforeStartMillis
|
||||
);
|
||||
|
||||
transactionPools.forEach((transactionPoolArg) => {
|
||||
const poolInvoiceId = transactionPoolArg[0].data.originApiObject.transaction_info.invoice_id;
|
||||
previousMonthTransactions.forEach((transactionArg) => {
|
||||
if (transactionArg.data.originApiObject.transaction_info.invoice_id === poolInvoiceId) {
|
||||
transactionPoolArg.push(transactionArg);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
let finalTransactions: PayPalTransaction[] = [];
|
||||
// lets process all transactionPool
|
||||
transactionPools.forEach((transactionPoolArg) => {
|
||||
// lets detect foreign transactions
|
||||
let hasForeignTransactions = false;
|
||||
transactionPoolArg.forEach((transactionArg) => {
|
||||
if (transactionArg.data.currency !== 'EUR') {
|
||||
hasForeignTransactions = true;
|
||||
}
|
||||
});
|
||||
if (hasForeignTransactions && transactionPoolArg.length < 4) {
|
||||
console.log(
|
||||
`Pool with invoiceId ${transactionPoolArg[0].data.originApiObject.transaction_info.invoice_id} is not completed yet. Omminiting ${transactionPoolArg.length} transactions as a result.`
|
||||
);
|
||||
return;
|
||||
}
|
||||
if (hasForeignTransactions && transactionPoolArg.length === 4) {
|
||||
const negativeNativeTransaction = transactionPoolArg.find(transactionArg => {
|
||||
return transactionArg.data.amount < 0 && transactionArg.data.currency === 'EUR';
|
||||
});
|
||||
|
||||
const negativeForeignTransaction = transactionPoolArg.find(transactionArg => {
|
||||
return transactionArg.data.amount < 0 && transactionArg.data.currency !== 'EUR';
|
||||
});
|
||||
|
||||
const positiveNativeTransaction = transactionPoolArg.find(transactionArg => {
|
||||
return transactionArg.data.amount > 0 && transactionArg.data.currency === 'EUR';
|
||||
});
|
||||
|
||||
const positiveForeignTransaction = transactionPoolArg.find(transactionArg => {
|
||||
return transactionArg.data.amount > 0 && transactionArg.data.currency !== 'EUR';
|
||||
});
|
||||
|
||||
negativeNativeTransaction.data.name = negativeForeignTransaction.data.name;
|
||||
negativeNativeTransaction.data.description = negativeForeignTransaction.data.description;
|
||||
positiveNativeTransaction.data.name = this.options.accountOwner;
|
||||
positiveNativeTransaction.data.description = 'account balance transfer';
|
||||
transactionPoolArg = transactionPoolArg.filter(transactionArg => transactionArg.data.currency === 'EUR');
|
||||
}
|
||||
if (!hasForeignTransactions && transactionPoolArg.length === 2) {
|
||||
const positiveNativeTransaction = transactionPoolArg.find(transactionArg => {
|
||||
return transactionArg.data.amount > 0 && transactionArg.data.currency === 'EUR';
|
||||
});
|
||||
positiveNativeTransaction.data.name = this.options.accountOwner;
|
||||
positiveNativeTransaction.data.description = 'account balance transfer';
|
||||
}
|
||||
// pool is ready
|
||||
finalTransactions = finalTransactions.concat(transactionPoolArg);
|
||||
});
|
||||
|
||||
return finalTransactions;
|
||||
}
|
||||
|
||||
public async request(methodArg: 'GET' | 'POST', routeArg: string, payloadArg: any) {
|
||||
if (!this.apiToken || this.apiTokenExpirationTime < Date.now()) {
|
||||
const authHeader = `Basic ${plugins.smartstring.base64.encode(
|
||||
`${this.options.clientId}:${this.options.clientSecret}`
|
||||
)}`;
|
||||
const response = await plugins.smartrequest.request(
|
||||
`${this.apiBaseUrl}/v1/oauth2/token?grant_type=client_credentials`,
|
||||
{
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Accept: 'application/json',
|
||||
'Accept-Language': 'en_US',
|
||||
Authorization: authHeader,
|
||||
},
|
||||
keepAlive: false,
|
||||
}
|
||||
);
|
||||
this.apiToken = response.body.access_token;
|
||||
this.apiTokenExpirationTime = Date.now() + response.body.expires_in * 1000 - 600000;
|
||||
}
|
||||
|
||||
// we have a token
|
||||
const response = await plugins.smartrequest.request(`${this.apiBaseUrl}${routeArg}`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
Accept: 'application/json',
|
||||
'Accept-Language': 'en_US',
|
||||
Authorization: `Bearer ${this.apiToken}`,
|
||||
},
|
||||
});
|
||||
return response.body;
|
||||
}
|
||||
}
|
@ -1,51 +0,0 @@
|
||||
import * as plugins from './paypal.plugins';
|
||||
|
||||
export interface IPayPalOptions {
|
||||
clientId: string;
|
||||
clientSecret: string;
|
||||
}
|
||||
|
||||
export class PayPal {
|
||||
public apiBaseUrl: string = 'https://api.paypal.com'
|
||||
public options: IPayPalOptions;
|
||||
|
||||
private apiToken: string;
|
||||
private apiTokenExpirationTime: number;
|
||||
|
||||
constructor(optionsArg: IPayPalOptions) {
|
||||
this.options = optionsArg;
|
||||
}
|
||||
|
||||
public async request() {
|
||||
if (!this.apiToken || this.apiTokenExpirationTime < Date.now()) {
|
||||
const authHeader = `Basic ${plugins.smartstring.base64.encode(
|
||||
`${this.options.clientId}:${this.options.clientSecret}`
|
||||
)}`;
|
||||
const response = await plugins.smartrequest.request(
|
||||
`${this.apiBaseUrl}/v1/oauth2/token?grant_type=client_credentials`,
|
||||
{
|
||||
method: 'POST',
|
||||
headers: {
|
||||
Accept: 'application/json',
|
||||
'Accept-Language': 'en_US',
|
||||
Authorization: authHeader,
|
||||
},
|
||||
keepAlive: false,
|
||||
}
|
||||
);
|
||||
this.apiToken = response.body.access_token;
|
||||
this.apiTokenExpirationTime = Date.now() + response.body.expires_in * 1000 - 600000;
|
||||
}
|
||||
|
||||
// we have a token
|
||||
const response = await plugins.smartrequest.request(`${this.apiBaseUrl}/v1/reporting/transactions?start_date=2020-05-01T00:00:00Z&end_date=2020-05-30T00:00:00Z`, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
Accept: 'application/json',
|
||||
'Accept-Language': 'en_US',
|
||||
Authorization: `Bearer ${this.apiToken}`,
|
||||
},
|
||||
});
|
||||
console.log(response.body);
|
||||
}
|
||||
}
|
@ -1 +1,109 @@
|
||||
import * as plugins from './paypal.plugins'
|
||||
import * as plugins from './paypal.plugins';
|
||||
import { PayPalAccount } from './paypal.classes.account';
|
||||
|
||||
export interface IPayPalOriginTransactionApiObject {
|
||||
transaction_info: {
|
||||
paypal_account_id: string;
|
||||
transaction_id: string;
|
||||
transaction_event_code: string;
|
||||
transaction_initiation_date: string;
|
||||
transaction_updated_date: string;
|
||||
transaction_amount: { currency_code: string; value: string };
|
||||
transaction_status: string;
|
||||
transaction_subject: string;
|
||||
ending_balance: { currency_code: string; value: string };
|
||||
available_balance: { currency_code: string; value: string };
|
||||
invoice_id: string;
|
||||
protection_eligibility: string;
|
||||
};
|
||||
cart_info: {
|
||||
item_details: {
|
||||
item_code: string;
|
||||
item_name: string;
|
||||
item_quantity: string;
|
||||
item_unit_price: {
|
||||
currency_code: string;
|
||||
value: string;
|
||||
};
|
||||
item_amount: {
|
||||
currency_code: string;
|
||||
value: string;
|
||||
};
|
||||
total_item_amount: {
|
||||
currency_code: string;
|
||||
value: string;
|
||||
};
|
||||
invoice_number: string;
|
||||
}[];
|
||||
};
|
||||
payer_info: {
|
||||
account_id: string;
|
||||
email_address: string;
|
||||
address_status: string;
|
||||
payer_status: string;
|
||||
payer_name: {
|
||||
alternate_full_name: string;
|
||||
};
|
||||
country_code: string;
|
||||
};
|
||||
}
|
||||
|
||||
export interface IPayPalTransactionOptions {
|
||||
id: string;
|
||||
originApiObject: IPayPalOriginTransactionApiObject;
|
||||
amount: number;
|
||||
name: string;
|
||||
description: string;
|
||||
currency: 'USD' | 'EUR';
|
||||
timestampIso: string;
|
||||
}
|
||||
|
||||
export class PayPalTransaction {
|
||||
public static async getTransactionFor30days(
|
||||
paypalInstanceArg: PayPalAccount,
|
||||
startPointMillis: number = Date.now() - plugins.smarttime.units.days(30)
|
||||
) {
|
||||
const startDate = startPointMillis;
|
||||
const endDate = startDate + plugins.smarttime.units.days(30);
|
||||
const startDateIso = plugins.smarttime.ExtendedDate.fromMillis(startDate).toISOString();
|
||||
const endDateIso = plugins.smarttime.ExtendedDate.fromMillis(endDate).toISOString();
|
||||
console.log(`getting PayPal transactions from ${startDateIso} + ${endDateIso}`);
|
||||
const response = await paypalInstanceArg.request(
|
||||
'GET',
|
||||
`/v1/reporting/transactions?start_date=${startDateIso}&end_date=${endDateIso}&fields=all`,
|
||||
{}
|
||||
);
|
||||
|
||||
const returnTransactions: PayPalTransaction[] = [];
|
||||
for (const transactionDetail of response.transaction_details) {
|
||||
const apiObject: IPayPalOriginTransactionApiObject = transactionDetail;
|
||||
const paypalTransaction = new PayPalTransaction({
|
||||
originApiObject: apiObject,
|
||||
id: apiObject.transaction_info.transaction_id,
|
||||
amount: parseFloat(apiObject.transaction_info.transaction_amount.value),
|
||||
currency: apiObject.transaction_info.transaction_amount.currency_code as 'EUR' | 'USD',
|
||||
timestampIso: apiObject.transaction_info.transaction_initiation_date,
|
||||
name: `${apiObject.payer_info.payer_name.alternate_full_name} (${apiObject.payer_info.email_address})`,
|
||||
description: `${apiObject.cart_info?.item_details?.length} items: ${apiObject.cart_info?.item_details?.map(itemArg => {
|
||||
return `${itemArg.item_name}`;
|
||||
}).reduce((accumulatorArg, currentValue) => {
|
||||
let returnString = '';
|
||||
if (accumulatorArg) {
|
||||
returnString = accumulatorArg + ', ' + currentValue;
|
||||
} else {
|
||||
returnString = currentValue;
|
||||
}
|
||||
return returnString;
|
||||
})}`,
|
||||
});
|
||||
returnTransactions.push(paypalTransaction);
|
||||
}
|
||||
return returnTransactions;
|
||||
}
|
||||
|
||||
public data: IPayPalTransactionOptions;
|
||||
|
||||
constructor(dataArg: IPayPalTransactionOptions) {
|
||||
this.data = dataArg;
|
||||
}
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
// pupshrocks scope
|
||||
import * as smartrequest from '@pushrocks/smartrequest';
|
||||
import * as smartstring from '@pushrocks/smartstring';
|
||||
import * as smarttime from '@pushrocks/smarttime';
|
||||
|
||||
export { smartrequest, smartstring };
|
||||
export { smartrequest, smartstring, smarttime };
|
||||
|
Loading…
x
Reference in New Issue
Block a user