Skip to content

Commit

Permalink
authorization function
Browse files Browse the repository at this point in the history
  • Loading branch information
infysumanta committed Feb 4, 2024
1 parent b12c01b commit 60e3be2
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 30 deletions.
15 changes: 13 additions & 2 deletions index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
import { version } from './package.json';
console.log(version);
import Paypal from "./src/index";

const clientId = "AT3frCPisQkPVvDpj9bSuku6QSvnf3KTryekADMVmPScg-1QtUBkr2Hxj-IJkQtq_qtwFc0IG5mVt4-B";
const clientSecret = "EJFSX2jdHTvYXm6KDdthkdqWjHUK8HNdMwd0yUFAV1X7lzyAQnWBtaG87plMfDkcTy6D44kJsZt7FgYx";
const environment = "sandbox";

const paypalClient = new Paypal({ clientId, clientSecret, environment });

(async () => {
const token = await paypalClient.authorization.getToken();
console.log(token);
await paypalClient.authorization.terminateAccessToken(token);
})();
35 changes: 14 additions & 21 deletions src/paypal.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { Environment } from './constant';
import { generateAccessToken, getToken } from './resource/Authorization';
import { getRequestUrl } from './utils';
import { Environment } from "./constant";
import { generateAccessToken, getToken, terminateAccessToken } from "./resource/Authorization";
import { getRequestUrl } from "./utils";

export class Paypal {
clientId: string;
clientSecret: string;
environment: Environment;
requestUrl: string;


authorization: {
generateAccessToken: () => Promise<any>;
getToken: () => Promise<string>;
terminateAccessToken: (token: string) => Promise<any>;
};
orders: {};
payments: {};
Expand All @@ -24,25 +24,12 @@ export class Paypal {
disputes: {};
paymentMethodsTokens: {};



constructor(
{
clientId,
clientSecret,
environment
}: {
clientId: string;
clientSecret: string;
environment: Environment;
}
) {
constructor({ clientId, clientSecret, environment }: { clientId: string; clientSecret: string; environment: Environment }) {
this.clientId = clientId;
this.clientSecret = clientSecret;
this.environment = environment;
this.requestUrl = getRequestUrl(environment);


// method binding
this.authorization = this._authorization;
this.orders = {};
Expand All @@ -57,11 +44,17 @@ export class Paypal {
this.paymentMethodsTokens = {};
}


private _authorization = {
generateAccessToken: async () => await generateAccessToken(this.clientId, this.clientSecret, this.requestUrl),
getToken: async () => await getToken(this.clientId, this.clientSecret, this.requestUrl)
getToken: async () => await getToken(this.clientId, this.clientSecret, this.requestUrl),
terminateAccessToken: async (token: string) => await terminateAccessToken(token, this.requestUrl),
};

private async getToken() {
return await getToken(this.clientId, this.clientSecret, this.requestUrl);
}


private async revokeToken(token: string): Promise<void> {
await terminateAccessToken(token, this.requestUrl);
}
}
23 changes: 16 additions & 7 deletions src/resource/Authorization.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import axios, { AxiosRequestConfig } from "axios";
import { post } from "../utils";

export async function generateAccessToken(
clientId: string,
Expand Down Expand Up @@ -35,23 +36,31 @@ export async function generateAccessToken(
return response.data;
}

export async function getToken(
clientId: string,
clientSecret: string,
requestUrl: string
): Promise<string> {
export async function getToken(clientId: string, clientSecret: string, requestUrl: string): Promise<string> {
const token = await generateAccessToken(clientId, clientSecret, requestUrl);
return token.access_token;
}

export async function terminateAccessToken() {
console.log("terminateAccessToken");
export async function terminateAccessToken(token: string, requestUrl: string): Promise<void> {
await post({
url: `${requestUrl}/v1/oauth2/token/terminate`,
header: {
"Content-Type": "application/x-www-form-urlencoded",
Authorization: "Bearer " + token,
},
data: {
token: token,
token_type_hint: "ACCESS_TOKEN",
},
});
}

// @todo: imeplement the following methods userInfo
export async function userInfo() {
console.log("userInfo");
}

// @todo: imeplement the following methods generateClientToken
export async function generateClientToken() {
console.log("generateClientToken");
}

0 comments on commit 60e3be2

Please sign in to comment.