Skip to content

Commit

Permalink
Merge pull request #102 from shashi-pp/feature/user_authorization
Browse files Browse the repository at this point in the history
Feature/user authorization
  • Loading branch information
Shreyansh Pandey authored Oct 13, 2020
2 parents 05b0ae1 + 9bec917 commit 824cc39
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 8 deletions.
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ export = {
GetPendingPaymentDetails: payPayRestSDK.getPendingPaymentDetails,
CancelPendingOrder: payPayRestSDK.cancelPendingOrder,
RefundPendingPayment: payPayRestSDK.refundPendingPayment,
GetUserAuthorizationStatus: payPayRestSDK.getUserAuthorizationStatus,
UnlinkUser: payPayRestSDK.unlinkUser,
};
12 changes: 11 additions & 1 deletion src/lib/conf.prod.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,15 @@
"METHOD": "POST",
"PATH": "/v1/requestOrder/refunds"
}
}
},
"USER_AUTHORIZATION": {
"GET_USER_AUTHORIZATION_STATUS": {
"METHOD": "GET",
"PATH": "/v2/user/authorizations?userAuthorizationId={userAuthorizationId}"
},
"UNLINK_USER": {
"METHOD": "DELETE",
"PATH": "/v2/user/authorizations/{userAuthorizationId}"
}
}
}
12 changes: 11 additions & 1 deletion src/lib/conf.stage.json
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,15 @@
"METHOD": "POST",
"PATH": "/v1/requestOrder/refunds"
}
}
},
"USER_AUTHORIZATION": {
"GET_USER_AUTHORIZATION_STATUS": {
"METHOD": "GET",
"PATH": "/v2/user/authorizations?userAuthorizationId={userAuthorizationId}"
},
"UNLINK_USER": {
"METHOD": "DELETE",
"PATH": "/v2/user/authorizations/{userAuthorizationId}"
}
}
}
39 changes: 33 additions & 6 deletions src/lib/paypay-rest-sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class PayPayRestSDK {

this.options.path = this.config.getHttpsPath(nameApi, nameMethod);
this.options.method = this.config.getHttpsMethod(nameApi, nameMethod);
let cleanPath = this.options.path.split("?")[0];

if (this.options.method === "GET" || this.options.method === "DELETE") {
queryParams = this.options.path.match(/{\w+}/g);
Expand All @@ -97,12 +98,12 @@ class PayPayRestSDK {
this.options.path = this.options.path.replace(q, input[n]);
});
}
} else if (this.options.method === 'POST') {
} else if (this.options.method === "POST") {
input.requestedAt = Math.round(new Date().getTime() / 1000);
}

