Skip to content

Commit 8770625

Browse files
axshaniilyamerman
andauthored
feat: Chargebacks (#371)
* implementation of Chargebacks * create test --------- Co-authored-by: Ilya Merman <[email protected]>
1 parent 54a825e commit 8770625

File tree

6 files changed

+246
-0
lines changed

6 files changed

+246
-0
lines changed

resources/chargeback.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { Customer, Account, Transaction } from "../types"
2+
import { Chargeback, ChargebackListParams, CreateChargebackRequest } from "../types/chargeback"
3+
import { Include, UnitConfig, UnitResponse } from "../types/common"
4+
import {BaseResource} from "./baseResource"
5+
6+
type ChargebackIncluded = Customer[] | Account[] | Transaction[]
7+
8+
export class Chargebacks extends BaseResource {
9+
10+
constructor(token: string, basePath: string, config?: UnitConfig) {
11+
super(token, basePath + "/chargebacks", config)
12+
}
13+
14+
public async create(request: CreateChargebackRequest): Promise<UnitResponse<Chargeback>> {
15+
return this.httpPost<UnitResponse<Chargeback>>("", {data: request})
16+
}
17+
18+
/**
19+
* Include is Optional. Related resource available to include: customer, account, transaction. See [Getting Related Resources](https://developers.unit.co/#intro-getting-related-resources)
20+
* @param chargebackId
21+
* @param include
22+
*/
23+
public async get(chargebackId: string, include = ""): Promise<UnitResponse<Chargeback> & Include<ChargebackIncluded>> {
24+
return this.httpGet<UnitResponse<Chargeback> & Include<ChargebackIncluded>>(`/${chargebackId}`, {params: {include}})
25+
}
26+
27+
public async list(params?: ChargebackListParams): Promise<UnitResponse<Chargeback[]> & Include<ChargebackIncluded>> {
28+
const parameters: any = {
29+
"page[limit]": (params?.limit ? params.limit : 100),
30+
"page[offset]": (params?.offset ? params.offset : 0),
31+
...(params?.accountId && {"filter[accountId]": params.accountId}),
32+
...(params?.customerId && {"filter[customerId]": params.customerId}),
33+
...(params?.since && {"filter[since]": params.since}),
34+
...(params?.until && {"filter[until]": params.until}),
35+
...(params?.tags && {"filter[tags]": params.tags}),
36+
...(params?.include && {"include": params.include}),
37+
...(params?.sort && {"sort": params.sort})
38+
}
39+
40+
return this.httpGet<UnitResponse<Chargeback[]> & Include<ChargebackIncluded>>("", {params: parameters})
41+
}
42+
43+
44+
45+
}

resources/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@ export * from "./simulations"
2929
export * from "./statements"
3030
export * from "./transactions"
3131
export * from "./webhooks"
32+
export * from "./chargeback"

tests/chargebacks.spec.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { Chargeback, CreateChargebackRequest, Unit } from "../unit"
2+
3+
import dotenv from "dotenv"
4+
import { createIndividualAccount } from "./testHelpers"
5+
import { createRelationship } from "../helpers"
6+
dotenv.config()
7+
const unit = new Unit(process.env.UNIT_TOKEN || "test", process.env.UNIT_API_URL || "test")
8+
9+
describe("Chargebacks List", () => {
10+
test("Get Chargebacks List", async () => {
11+
const res = await unit.chargebacks.list()
12+
res.data.forEach(element => {
13+
expect(element.type).toBe("chargeback")
14+
})
15+
})
16+
17+
test("Get Chargebacks list with included customer", async () => {
18+
const res = await unit.chargebacks.list()
19+
res.data.forEach(async (element: Chargeback) => {
20+
expect(element.type).toBe("chargeback")
21+
22+
const chargeback: Chargeback = (await unit.chargebacks.get(element.id)).data
23+
expect(chargeback.id).toBe(element.id)
24+
expect(chargeback.type).toBe(element.type)
25+
expect(chargeback.attributes.amount).toBe(element.attributes.amount)
26+
})
27+
})
28+
29+
test("Get Chargebacks list with included customer", async () => {
30+
const res = await unit.chargebacks.list({include: "customer"})
31+
res.data.forEach(async (element: Chargeback) => {
32+
expect(element.type).toBe("chargeback")
33+
})
34+
35+
36+
// expect(res.included).not.toBeUndefined()
37+
})
38+
})
39+
40+
describe("Test Create Chargeback", () => {
41+
test("Create Chargeback", async () => {
42+
const accountId = (await createIndividualAccount(unit)).data.id
43+
const counterpartyId = (await createIndividualAccount(unit)).data.id
44+
45+
const req: CreateChargebackRequest = {
46+
type: "chargeback",
47+
attributes: {
48+
amount: 50,
49+
description: "Chargeback for test"
50+
},
51+
relationships: {
52+
account: createRelationship("account", accountId),
53+
counterpartyAccount: createRelationship("depositAccount", counterpartyId)
54+
}
55+
}
56+
57+
const res = await unit.chargebacks.create(req)
58+
expect(res.data.type).toBe("chargeback")
59+
})
60+
})

types/chargeback.ts

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
import { BaseCreateRequestAttributes, BaseListParams, Relationship, Sort, Tags } from "./common"
2+
3+
export interface Chargeback {
4+
/**
5+
* Identifier of the chargeback resource.
6+
*/
7+
id: string
8+
9+
/**
10+
* Type of the chargeback resource. The value is always chargeback.
11+
*/
12+
type: "chargeback"
13+
14+
/**
15+
* JSON object representing the chargeback data.
16+
*/
17+
attributes: {
18+
/**
19+
* The date the chargeback was created.
20+
* RFC3339 format. For more information: https://en.wikipedia.org/wiki/ISO_8601#RFCs
21+
*/
22+
createdAt: string
23+
24+
/**
25+
* The amount (in cents) of the chargeback.
26+
*/
27+
amount: number
28+
29+
/**
30+
* Description of the chargeback.
31+
*/
32+
description: string
33+
34+
/**
35+
* See [Tags](https://developers.unit.co/#tags).
36+
*/
37+
tags?: object
38+
}
39+
40+
/**
41+
* Describes relationships between the chargeback resource and other resources (accounts, transaction, customer).
42+
*/
43+
relationships: {
44+
/**
45+
* The [Deposit Account](https://docs.unit.co/deposit-accounts/) the funds will be debited from.
46+
*/
47+
account: Relationship
48+
49+
/**
50+
* The [Customer](https://developers.unit.co/customers/) the deposit account belongs to.
51+
*/
52+
customer: Relationship
53+
54+
/**
55+
* The account that will receive the funds.
56+
*/
57+
counterpartyAccount: Relationship
58+
59+
/**
60+
* The Chargeback [Transaction](https://docs.unit.co/resources/#transaction-chargeback) generated by this chargeback.
61+
*/
62+
transaction: Relationship
63+
}
64+
}
65+
66+
export interface ChargebackListParams extends BaseListParams {
67+
/**
68+
* Optional. Filters the results by the specified account id.
69+
* default: empty
70+
*/
71+
accountId?: string
72+
73+
/**
74+
* Optional. Filters the results by the specified customer id.
75+
* default: empty
76+
*/
77+
customerId?: string
78+
79+
/**
80+
* Optional. Filter Chargebacks by [Tags](https://docs.unit.co/#tags).
81+
* default: empty
82+
*/
83+
tags?: Tags
84+
85+
/**
86+
* Optional. Filters the Chargebacks that occurred after the specified date. e.g. 2020-01-13T16:01:19.346Z
87+
*/
88+
since?: string
89+
90+
/**
91+
* Optional. Filters the Chargebacks that occurred before the specified date. e.g. 2020-01-02T20:06:23.486Z
92+
*/
93+
until?: string
94+
95+
/**
96+
* Optional. Leave empty or provide sort=createdAt for ascending order. Provide sort=-createdAt (leading minus sign) for descending order.
97+
* default: sort=-createdAt
98+
*/
99+
sort?: Sort
100+
101+
/**
102+
* Optional. A comma-separated list of related resources to include in the response. Related resources include: customer, account, transaction. See [Getting Related Resources](https://docs.unit.co/about-jsonapi/#intro-getting-related-resources).
103+
*/
104+
include?: string
105+
}
106+
107+
export interface CreateChargebackRequest {
108+
/**
109+
* Type of the resource to be created, the value is always chargeback.
110+
*/
111+
type: "chargeback"
112+
113+
attributes: {
114+
/**
115+
* The amount (in cents) to charge the account and credit the counterparty.
116+
*/
117+
amount: number
118+
119+
/**
120+
* Description of the chargeback (maximum of 50 characters).
121+
*/
122+
description: string
123+
} & BaseCreateRequestAttributes
124+
125+
relationships: {
126+
/**
127+
* The Deposit Account the funds will be debited from.
128+
*/
129+
account: Relationship
130+
131+
/**
132+
* The account that will receive the funds.
133+
*/
134+
counterpartyAccount: Relationship
135+
}
136+
}

types/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ export * from "./rewards"
2727
export * from "./simulations"
2828
export * from "./transactions"
2929
export * from "./webhooks"
30+
export * from "./chargeback"

unit.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import { Simulations } from "./resources/simulations"
2929
import { Disputes } from "./resources/dispute"
3030
import { Repayments } from "./resources/repayments"
3131
import { StopPayments } from "./resources/stopPayments"
32+
import { Chargebacks } from "./resources/chargeback"
3233

3334
export class Unit {
3435
public applications: Applications
@@ -63,6 +64,7 @@ export class Unit {
6364
public repayments: Repayments
6465
public stopPayments: StopPayments
6566
public checkPayments: CheckPayments
67+
public chargebacks: Chargebacks
6668

6769
constructor(token: string, basePath: string, config?: UnitConfig) {
6870
// remove all trailing slashes from user-provided basePath
@@ -99,6 +101,7 @@ export class Unit {
99101
this.repayments = new Repayments(token, basePath, config)
100102
this.stopPayments = new StopPayments(token, basePath, config)
101103
this.checkPayments = new CheckPayments(token, basePath, config)
104+
this.chargebacks = new Chargebacks(token, basePath, config)
102105
this.helpers = helpers
103106
}
104107

0 commit comments

Comments
 (0)