Skip to content

Commit

Permalink
[wip] add digital issue gc act
Browse files Browse the repository at this point in the history
  • Loading branch information
lmscunha committed Nov 27, 2024
1 parent 438e76b commit 8210992
Show file tree
Hide file tree
Showing 9 changed files with 257 additions and 53 deletions.
6 changes: 6 additions & 0 deletions dist/TilloProvider-doc.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
declare const docs: {
get_info: {
desc: string;
};
};
export default docs;
13 changes: 13 additions & 0 deletions dist/TilloProvider-doc.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/TilloProvider-doc.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions dist/tillo-provider.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
type TilloProviderOptions = {
url: string;
fetch: any;
entity: Record<string, any>;
debug: boolean;
};
declare function TilloProvider(this: any, options: TilloProviderOptions): {
exports: {};
};
export default TilloProvider;
152 changes: 152 additions & 0 deletions dist/tillo-provider.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions dist/tillo-provider.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"peerDependencies": {
"@seneca/env": ">=0.2",
"@seneca/provider": ">=1.0",
"seneca": ">=3",
"seneca": ">=3||>=4.0.0-rc2",
"seneca-entity": ">=19",
"seneca-promisify": ">=3"
},
Expand Down
111 changes: 59 additions & 52 deletions src/tillo-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,24 @@ type TilloProviderOptions = {
debug: boolean
}

type TilloSignatureOptions = {
apiKey: string
apiSecret: string
method: string
path?: string
timestamp: string
function generateAuthSign(signData: Map<string, string>) {
const sd = new Map(signData)
sd.delete("apiSecret")
const sdList: any = []


sd.forEach((v: any) => {
sdList.push(v)
})

return sdList.join('-')
}

function getAuthSignature(signData: Map<string, string>) {
const authSign = generateAuthSign(signData)

function getAuthSignature(signData: TilloSignatureOptions) {
const authSign = `${signData.apiKey}-${signData.method}-${signData.path}-${signData.timestamp}`
const hashedSign = crypto.createHmac('sha256', signData.apiSecret).update(authSign).digest('hex')
const hashedSign = crypto.createHmac('sha256', signData.get("apiSecret"))
.update(authSign).digest('hex')
return hashedSign
}

Expand Down Expand Up @@ -69,76 +75,77 @@ function TilloProvider(this: any, options: TilloProviderOptions) {
name: 'tillo'
},
entity: {
customer: {
cmd: {
list: {
action: async function(this: any, entize: any, msg: any) {
let json: any =
await getJSON(makeUrl('customers', msg.q), makeConfig())
let customers = json
let list = customers.map((data: any) => entize(data))
return list
},
}
}
},
brand: {
cmd: {
list: {
action: async function(this: any, entize: any, msg: any) {
const path = "brands"
const timestamp = new Date().getTime().toString()
const options: TilloSignatureOptions = {
apiKey: this.shared.headers["API-Key"],
method: "GET",
path,
timestamp,
apiSecret: this.shared.secret
}

const options: Map<string, string> = new Map([
["apikey", this.shared.headers["API-Key"]],
["method", "GET"],
["path", path],
["timestamp", timestamp],
["apiSecret", this.shared.secret],
])

this.shared.headers.Signature = getAuthSignature(options)
this.shared.headers.Timestamp = timestamp
this.shared.headers.Accept = "application/json"

let json: any =
await getJSON(makeUrl(path, msg.q), makeConfig())
await getJSON(makeUrl(path + "?detail=true", msg.q), makeConfig())
let brands = json.data.brands
let list = Object.entries(brands).map(([name, value]: any) => entize({ name, value }))
return list
},
}
}
},
order: {
digitalGC: {
cmd: {
list: {
action: async function(this: any, entize: any, msg: any) {
let json: any =
await getJSON(makeUrl('orders', msg.q), makeConfig())
let orders = json.orders
let list = orders.map((data: any) => entize(data))

// TODO: ensure seneca-transport preserves array props
list.page = json.page

return list
},
},
save: {
action: async function(this: any, entize: any, msg: any) {
let body = this.util.deep(
this.shared.primary,
options.entity.order.save,
msg.ent.data$(false)
)
const timestamp = new Date().getTime().toString()
const clientRequestId = `${msg.q.user_id}-digitalissue-${timestamp}`
const brand = msg.q.brand
const currency = msg.q?.currency || "GBP"
const value = msg.q.value
const sector = msg.q?.sector || "other"

const options: Map<string, string> = new Map([
["apikey", this.shared.headers["API-Key"]],
["method", "POST"],
["path", "digital-issue"],
["clientRequestId", clientRequestId],
["brand", brand],
["currency", currency],
["value", value],
["timestamp", timestamp],
["apiSecret", this.shared.secret],
])

this.shared.headers.Signature = getAuthSignature(options)
this.shared.headers.Timestamp = timestamp
this.shared.headers.Accept = "application/json"

let json: any =
await postJSON(makeUrl('orders', msg.q), makeConfig({
body
await postJSON(makeUrl('digital/issue'), makeConfig({
body: {
client_request_id: clientRequestId,
brand: brand,
face_value: {
amount: value,
currency,
},
delivery_method: 'url',
fulfilment_by: 'partner',
sector: sector
}
}))

let order = json
order.id = order.referenceOrderID
return entize(order)
},
}
Expand Down
14 changes: 14 additions & 0 deletions test/tillo-provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,20 @@ describe('tillo-provider', () => {
// expect(list.length > 0).toBeTruthy()
// })

// test('issue-gc', async () => {
// if (!Config) return;
// const seneca = await makeSeneca()
//
// const redeemTemplate = await seneca.entity("provider/tillo/digitalGC").save$({
// user_id: "user01",
// brand: "hobbycraft",
// value: 10.00,
// })
// console.log('REDEEM TEMPLATE ', redeemTemplate)
//
// expect(redeemTemplate).toBeTruthy()
// })

})


Expand Down

0 comments on commit 8210992

Please sign in to comment.