Skip to content

Commit

Permalink
Merge 9b4dd6e into 4a211bb
Browse files Browse the repository at this point in the history
  • Loading branch information
curtisfennerpaypay committed Jan 18, 2022
2 parents 4a211bb + 9b4dd6e commit ba753e2
Show file tree
Hide file tree
Showing 28 changed files with 318 additions and 66 deletions.
32 changes: 16 additions & 16 deletions README.md
Expand Up @@ -96,7 +96,7 @@ let payload = {
};

const response = await PAYPAY.QRCodeCreate(payload);
const body = JSON.parse(response.BODY);
const body = response.BODY;
console.log(response.STATUS, body.resultInfo.code);
```

Expand All @@ -117,7 +117,7 @@ Now that you have created a Code, the next step is to implement polling to get
let merchantPaymentId = 'merchantPaymentId';

const response = await PAYPAY.GetCodePaymentDetails([merchantPaymentId]);
const body = JSON.parse(response.BODY);
const body = response.BODY;
console.log(body.resultInfo.code);
console.log(body.data.status);
```
Expand All @@ -139,7 +139,7 @@ Following are the important parameters that you can provide for this method:

```javascript
const response = await PAYPAY.QRCodeDelete([codeId]);
const body = JSON.parse(response.BODY);
const body = response.BODY;
console.log(body.resultInfo.code);
```

Expand All @@ -162,7 +162,7 @@ Following are the important parameters that you can provide for this method:

```javascript
const response = await PAYPAY.PaymentCancel([merchantPaymentId]);
const body = JSON.parse(response.BODY);
const body = response.BODY;
console.log(body.resultInfo.code);
```

Expand Down Expand Up @@ -190,7 +190,7 @@ let payload = {
};

const response = await PAYPAY.PaymentRefund(payload);
const body = JSON.parse(response.BODY);
const body = response.BODY;
console.log(body.resultInfo.code);
```

Expand Down Expand Up @@ -220,7 +220,7 @@ let payload = {
};

const response = await PAYPAY.PaymentAuthCapture(payload);
const body = JSON.parse(response.BODY);
const body = response.BODY;
console.log(body.resultInfo.code);
```
For details of all the request and response parameters , check our [API Documentation guide](https://www.paypay.ne.jp/opa/doc/v1.0/dynamicqrcode#operation/capturePaymentAuth).
Expand All @@ -243,7 +243,7 @@ let payload = {
};

const response = await PAYPAY.PaymentAuthRevert(payload);
const body = JSON.parse(response.BODY);
const body = response.BODY:
console.log(body.resultInfo.code);
```
For List of params refer to the API guide :
Expand All @@ -260,7 +260,7 @@ So you want to confirm the status of the refund, maybe because the request for t
```javascript
let merchantPaymentId = 'merchantRefundId';
const response = await PAYPAY.GetRefundDetails([merchantRefundId]);
const body = JSON.parse(response.BODY);
const body = response.BODY;
console.log(body.resultInfo.code);
```
For details of all the request and response parameters , check our [API Documentation guide](https://www.paypay.ne.jp/opa/doc/v1.0/dynamicqrcode#operation/getRefundDetails).
Expand Down Expand Up @@ -305,7 +305,7 @@ let payload = {
};
// Calling the method to create the account linking QR Code
const response = await PAYPAY.AccountLinkQRCodeCreate(payload);
const body = JSON.parse(response.BODY);
const body = response.BODY;

// Printing if the method call was SUCCESS
console.log(body.resultInfo.code);
Expand All @@ -332,7 +332,7 @@ const userAuthorizationId = jwtResponse["userAuthorizationId"];
```javascript
// Calling the method to unlink a Payment
const result = await PAYPAY.unlinkUser([userAuthorizationId]);
const body = JSON.parse(result.BODY);
const body = result.BODY;

// Printing if the method call was SUCCESS
console.log(body.resultInfo.code);
Expand Down Expand Up @@ -365,7 +365,7 @@ let payload = {

// Calling the method to create a payment
const response = await PAYPAY.CreatePayment(payload);
const body = JSON.parse(response.BODY);
const body = response.BODY;

// Printing if the method call was SUCCESS
console.log(body.resultInfo.code);
Expand Down Expand Up @@ -424,7 +424,7 @@ Now that you have created a payment, in case the payment request timeout, you ca
```javascript
let merchantPaymentId = 'merchantPaymentId';
const response = await PAYPAY.GetPaymentDetails([merchantPaymentId]);
const body = JSON.parse(response.BODY);
const body = response.BODY;

// Printing if the method call was SUCCESS, this does not mean the payment was a success
console.log(body.resultInfo.code);
Expand Down Expand Up @@ -454,7 +454,7 @@ Following are the important parameters that you can provide for this method:

```javascript
const response = await PAYPAY.PaymentCancel([merchantPaymentId]);
const body = JSON.parse(response.BODY);
const body = response.BODY;
// Printing if the method call was SUCCESS
console.log(body.resultInfo.code);
```
Expand All @@ -475,7 +475,7 @@ So the user has decided to return the goods they have purchased and needs to be
|reason | No |integer <= 11 characters |The reason for refund |

```javascript
let payload = {
let payload = {
merchantRefundId: 'merchant_refund_id',
paymentId: 'paypay_payment_id',
amount: {
Expand All @@ -486,7 +486,7 @@ So the user has decided to return the goods they have purchased and needs to be
};
// Calling the method to refund a Payment
const response = await PAYPAY.PaymentRefund(payload);
const body = JSON.parse(response.BODY);
const body = response.BODY;
// Printing if the method call was SUCCESS
console.log(body.resultInfo.code);
});
Expand All @@ -509,7 +509,7 @@ So you want to confirm the status of the refund, maybe because the request for t
```javascript
let merchantPaymentId = 'merchantRefundId'
const response = await PAYPAY.GetRefundDetails([merchantRefundId]);
const body = JSON.parse(response.BODY);
const body = response.BODY;
// Printing if the method call was SUCCESS
console.log(body.resultInfo.code);
```
Expand Down
19 changes: 15 additions & 4 deletions src/lib/httpsClient.ts
Expand Up @@ -2,7 +2,7 @@ import * as https from "https";

export interface HttpsClientSuccess {
STATUS: number;
BODY: string;
BODY: object | null;
}

export interface HttpsClientError {
Expand Down Expand Up @@ -52,7 +52,20 @@ export class HttpsClient {
if (status < 200 || status > 299) {
this.printDebugMessage(status, body, apiName);
}
callback({ STATUS: status, BODY: body });
let parsed;
try {
parsed = body.match(/\S/) ? JSON.parse(body) : null;
} catch (e: any) {
callback({ STATUS: 500, ERROR: e.message });
return;
}

// Make the `BODY.toString()` return the raw JSON.
// This makes the library compatible with calls like `JSON.parse(response.BODY)`,
// which were required prior to version 2.
const responseObject = parsed && Object.assign(Object.create({ toString() { return body; } }), parsed);

callback({ STATUS: status, BODY: responseObject });
});
});