const authHeader = this.createAuthHeader(this.options.method,
this.options.path,
cleanPath,
this.options.method === "GET" || this.options.method === "DELETE" ? null : input,
auth);
this.setHttpsOptions(authHeader);
Expand Down Expand Up @@ -314,7 +315,7 @@ class PayPayRestSDK {
* @param {Object} payload JSON object payload
*/
public createPendingPayment = (payload: any, callback: HttpsClientMessage): void => {
httpsClient.httpsCall(this.paypaySetupOptions('API_REQUEST_ORDER', 'PENDING_PAYMENT_CREATE', payload), payload, (result: any) => {
httpsClient.httpsCall(this.paypaySetupOptions("API_REQUEST_ORDER", "PENDING_PAYMENT_CREATE", payload), payload, (result: any) => {
callback(result);
})
}
Expand All @@ -327,7 +328,7 @@ class PayPayRestSDK {
* @param {string} inputParams Array of merchantPaymentId : The unique payment transaction id provided by merchant
*/
public getPendingPaymentDetails = (inputParams: Array<string | number>, callback: HttpsClientMessage): void => {
httpsClient.httpsCall(this.paypaySetupOptions('API_REQUEST_ORDER', 'GET_ORDER_DETAILS', inputParams), '', (result: any) => {
httpsClient.httpsCall(this.paypaySetupOptions("API_REQUEST_ORDER", "GET_ORDER_DETAILS", inputParams), "", (result: any) => {
callback(result);
})
}
Expand All @@ -340,7 +341,7 @@ class PayPayRestSDK {
* @param {string} inputParams Array of merchantPaymentId : The unique payment transaction id provided by merchant
*/
public cancelPendingOrder = (inputParams: Array<string | number>, callback: HttpsClientMessage): void => {
httpsClient.httpsCall(this.paypaySetupOptions('API_REQUEST_ORDER', 'PENDING_ORDER_CANCEL', inputParams), '', (result: any) => {
httpsClient.httpsCall(this.paypaySetupOptions("API_REQUEST_ORDER", "PENDING_ORDER_CANCEL", inputParams), "", (result: any) => {
callback(result);
})
}
Expand All @@ -353,10 +354,36 @@ class PayPayRestSDK {
* @param {Object} payload JSON object payload
*/
public refundPendingPayment = (payload: any, callback: HttpsClientMessage): void => {
httpsClient.httpsCall(this.paypaySetupOptions('API_REQUEST_ORDER', 'PAYMENT_REFUND', payload), payload, (result: any) => {
httpsClient.httpsCall(this.paypaySetupOptions("API_REQUEST_ORDER", "PAYMENT_REFUND", payload), payload, (result: any) => {
callback(result);
})
}

/**
* Get user authorization status
*
* @callback Callback function to handle result
* @returns {Object} Returns result containing STATUS and BODY
* @param {string} inputParams Array of UserAuthorizationId : The unique UserAuthorizationId id
*/
public getUserAuthorizationStatus = (inputParams: Array<string | number>, callback: HttpsClientMessage): void => {
httpsClient.httpsCall(this.paypaySetupOptions("USER_AUTHORIZATION", "GET_USER_AUTHORIZATION_STATUS", inputParams), "", (result: any) => {
callback(result);
})
}

/**
* Unlink user
*
* @callback Callback function to handle result
* @returns {Object} Returns result containing STATUS and BODY
* @param {string} inputParams Array of UserAuthorizationId : The unique UserAuthorizationId id
*/
public unlinkUser = (inputParams: Array<string | number>, callback: HttpsClientMessage): void => {
httpsClient.httpsCall(this.paypaySetupOptions("USER_AUTHORIZATION", "UNLINK_USER", inputParams), "", (result: any) => {
callback(result);
})
}
}

/**
Expand Down
45 changes: 45 additions & 0 deletions test/getUserAuthorizationStatus.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { payPayRestSDK } from "../src/lib/paypay-rest-sdk";
import { httpsClient } from '../src/lib/httpsClient';

const conf = {
clientId: '5345435fsdfsr54353454',
clientSecret: 'dgfgdfgt46435gsdr35tte5',
merchantId: '2473982',
productionMode: false
};

payPayRestSDK.configure(conf);

test('Unit Test - Get User Authorization Status', async () => {
const userAuthorizationId = [6787435345];
const response = {
"resultInfo": {
"code": "SUCCESS",
"message": "Success",
"codeId": "08100001"
},
"data": {
"userAuthorizationId": "6787435345",
"referenceIds": [ ],
"status": "ACTIVE",
"scopes": [
"continuous_payments"
],
"expireAt": 34534543534,
"issuedAt": 45353535343
}
};

const mockHttpsCall = jest.spyOn(httpsClient, 'httpsCall');
mockHttpsCall.mockImplementation(jest.fn((_options: any, _payload = '', _callback: any) => {
_callback(response);
}));

await payPayRestSDK.getUserAuthorizationStatus(userAuthorizationId, (result: any) => {
expect(result).toEqual(response);
});

expect(mockHttpsCall).toHaveBeenCalledTimes(1);

mockHttpsCall.mockClear();
});
35 changes: 35 additions & 0 deletions test/unlinkUser.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { payPayRestSDK } from "../src/lib/paypay-rest-sdk";
import { httpsClient } from '../src/lib/httpsClient';

const conf = {
clientId: '5345435fsdfsr54353454',
clientSecret: 'dgfgdfgt46435gsdr35tte5',
merchantId: '2473982',
productionMode: false
};

payPayRestSDK.configure(conf);

test('Unit Test - Unlink user', async () => {
const userAuthorizationId = [6787435345];
const response = {
"resultInfo": {
"code": "SUCCESS",
"message": "Success",
"codeId": "08100001"
}
};

const mockHttpsCall = jest.spyOn(httpsClient, 'httpsCall');
mockHttpsCall.mockImplementation(jest.fn((_options: any, _payload = '', _callback: any) => {
_callback(response);
}));

await payPayRestSDK.unlinkUser(userAuthorizationId, (result: any) => {
expect(result).toEqual(response);
});

expect(mockHttpsCall).toHaveBeenCalledTimes(1);

mockHttpsCall.mockClear();
});

0 comments on commit 824cc39

Please sign in to comment.