fix(core): update
This commit is contained in:
62
ts/paypal.classes.account.ts
Normal file
62
ts/paypal.classes.account.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
import * as plugins from './paypal.plugins';
|
||||
import { PayPalTransaction } from './paypal.classes.transaction';
|
||||
|
||||
export interface IPayPalOptions {
|
||||
clientId: string;
|
||||
clientSecret: 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 returnTransactions: PayPalTransaction[] = [];
|
||||
do {
|
||||
const transactions = await PayPalTransaction.getTransactionFor30days(this, fromTimeMillisArg);
|
||||
returnTransactions = returnTransactions.concat(transactions);
|
||||
fromTimeMillisArg = fromTimeMillisArg + plugins.smarttime.getMilliSecondsFromUnits({days: 30});
|
||||
} while (fromTimeMillisArg < toTimeMillisArg);
|
||||
return returnTransactions;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user