Skip to content

Commit

Permalink
Merge pull request #13 from caub/mixins
Browse files Browse the repository at this point in the history
Refactor to mixins
  • Loading branch information
linyows authored Jun 4, 2019
2 parents f0f627d + 01346fe commit 277f497
Show file tree
Hide file tree
Showing 18 changed files with 183 additions and 381 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ Purchase example
### Node.js

```js
const GMOPG = require('gmopg').GMOPG;
const {default: GMOPG, ENUMS} = require('gmopg');

const gmopg = GMOPG.CREATE({
const gmopg = new GMOPG({
axios: { baseURL: 'https://p01.mul-pay.jp' },
SiteID: 'Your SiteID',
SitePass: 'Your SitePass',
Expand All @@ -40,22 +40,22 @@ const amount = 1234

gmopg.entryTran({
OrderID: orderID,
JobCd: GMOPG.ENUMS.JobCd.Auth,
JobCd: ENUMS.JobCd.Auth,
Amount: amount
}).then((entryRes) => {
gmopg.execTran({
AccessID: entryRes.AccessID,
AccessPass: entryRes.AccessPass,
OrderID: orderID,
Method: GMOPG.ENUMS.Method.Lump,
Method: ENUMS.Method.Lump,
CardNo: '1234123412341234',
Expire: '2024',
SecurityCode: '123'
}).then((execRes) => {
gmopg.alterTran({
AccessID: entryRes.AccessID,
AccessPass: entryRes.AccessPass,
JobCd: GMOPG.ENUMS.JobCd.Sales,
JobCd: ENUMS.JobCd.Sales,
Amount: amount
}).then((alterRes) => {
console.log(alterRes)
Expand All @@ -67,7 +67,7 @@ gmopg.entryTran({
### TypeScript

```ts
import GMOPG from 'gmopg'
import GMOPG, {ENUMS} from 'gmopg'

