Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add vbase client to persist responses #30

Merged
merged 1 commit into from
Nov 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
"docs": "0.x"
},
"policies": [
{
"name": "vbase-read-write"
},
{
"name": "colossus-fire-event"
},
Expand Down
36 changes: 35 additions & 1 deletion node/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,55 @@ import {
SettlementResponse,
Settlements,
} from '@vtex/payment-provider'
import { VBase } from '@vtex/api'

import { randomString } from './utils'
import { executeAuthorization } from './flow'

const authorizationsBucket = 'authorizations'
const persistAuthorizationResponse = async (
vbase: VBase,
resp: AuthorizationResponse
) => vbase.saveJSON(authorizationsBucket, resp.paymentId, resp)

const getPersistedAuthorizationResponse = async (
vbase: VBase,
req: AuthorizationRequest
) =>
vbase.getJSON<AuthorizationResponse | undefined>(
authorizationsBucket,
req.paymentId,
true
)

export default class TestSuiteApprover extends PaymentProvider {
// This class needs modifications to pass the test suit.
// Refer to https://help.vtex.com/en/tutorial/payment-provider-protocol#4-testing
// in order to learn about the protocol and make the according changes.

private async saveAndRetry(
req: AuthorizationRequest,
resp: AuthorizationResponse
) {
await persistAuthorizationResponse(this.context.clients.vbase, resp)
this.callback(req, resp)
}

public async authorize(
authorization: AuthorizationRequest
): Promise<AuthorizationResponse> {
if (this.isTestSuite) {
const persistedResponse = await getPersistedAuthorizationResponse(
this.context.clients.vbase,
authorization
)

if (persistedResponse !== undefined && persistedResponse !== null) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can do something like if (!!persistedResponse) instead of this bigger expression or use !isNil(persistedResponse) in case you already using ramda is this project.

return persistedResponse
}

return executeAuthorization(authorization, response =>
this.callback(authorization, response)
this.saveAndRetry(authorization, response)
)
}

Expand Down
24 changes: 12 additions & 12 deletions node/flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const flows: Record<
Flow,
(
authorization: AuthorizationRequest,
callback: (response: AuthorizationResponse) => void
retry: (response: AuthorizationResponse) => void
) => AuthorizationResponse
> = {
Authorize: request =>
Expand All @@ -34,10 +34,10 @@ export const flows: Record<

Denied: request => Authorizations.deny(request, { tid: randomString() }),

Cancel: (request, callback) => flows.Authorize(request, callback),
Cancel: (request, retry) => flows.Authorize(request, retry),

AsyncApproved: (request, callback) => {
callback(
AsyncApproved: (request, retry) => {
retry(
Authorizations.approve(request, {
authorizationId: randomString(),
nsu: randomString(),
Expand All @@ -51,17 +51,17 @@ export const flows: Record<
})
},

AsyncDenied: (request, callback) => {
callback(Authorizations.deny(request, { tid: randomString() }))
AsyncDenied: (request, retry) => {
retry(Authorizations.deny(request, { tid: randomString() }))

return Authorizations.pending(request, {
delayToCancel: 1000,
tid: randomString(),
})
},

BankInvoice: (request, callback) => {
callback(
BankInvoice: (request, retry) => {
retry(
Authorizations.approve(request, {
authorizationId: randomString(),
nsu: randomString(),
Expand All @@ -76,8 +76,8 @@ export const flows: Record<
})
},

Redirect: (request, callback) => {
callback(
Redirect: (request, retry) => {
retry(
Authorizations.approve(request, {
authorizationId: randomString(),
nsu: randomString(),
Expand Down Expand Up @@ -123,9 +123,9 @@ const findFlow = (request: AuthorizationRequest): Flow => {

export const executeAuthorization = (
request: AuthorizationRequest,
callback: (response: AuthorizationResponse) => void
retry: (response: AuthorizationResponse) => void
): AuthorizationResponse => {
const flow = findFlow(request)

return flows[flow](request, callback)
return flows[flow](request, retry)
}
6 changes: 3 additions & 3 deletions node/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"dependencies": {
"@vtex/payment-provider": "^1.0.0",
"@vtex/payment-provider": "^1.4.0",
"axios": "^0.21.1",
"co-body": "^6.0.0",
"ramda": "^0.25.0",
Expand All @@ -11,10 +11,10 @@
"@types/jest": "^24.0.18",
"@types/node": "^12.0.0",
"@types/ramda": "types/npm-ramda#dist",
"@vtex/api": "6.39.1",
"@vtex/api": "6.45.6",
"@vtex/test-tools": "^1.0.0",
"typescript": "3.9.7",
"vtex.payment-provider-example": "https://amb--coinshop.myvtex.com/_v/private/typings/linked/v1/vtex.payment-provider-example@0.3.0+build1614375977/public/_types/react"
"vtex.payment-provider-example": "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.payment-provider-example@1.0.2/public/_types/react"
},
"scripts": {
"lint": "tsc --noEmit --pretty"
Expand Down
70 changes: 37 additions & 33 deletions node/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1458,10 +1458,10 @@
resolved "https://registry.yarnpkg.com/@types/zen-observable/-/zen-observable-0.8.2.tgz#808c9fa7e4517274ed555fa158f2de4b4f468e71"
integrity sha512-HrCIVMLjE1MOozVoD86622S7aunluLb2PJdPfb3nYiEtohm8mIB/vyv0Fd37AdeMFrTUQXEunw78YloMA3Qilg==

"@vtex/api@6.39.1", "@vtex/[email protected]":
version "6.39.1"
resolved "https://registry.yarnpkg.com/@vtex/api/-/api-6.39.1.tgz#ad675cf46404b0d21476ac441626843b6950c4a2"
integrity sha512-w3FghXguk4b+bOSOihB8I4+gGt7JljRCXFgirK9XiHDOr+uPsUEGhC4JeeQ42aBIEQeyGmQQOsyvZ+qZp2C/oA==
"@vtex/api@6.45.6":
version "6.45.6"
resolved "https://registry.yarnpkg.com/@vtex/api/-/api-6.45.6.tgz#e5f08476d7c76f26baa1c040666d1364bdd6bc35"
integrity sha512-9pDiUxcUzq8RZa9yBex4C8dcAHXBRGAZokvHJ6sykrojq6mJxs+rUTE28TPeeQxwvvKsvrdpsfCUAYQ+UDz+zA==
dependencies:
"@types/koa" "^2.11.0"
"@types/koa-compose" "^3.2.3"
Expand All @@ -1470,7 +1470,7 @@
agentkeepalive "^4.0.2"
apollo-server-errors "^2.2.1"
archiver "^3.0.0"
axios "^0.19.2"
axios "^0.21.1"
axios-retry "^3.1.2"
bluebird "^3.5.4"
chalk "^2.4.2"
Expand Down Expand Up @@ -1500,6 +1500,7 @@
semver "^5.5.1"
stats-lite vtex/node-stats-lite#dist
tar-fs "^2.0.0"
tokenbucket "^0.3.2"
uuid "^3.3.3"
xss "^1.0.6"

Expand All @@ -1510,12 +1511,11 @@
dependencies:
is-stream "^2.0.0"

"@vtex/payment-provider@^1.0.0":
version "1.0.0"
resolved "https://registry.yarnpkg.com/@vtex/payment-provider/-/payment-provider-1.0.0.tgz#918440e9e1e85b40189dcc100728a0552a040cc9"
integrity sha512-cZJQTbu+Efw449ypuM6RgJpjfnEg5KkQcOQOCHV+ugktOLO3DS/Jn/dUfMxhKOCenOkNTq1JE8LfKfvr5NMUNg==
"@vtex/payment-provider@^1.4.0":
version "1.4.0"
resolved "https://registry.yarnpkg.com/@vtex/payment-provider/-/payment-provider-1.4.0.tgz#b6c856db778af870576aeca27d4f23f6c927444e"
integrity sha512-W+ta+C+jLkRnKTaI6521lh6PKswJmKxSdHCGZOHtmZ74y7Bxmlxzj4Tao8qgTv0PPft3CBtRlShGu+xPiDLgMA==
dependencies:
"@vtex/api" "6.x"
bluebird "^3.7.2"
co-body "^6.0.0"
ramda "^0.27.0"
Expand Down Expand Up @@ -1862,13 +1862,6 @@ axios-retry@^3.1.2:
dependencies:
is-retry-allowed "^1.1.0"

axios@^0.19.2:
version "0.19.2"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.19.2.tgz#3ea36c5d8818d0d5f8a8a97a6d36b86cdc00cb27"
integrity sha512-fjgm5MvRHLhx+osE2xoekY70AhARk3a6hkN+3Io1jc00jtquGvxYlKlsFUhmUET0V5te6CcZI7lcv2Ym61mjHA==
dependencies:
follow-redirects "1.5.10"

axios@^0.21.1:
version "0.21.1"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.21.1.tgz#22563481962f4d6bde9a76d516ef0e5d3c09b2b8"
Expand Down Expand Up @@ -1996,6 +1989,11 @@ bl@^4.0.3:
inherits "^2.0.4"
readable-stream "^3.4.0"

[email protected]:
version "2.9.24"
resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-2.9.24.tgz#14a2e75f0548323dc35aa440d92007ca154e967c"
integrity sha1-FKLnXwVIMj3DWqRA2SAHyhVOlnw=

bluebird@^3.5.4, bluebird@^3.7.2:
version "3.7.2"
resolved "https://registry.yarnpkg.com/bluebird/-/bluebird-3.7.2.tgz#9f229c15be272454ffa973ace0dbee79a1b0c36f"
Expand Down Expand Up @@ -2437,13 +2435,6 @@ dataloader@^1.4.0:
resolved "https://registry.yarnpkg.com/dataloader/-/dataloader-1.4.0.tgz#bca11d867f5d3f1b9ed9f737bd15970c65dff5c8"
integrity sha512-68s5jYdlvasItOJnCuI2Q9s4q98g0pCyL3HrcKJu8KNugUl8ahgmZYg38ysLTgQjjXX3H8CJLkAvWrclWfcalw==

debug@=3.1.0, debug@~3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/debug/-/debug-3.1.0.tgz#5bb5a0672628b64149566ba16819e61518c67261"
integrity sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==
dependencies:
ms "2.0.0"

debug@^2.2.0, debug@^2.3.3:
version "2.6.9"
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f"
Expand All @@ -2465,6 +2456,13 @@ debug@^4.1.0, debug@^4.1.1:
dependencies:
ms "2.1.2"

debug@~3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/debug/-/debug-3.1.0.tgz#5bb5a0672628b64149566ba16819e61518c67261"
integrity sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==
dependencies:
ms "2.0.0"

decamelize@^1.2.0:
version "1.2.0"
resolved "https://registry.yarnpkg.com/decamelize/-/decamelize-1.2.0.tgz#f6534d15148269b20352e7bee26f501f9a191290"
Expand Down Expand Up @@ -2847,13 +2845,6 @@ find-up@^3.0.0:
dependencies:
locate-path "^3.0.0"

[email protected]:
version "1.5.10"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.5.10.tgz#7b7a9f9aea2fdff36786a94ff643ed07f4ff5e2a"
integrity sha512-0V5l4Cizzvqt5D44aTXbFZz+FtyXV1vrDN6qrelxtfYQKW0KO0W2T/hkE8xvGa/540LkZlkaUjO4ailYTFtHVQ==
dependencies:
debug "=3.1.0"

follow-redirects@^1.10.0:
version "1.13.2"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.13.2.tgz#dd73c8effc12728ba5cf4259d760ea5fb83e3147"
Expand Down Expand Up @@ -4941,6 +4932,11 @@ redent@^3.0.0:
indent-string "^4.0.0"
strip-indent "^3.0.0"

redis@^0.12.1:
version "0.12.1"
resolved "https://registry.yarnpkg.com/redis/-/redis-0.12.1.tgz#64df76ad0fc8acebaebd2a0645e8a48fac49185e"
integrity sha1-ZN92rQ/IrOuuvSoGReikj6xJGF4=

regenerate-unicode-properties@^8.2.0:
version "8.2.0"
resolved "https://registry.yarnpkg.com/regenerate-unicode-properties/-/regenerate-unicode-properties-8.2.0.tgz#e5de7111d655e7ba60c057dbe9ff37c87e65cdec"
Expand Down Expand Up @@ -5592,6 +5588,14 @@ [email protected]:
resolved "https://registry.yarnpkg.com/toidentifier/-/toidentifier-1.0.0.tgz#7e1be3470f1e77948bc43d94a3c8f4d7752ba553"
integrity sha512-yaOH/Pk/VEhBWWTlhI+qXxDFXlejDGcQipMlyxda9nthulaxLZUNcUqFxokp0vcYnvteJln5FNQDRrxj3YcbVw==

tokenbucket@^0.3.2:
version "0.3.2"
resolved "https://registry.yarnpkg.com/tokenbucket/-/tokenbucket-0.3.2.tgz#8172b2b58e3083acc8d914426fed15162a3a8e90"
integrity sha1-gXKytY4wg6zI2RRCb+0VFio6jpA=
dependencies:
bluebird "2.9.24"
redis "^0.12.1"

tough-cookie@^2.3.3, tough-cookie@^2.3.4, tough-cookie@~2.5.0:
version "2.5.0"
resolved "https://registry.yarnpkg.com/tough-cookie/-/tough-cookie-2.5.0.tgz#cd9fb2a0aa1d5a12b473bd9fb96fa3dcff65ade2"
Expand Down Expand Up @@ -5787,9 +5791,9 @@ [email protected]:
core-util-is "1.0.2"
extsprintf "^1.2.0"

"vtex.payment-provider-example@https://amb--coinshop.myvtex.com/_v/private/typings/linked/v1/vtex.payment-provider-example@0.3.0+build1614375977/public/_types/react":
"vtex.payment-provider-example@http://vtex.vtexassets.com/_v/public/typings/v1/vtex.payment-provider-example@1.0.2/public/_types/react":
version "0.0.0"
resolved "https://amb--coinshop.myvtex.com/_v/private/typings/linked/v1/vtex.payment-provider-example@0.3.0+build1614375977/public/_types/react#fa7a0347e046eab3dd768998fc9252b2c0dd5aef"
resolved "http://vtex.vtexassets.com/_v/public/typings/v1/vtex.payment-provider-example@1.0.2/public/_types/react#fa7a0347e046eab3dd768998fc9252b2c0dd5aef"

w3c-hr-time@^1.0.1:
version "1.0.2"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@
"prettier": "^1.19.1",
"typescript": "^3.7.5"
}
}
}