Skip to content

Commit

Permalink
Merge pull request #5 from p-fedyukovich/introduce-card-model
Browse files Browse the repository at this point in the history
Introduce card model
  • Loading branch information
p-fedyukovich authored Nov 16, 2020
2 parents cad6299 + fad4b54 commit cab5159
Show file tree
Hide file tree
Showing 11 changed files with 120 additions and 125 deletions.
2 changes: 1 addition & 1 deletion lib/api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default class SwedbankPayAPI {
}
})

axiosRetry(this._client, { retryDelay: axiosRetry.exponentialDelay , retries: 5})
axiosRetry(this._client, { retryDelay: axiosRetry.exponentialDelay, retries: 5 })
}

private async request<T>(method: Method, url?: string, data?: any): Promise<T> {
Expand Down
3 changes: 2 additions & 1 deletion lib/api/response/raw-authorizations-response.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import RawCommonPaymentResponse from './raw-common-payment-response'
import { RawTransaction } from './raw-transactions-response'
import RawCardBrand from './raw-card-brand'

export interface RawAuthorization {
id: string
cardBrand: string
cardBrand: RawCardBrand
cardType: string
maskedPan: string
expiryDate: string
Expand Down
12 changes: 12 additions & 0 deletions lib/api/response/raw-card-brand.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
type RawCardBrand =
| 'Visa'
| 'MasterCard'
| 'Amex'
| 'Dankort'
| 'Diners'
| 'Finax'
| 'Jcb'
| 'IkanoFinansDK'
| 'Maestro'

export default RawCardBrand
13 changes: 3 additions & 10 deletions lib/api/response/raw-instrument-response.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
import RawCardBrand from './raw-card-brand'

export default interface RawInstrumentResponse {
instrumentData: {
cardBrand:
| 'Visa'
| 'MasterCard'
| 'Amex'
| 'Dankort'
| 'Diners'
| 'Finax'
| 'Jcb'
| 'IkanoFinansDK'
| 'Maestro'
cardBrand: RawCardBrand
maskedPan: string
expiryDate: string
paymentToken: string
Expand Down
13 changes: 3 additions & 10 deletions lib/api/response/raw-paid-payment-response.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import RawCardBrand from './raw-card-brand'

export default interface RawPaidPaymentResponse {
payment: string
paid: {
Expand All @@ -8,16 +10,7 @@ export default interface RawPaidPaymentResponse {
expiryDate: string
}>
detail: {
cardBrand:
| 'Visa'
| 'MasterCard'
| 'Amex'
| 'Dankort'
| 'Diners'
| 'Finax'
| 'Jcb'
| 'IkanoFinansDK'
| 'Maestro'
cardBrand: RawCardBrand
maskedPan: string
cardType: string
issuingBank: string
Expand Down
2 changes: 1 addition & 1 deletion lib/api/response/raw-payee-info-response.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import RawCommonPaymentResponse from './raw-common-payment-response'

export default interface RawPayeeInfoResponse extends RawCommonPaymentResponse{
export default interface RawPayeeInfoResponse extends RawCommonPaymentResponse {
payeeInfo: {
id: string
/**
Expand Down
2 changes: 1 addition & 1 deletion lib/api/response/raw-urls-response.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import RawCommonPaymentResponse from './raw-common-payment-response'

export default interface RawUrlsResponse extends RawCommonPaymentResponse{
export default interface RawUrlsResponse extends RawCommonPaymentResponse {
urls: {
id: string
/**
Expand Down
26 changes: 8 additions & 18 deletions lib/models/authorization.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { RawAuthorization } from '../api/response/raw-authorizations-response'
import Transaction from './transaction'
import Card from './card'

export default class Authorization {
private readonly _id: string
private readonly _cardBrand: string
private readonly _cardType: string
private readonly _maskedPan: string
private readonly _expiryDate: string
private readonly _panToken: string
private readonly _panEnrolled: boolean
private readonly _card: Card
private readonly _acquirerTransactionType: string
private readonly _recurrenceToken: string | null = null
private readonly _paymentToken: string | null = null
Expand All @@ -22,13 +21,12 @@ export default class Authorization {

private constructor(raw: RawAuthorization) {
this._id = raw.id
this._cardBrand = raw.cardBrand
this._cardType = raw.cardType
this._maskedPan = raw.maskedPan
this._expiryDate = raw.expiryDate
this._panToken = raw.panToken
this._panEnrolled = raw.panEnrolled
this._acquirerTransactionType = raw.acquirerTransactionType
this._card = Card.generate(raw.cardBrand, raw.maskedPan, raw.expiryDate)

if (raw.recurrenceToken) {
this._recurrenceToken = raw.recurrenceToken
}
Expand Down Expand Up @@ -60,22 +58,10 @@ export default class Authorization {
return this._id
}

get cardBrand(): string {
return this._cardBrand
}

get cardType(): string {
return this._cardType
}

get maskedPan(): string {
return this._maskedPan
}

get expiryDate(): string {
return this._expiryDate
}

get panToken(): string {
return this._panToken
}
Expand All @@ -84,6 +70,10 @@ export default class Authorization {
return this._panEnrolled
}

get card(): Card {
return this._card
}

get acquirerTransactionType(): string {
return this._acquirerTransactionType
}
Expand Down
77 changes: 77 additions & 0 deletions lib/models/card.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import RawCardBrand from '../api/response/raw-card-brand'

type CardBrand =
| 'visa'
| 'mastercard'
| 'amex'
| 'dankort'
| 'diners'
| 'finax'
| 'jcb'
| 'ikanofinansdk'
| 'maestro'

export default class Card {
private readonly _rawCardBrand: RawCardBrand
private readonly _maskedPan: string
private readonly _expiryDate: string

private constructor(rawCardBrand: RawCardBrand, maskedPan: string, expiryDate: string) {
this._rawCardBrand = rawCardBrand
this._maskedPan = maskedPan
this._expiryDate = expiryDate
}

get rawCardBrand(): RawCardBrand {
return this._rawCardBrand
}

get brand(): CardBrand {
switch (this._rawCardBrand) {
case 'Visa':
return 'visa'
case 'MasterCard':
return 'mastercard'
case 'Amex':
return 'amex'
case 'Dankort':
return 'dankort'
case 'Diners':
return 'diners'
case 'Finax':
return 'finax'
case 'Jcb':
return 'jcb'
case 'IkanoFinansDK':
return 'ikanofinansdk'
case 'Maestro':
return 'maestro'
default:
throw new Error(`Card brand is not recognized: ${this._rawCardBrand}`)
}
}

get expiryDate(): string {
return this._expiryDate
}

get expMonth(): number {
return Number(this._expiryDate.split('/')[0])
}

get expYear(): number {
return Number(this._expiryDate.split('/')[1])
}

get maskedPan(): string {
return this._maskedPan
}

get last4(): string {
return this._maskedPan.slice(this.maskedPan.length - 4)
}

static generate(brand: RawCardBrand, maskedPan: string, expiryDate: string): Card {
return new Card(brand, maskedPan, expiryDate)
}
}
93 changes: 11 additions & 82 deletions lib/models/instrument.ts
Original file line number Diff line number Diff line change
@@ -1,38 +1,30 @@
import RawInstrumentResponse from '../api/response/raw-instrument-response'
import Card from './card'

export default class Instrument {
private readonly _raw: RawInstrumentResponse
private readonly _id: string
private readonly _displayBrand:
| 'Visa'
| 'MasterCard'
| 'Amex'
| 'Dankort'
| 'Diners'
| 'Finax'
| 'Jcb'
| 'IkanoFinansDK'
| 'Maestro'
private readonly _maskedPan: string
private readonly _expiryDate: string
private readonly _paymentToken: string
private readonly _payeeId: string
private readonly _isPayeeToken: boolean
private readonly _isDeleted: boolean
private readonly _tokenType: 'RecurrenceToken' | 'PaymentToken'
private readonly _card: Card

private constructor(raw: RawInstrumentResponse) {
this._raw = raw
const { instrumentData } = raw
this._id = instrumentData.id
this._displayBrand = instrumentData.cardBrand
this._maskedPan = instrumentData.maskedPan
this._expiryDate = instrumentData.expiryDate
this._paymentToken = instrumentData.paymentToken
this._payeeId = instrumentData.payeeId
this._isPayeeToken = instrumentData.isPayeeToken
this._isDeleted = instrumentData.isDeleted
this._tokenType = instrumentData.tokenType
this._card = Card.generate(
instrumentData.cardBrand,
instrumentData.maskedPan,
instrumentData.expiryDate
)
}

static generate(raw: RawInstrumentResponse): Instrument {
Expand All @@ -47,73 +39,6 @@ export default class Instrument {
return this._id
}

get displayBrand():
| 'Visa'
| 'MasterCard'
| 'Amex'
| 'Dankort'
| 'Diners'
| 'Finax'
| 'Jcb'
| 'IkanoFinansDK'
| 'Maestro' {
return this._displayBrand
}

get brand():
| 'visa'
| 'mastercard'
| 'amex'
| 'dankort'
| 'diners'
| 'finax'
| 'jcb'
| 'ikanofinansdk'
| 'maestro' {
switch (this._displayBrand) {
case 'Visa':
return 'visa'
case 'MasterCard':
return 'mastercard'
case 'Amex':
return 'amex'
case 'Dankort':
return 'dankort'
case 'Diners':
return 'diners'
case 'Finax':
return 'finax'
case 'Jcb':
return 'jcb'
case 'IkanoFinansDK':
return 'ikanofinansdk'
case 'Maestro':
return 'maestro'
default:
throw new Error(`Card brand is not recognized: ${this._displayBrand}`)
}
}

get maskedPan(): string {
return this._maskedPan
}

get last4(): string {
return this._maskedPan.slice(this.maskedPan.length - 4)
}

get expiryDate(): string {
return this._expiryDate
}

get expMonth(): number {
return Number(this._expiryDate.split('/')[0])
}

get expYear(): number {
return Number(this._expiryDate.split('/')[1])
}

get paymentToken(): string {
return this._paymentToken
}
Expand All @@ -133,4 +58,8 @@ export default class Instrument {
get tokenType(): 'RecurrenceToken' | 'PaymentToken' {
return this._tokenType
}

get card(): Card {
return this._card
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "swedbank-pay-js",
"version": "1.0.0-RC1",
"version": "1.0.0-RC2",
"description": "Swedbank Pay (PayEx) API wrapper",
"keywords": [
"payex",
Expand Down

0 comments on commit cab5159

Please sign in to comment.