const gmopg = new GMOPG({
axios: { baseURL: 'https://p01.mul-pay.jp' },
Expand All @@ -82,15 +82,15 @@ const amount = 1234

const entryRes = await gmopg.entryTran({
OrderID: orderID,
JobCd: GMOPG.ENUMS.JobCd.Auth,
JobCd: ENUMS.JobCd.Auth,
Amount: amount
})

const execRes = await gmopg.execTran({
AccessID: entryRes.AccessID,
AccessPass: entryRes.AccessPass,
OrderID: orderID,
Method: GMOPG.ENUMS.Method.Lump,
Method: ENUMS.Method.Lump,
CardNo: '1234123412341234',
Expire: '2024',
SecurityCode: '123'
Expand All @@ -99,7 +99,7 @@ const execRes = await gmopg.execTran({
const alterRes = await gmopg.alterTran({
AccessID: entryRes.AccessID,
AccessPass: entryRes.AccessPass,
JobCd: GMOPG.ENUMS.JobCd.Sales,
JobCd: ENUMS.JobCd.Sales,
Amount: amount
})
```
Expand Down
2 changes: 1 addition & 1 deletion src/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ test.beforeEach((t) => {
})

test('.post requests body correctly', async (t) => {
t.context.client.options = {
t.context.client.config.axios = {
adapter: async (config: AxiosRequestConfig) => {
const response: AxiosResponse = {
data: 'AccessID=accessid&AccessPass=accesspass',
Expand Down
14 changes: 11 additions & 3 deletions src/client.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
import {AxiosInstance, AxiosResponse} from 'axios'
import Axios, {AxiosInstance, AxiosResponse} from 'axios'
import {BadRequest} from './errors'
import {IConfig} from './config.interface'
import {buildByEnv, defaults} from './config'
import * as qs from 'qs'
import * as merge from 'deepmerge'

export default class Client {
public client: AxiosInstance
public options: object = {}
public config: IConfig

constructor(config: IConfig = {}) {
this.config = merge(merge(defaults, config), buildByEnv())
this.client = Axios.create(this.config.axios)
}

public async post(endpoint: string, data: any): Promise<any> {
const res: AxiosResponse = await this.client.post(endpoint, qs.stringify(data, { encode: false }), this.options)
const res: AxiosResponse = await this.client.post(endpoint, qs.stringify(data, {encode: false}), this.config.axios)
const parsed: any = qs.parse(res.data)

if (this.isError(parsed)) {
Expand Down
31 changes: 14 additions & 17 deletions src/client/cardable.test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
import anyTest, {TestInterface} from 'ava'
import test from 'ava'
import Axios, {AxiosRequestConfig, AxiosResponse} from 'axios'
import Cardable from './cardable'
import Client from '../client'
import WithCardable from './cardable'
import {SeqMode} from '../client.enum'
import {IDeleteCardResult, ISaveCardResult, ISearchCardResult} from './cardable.interface'

interface Context {
card: Cardable
}
const Cardable = WithCardable(Client)
let card: any

const test = anyTest as TestInterface<Context>;

test.beforeEach((t) => {
const card = new Cardable()
test.beforeEach(() => {
card = new Cardable()
card.client = Axios.create({})
t.context.card = card
})

test('.defaultCardData returns default object', async (t) => {
const res = await t.context.card.defaultCardData()
const res = await card.defaultCardData()
const expect = {
SiteID: undefined,
SitePass: undefined,
Expand All @@ -27,7 +24,7 @@ test('.defaultCardData returns default object', async (t) => {
})

test('.saveCard calls API and returns response', async (t) => {
t.context.card.options = {
card.config.axios = {
adapter: async (config: AxiosRequestConfig) => {
const response: AxiosResponse = {
data: 'CardSeq=cardseq&CardNo=cardno&Forward=forward&Brand=brand',
Expand All @@ -46,7 +43,7 @@ test('.saveCard calls API and returns response', async (t) => {
SitePass: 'sitepass',
MemberID: 'memberid'
}
const res = await t.context.card.saveCard(args)
const res = await card.saveCard(args)

const expect: ISaveCardResult = {
CardSeq: 'cardseq',
Expand All @@ -58,7 +55,7 @@ test('.saveCard calls API and returns response', async (t) => {
})

test('.deleteCard calls API and returns response', async (t) => {
t.context.card.options = {
card.config.axios = {
adapter: async (config: AxiosRequestConfig) => {
const response: AxiosResponse = {
data: 'CardSeq=cardseq',
Expand All @@ -79,7 +76,7 @@ test('.deleteCard calls API and returns response', async (t) => {
SeqMode: SeqMode.Logic,
CardSeq: 'cardseq'
}
const res = await t.context.card.deleteCard(args)
const res = await card.deleteCard(args)

const expect: IDeleteCardResult = {
CardSeq: 'cardseq'
Expand All @@ -88,7 +85,7 @@ test('.deleteCard calls API and returns response', async (t) => {
})

test('.searchCard calls API and returns response', async (t) => {
t.context.card.options = {
card.config.axios = {
adapter: async (config: AxiosRequestConfig) => {
const response: AxiosResponse = {
data: 'CardSeq=cardseq&DefaultFlag=1&CardName=cardname&CardNo=cardno&Expire=expire&HolderName=holdername&DeleteFlag=0',
Expand All @@ -109,7 +106,7 @@ test('.searchCard calls API and returns response', async (t) => {
SeqMode: SeqMode.Logic,
CardSeq: 'cardseq'
}
const res = await t.context.card.searchCard(args)
const res = await card.searchCard(args)

const result: ISearchCardResult = {
CardSeq: 'cardseq',
Expand Down
26 changes: 7 additions & 19 deletions src/client/cardable.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {AxiosInstance} from 'axios'
import * as merge from 'deepmerge'
import Client from '../client'
import {IConfig} from '../config.interface'
import {Constructor} from '../util'
import {
IDeleteCardArgs,
IDeleteCardResult,
Expand All @@ -11,24 +10,13 @@ import {
ISearchCardResult
} from './cardable.interface'

export default class Cardable extends Client {
public name: string = 'Cardable'
public config: IConfig
public client: AxiosInstance
public options: object = {}

export default <T extends Constructor<Client>>(Base: T) => class Cardable extends Base {
public defaultCardData(): any {
let siteID
let sitePass

if (this.config !== undefined) {
siteID = this.config.SiteID
sitePass = this.config.SitePass
}
const {SiteID, SitePass} = this.config

return {
SiteID: siteID,
SitePass: sitePass,
SiteID,
SitePass,
MemberID: undefined
}
}
Expand All @@ -37,14 +25,14 @@ export default class Cardable extends Client {
const data: ISaveCardArgs = merge(this.defaultCardData(), args)
const parsed: any = await this.post('/payment/SaveCard.idPass', data)

return <ISaveCardResult> parsed
return <ISaveCardResult>parsed
}

public async deleteCard(args: IDeleteCardArgs): Promise<IDeleteCardResult> {
const data: IDeleteCardArgs = merge(this.defaultCardData(), args)
const parsed: any = await this.post('/payment/DeleteCard.idPass', data)

return <IDeleteCardResult> parsed
return <IDeleteCardResult>parsed
}

public async searchCard(args: ISearchCardArgs): Promise<ISearchCardResult[]> {
Expand Down
25 changes: 13 additions & 12 deletions src/client/cvsTranable.test.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
import anyTest, {TestInterface} from 'ava'
import test from 'ava'
import Axios, {AxiosRequestConfig, AxiosResponse} from 'axios'
import {CvsCode, Status} from '../client.enum'
import CvsTranable from './cvsTranable'
import Client from '../client'
import WithCvsTranable from './cvsTranable'
import {
ICancelCvsResult,
IEntryTranCvsResult,
IExecTranCvsResult
} from './cvsTranable.interface'

const test = anyTest as TestInterface<{cvsTran: CvsTranable}>;
const CvsTranable = WithCvsTranable(Client)
let cvsTran: any

test.beforeEach((t) => {
const cvsTran = new CvsTranable()
test.beforeEach(() => {
cvsTran = new CvsTranable()
cvsTran.client = Axios.create({})
t.context.cvsTran = cvsTran
})

test('.entryTranCvs calls API and returns response', async (t) => {
t.context.cvsTran.options = {
cvsTran.config.axios = {
adapter: async (config: AxiosRequestConfig) => {
const response: AxiosResponse = {
data: 'AccessID=accessid&AccessPass=accesspass',
Expand All @@ -38,7 +39,7 @@ test('.entryTranCvs calls API and returns response', async (t) => {
Amount: 1234,
Tax: 123
}
const res = await t.context.cvsTran.entryTranCvs(args)
const res = await cvsTran.entryTranCvs(args)

const expect: IEntryTranCvsResult = {
AccessID: 'accessid',
Expand All @@ -48,7 +49,7 @@ test('.entryTranCvs calls API and returns response', async (t) => {
})

test('.execTranCvs calls API and returns response', async (t) => {
t.context.cvsTran.options = {
cvsTran.config.axios = {
adapter: async (config: AxiosRequestConfig) => {
const text = [
'OrderID=orderid',
Expand Down Expand Up @@ -86,7 +87,7 @@ test('.execTranCvs calls API and returns response', async (t) => {
ReceiptsDisp12: '09011112222',
ReceiptsDisp13: '10:00-19:00'
}
const res = await t.context.cvsTran.execTranCvs(args)
const res = await cvsTran.execTranCvs(args)

const expect: IExecTranCvsResult = {
OrderID: 'orderid',
Expand All @@ -104,7 +105,7 @@ test('.execTranCvs calls API and returns response', async (t) => {
})

test('.cancelCvs calls API and returns response', async (t) => {
t.context.cvsTran.options = {
cvsTran.config.axios = {
adapter: async (config: AxiosRequestConfig) => {
const text = [
'OrderID=orderid',
Expand All @@ -129,7 +130,7 @@ test('.cancelCvs calls API and returns response', async (t) => {
AccessPass: 'accesspass',
OrderID: 'orderid'
}
const res = await t.context.cvsTran.cancelCvs(args)
const res = await cvsTran.cancelCvs(args)

const expect: ICancelCvsResult = {
OrderID: 'orderid',
Expand Down
18 changes: 6 additions & 12 deletions src/client/cvsTranable.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {AxiosInstance} from 'axios'
import * as encoding from 'encoding-japanese'
import * as merge from 'deepmerge'
import Client from '../client'
import {IConfig} from '../config.interface'
import {Constructor} from '../util'
import {
ICancelCvsArgs,
ICancelCvsResult,
Expand All @@ -12,24 +11,19 @@ import {
IExecTranCvsResult
} from './cvsTranable.interface'

export default class CvsTranable extends Client {
public name: string = 'CvsTranable'
public config: IConfig
public client: AxiosInstance
public options: object = {}

export default <T extends Constructor<Client>>(Base: T) => class extends Base {
public async entryTranCvs(args: IEntryTranCvsArgs): Promise<IEntryTranCvsResult> {
const defaultData = {
ShopID: this.config !== undefined ? this.config.ShopID : undefined,
ShopPass: this.config !== undefined ? this.config.ShopPass : undefined,
ShopID: this.config.ShopID,
ShopPass: this.config.ShopPass,
OrderID: undefined,
Amount: undefined,
Tax: undefined
}
const data: IEntryTranCvsArgs = merge(defaultData, args)
const parsed: any = await this.post('/payment/EntryTranCvs.idPass', data)

return <IEntryTranCvsResult> parsed
return <IEntryTranCvsResult>parsed
}

public async execTranCvs(args: IExecTranCvsArgs): Promise<IExecTranCvsResult> {
Expand All @@ -39,7 +33,7 @@ export default class CvsTranable extends Client {
CustomerKana: encoding.urlEncode(encoding.convert(args.CustomerKana, 'SJIS'))
})

return <IExecTranCvsResult> parsed
return <IExecTranCvsResult>parsed
}

public async cancelCvs(args: ICancelCvsArgs): Promise<ICancelCvsResult> {
Expand Down
Loading

0 comments on commit 277f497

Please sign in to comment.