Expand All @@ -66,5 +79,3 @@ export class HttpsClient {
req.end();
}
}

export const httpsClient = new HttpsClient();
12 changes: 10 additions & 2 deletions src/lib/paypay-rest-sdk.ts
Expand Up @@ -3,7 +3,7 @@
*/
import { Auth } from "./auth";
import { Conf } from "./conf";
import { httpsClient, HttpsClientError, HttpsClientMessage, HttpsClientSuccess } from "./httpsClient";
import { HttpsClient, HttpsClientError, HttpsClientMessage, HttpsClientSuccess } from "./httpsClient";
import { HmacSHA256, enc, algo } from "crypto-js";
import { v4 as uuidv4 } from "uuid";
import * as jwt from "jsonwebtoken";
Expand All @@ -19,12 +19,20 @@ class PayPayRestSDK {
private perfMode: boolean = false;
private readonly auth: Auth;
private config: Conf;
private httpsClient: HttpsClient = new HttpsClient();

constructor() {
this.config = new Conf(this.productionMode, this.perfMode);
this.auth = new Auth();
}

/**
* Replace the HttpsClient that this SDK client instance uses for API calls.
*/
setHttpsClient(httpsClient: HttpsClient) {
this.httpsClient = httpsClient;
}

/**
* Set authentication passed by end-user
* @param clientConfig
Expand Down Expand Up @@ -130,7 +138,7 @@ class PayPayRestSDK {
callback?: HttpsClientMessage): Promise<HttpsClientSuccess | HttpsClientError> => {
const options = this.paypaySetupOptions(endpoint, payload);
return new Promise((resolve) => {
httpsClient.httpsCall(options, payload, (result) => {
this.httpsClient.httpsCall(options, payload, (result) => {
resolve(result);
if (callback !== undefined) {
callback(result);
Expand Down
5 changes: 4 additions & 1 deletion test/accountLinkQRCodeCreate.test.ts
@@ -1,5 +1,5 @@
import { payPayRestSDK } from "../src/lib/paypay-rest-sdk";
import { httpsClient } from '../src/lib/httpsClient';
import { HttpsClient } from '../src/lib/httpsClient';

const conf = {
clientId: '5345435fsdfsr54353454',
Expand All @@ -10,6 +10,9 @@ const conf = {

payPayRestSDK.configure(conf);

const httpsClient = new HttpsClient();
payPayRestSDK.setHttpsClient(httpsClient);

test('Unit Test - Account Link Create QR code', async () => {

const response = {
Expand Down
5 changes: 4 additions & 1 deletion test/cancelPayment.test.ts
@@ -1,5 +1,5 @@
import { payPayRestSDK } from "../src/lib/paypay-rest-sdk";
import { httpsClient } from '../src/lib/httpsClient';
import { HttpsClient } from '../src/lib/httpsClient';

const conf = {
clientId: '5345435fsdfsr54353454',
Expand All @@ -10,6 +10,9 @@ const conf = {

payPayRestSDK.configure(conf);

const httpsClient = new HttpsClient();
payPayRestSDK.setHttpsClient(httpsClient);

test('Unit Test - Cancel Payment', async () => {

const merchantPaymentId = [12393849];
Expand Down
5 changes: 4 additions & 1 deletion test/cancelPendingOrder.test.ts
@@ -1,5 +1,5 @@
import { payPayRestSDK } from "../src/lib/paypay-rest-sdk";
import { httpsClient } from '../src/lib/httpsClient';
import { HttpsClient } from '../src/lib/httpsClient';

const conf = {
clientId: '5345435fsdfsr54353454',
Expand All @@ -10,6 +10,9 @@ const conf = {

payPayRestSDK.configure(conf);

const httpsClient = new HttpsClient();
payPayRestSDK.setHttpsClient(httpsClient);

test('Unit Test - Cancel Pending Order', async () => {

const merchantPaymentId = [12393849];
Expand Down
5 changes: 4 additions & 1 deletion test/cashback.test.ts
@@ -1,5 +1,5 @@
import { payPayRestSDK } from "../src/lib/paypay-rest-sdk";
import { httpsClient } from '../src/lib/httpsClient';
import { HttpsClient } from '../src/lib/httpsClient';
const { v4: uuidv4 } = require('uuid');

const conf = {
Expand All @@ -11,6 +11,9 @@ const conf = {

payPayRestSDK.configure(conf);

const httpsClient = new HttpsClient();
payPayRestSDK.setHttpsClient(httpsClient);

test('Unit Test - Cash Back', async () => {

const payload = {
Expand Down
28 changes: 25 additions & 3 deletions test/checkCashBackDetails.test.ts
@@ -1,5 +1,5 @@
import { payPayRestSDK } from "../src/lib/paypay-rest-sdk";
import { httpsClient } from '../src/lib/httpsClient';
import { HttpsClient } from '../src/lib/httpsClient';

const conf = {
clientId: 'testId',
Expand All @@ -10,13 +10,35 @@ const conf = {

payPayRestSDK.configure(conf);

const httpsClient = new HttpsClient();
payPayRestSDK.setHttpsClient(httpsClient);

test('Unit Test - Check cashback details', async () => {

const merchantPaymentId = [12393849];
const response = {
STATUS: 200,
BODY: '{"resultInfo":{"code":"SUCCESS","message":"Success","codeId":"08100001"},"data":{"status":"SUCCESS","acceptedAt":1611747653,"merchantAlias":"test","amount":{"amount":1,"currency":"JPY"},"requestedAt":1611747650,"metadata":"","cashbackId":"test","merchantCashbackId":"test","userAuthorizationId":"test","orderDescription":"order description","walletType":"PREPAID"}}'
}
BODY: {
"resultInfo": {
"code": "SUCCESS",
"message": "Success",
"codeId": "08100001",
},
"data": {
"status": "SUCCESS",
"acceptedAt": 1611747653,
"merchantAlias": "test",
"amount": { "amount": 1, "currency": "JPY" },
"requestedAt": 1611747650,
"metadata": "",
"cashbackId": "test",
"merchantCashbackId": "test",
"userAuthorizationId": "test",
"orderDescription": "order description",
"walletType": "PREPAID",
},
},
};

const mockHttpsCall = jest.spyOn(httpsClient, 'httpsCall');
mockHttpsCall.mockImplementation(jest.fn((_options: any, _payload = '', _callback: any) => {
Expand Down
26 changes: 23 additions & 3 deletions test/checkCashBackReversalDetails.test.ts
@@ -1,5 +1,5 @@
import { payPayRestSDK } from "../src/lib/paypay-rest-sdk";
import { httpsClient } from '../src/lib/httpsClient';
import { HttpsClient } from '../src/lib/httpsClient';

const conf = {
clientId: 'test',
Expand All @@ -10,13 +10,33 @@ const conf = {

payPayRestSDK.configure(conf);

const httpsClient = new HttpsClient();
payPayRestSDK.setHttpsClient(httpsClient);

test('Unit Test - Check cashback reversal details', async () => {

const merchantPaymentId = [12393849];
const response = {
STATUS: 200,
BODY: '{"resultInfo":{"code":"SUCCESS","message":"Success","codeId":"08100001"},"data":{"status":"SUCCESS","acceptedAt":1611747702,"merchantAlias":"test","amount":{"amount":1,"currency":"JPY"},"requestedAt":1611747699,"metadata":"","cashbackReversalId":"test","merchantCashbackReversalId":"test","merchantCashbackId":"test"}}'
}
BODY: {
"resultInfo": {
"code": "SUCCESS",
"message": "Success",
"codeId": "08100001",
},
"data": {
"status": "SUCCESS",
"acceptedAt": 1611747702,
"merchantAlias": "test",
"amount": { "amount": 1, "currency": "JPY" },
"requestedAt": 1611747699,
"metadata": "",
"cashbackReversalId": "test",
"merchantCashbackReversalId": "test",
"merchantCashbackId": "test",
},
},
};

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

0 comments on commit ba753e2

Please sign in to comment.