-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update API version, add payment models & tests
- Bump version to 3.1.0 - Add new online payment models and types - Introduce `Payment` class for handling transactions - Replace `ts-node` with `vitest` for testing - Remove unused hashing functions (sha1, md5) - Refactor query parameter handling in services - Add paginated response type for direct billing service - Implement unit tests using `vitest` - Delete old test scripts
- Loading branch information
Showing
18 changed files
with
1,743 additions
and
128 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/models/directbilling/service/service-paginated.response.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import type { DBServiceStatus } from './db.service.status.js'; | ||
|
||
interface DirectBillingServicePaginatedResponse { | ||
success: boolean; | ||
data: Datum[]; | ||
pagination: Pagination; | ||
} | ||
|
||
interface Datum { | ||
id: string; | ||
name: string; | ||
suffix: string; | ||
status: DBServiceStatus; | ||
created_at: Date; | ||
} | ||
|
||
interface Pagination { | ||
total: number; | ||
count: number; | ||
per_page: number; | ||
current_page: number; | ||
total_pages: number; | ||
links: Links; | ||
} | ||
|
||
interface Links { | ||
next_page: null; | ||
prev_page: null; | ||
} | ||
|
||
export type { DirectBillingServicePaginatedResponse }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
enum TransactionStatus { | ||
NEW = 'transaction_new', | ||
CONFIRMED = 'transaction_confirmed', | ||
GENERATED = 'transaction_generated', | ||
PAID = 'transaction_paid', | ||
FAILED = 'transaction_failed', | ||
EXPIRED = 'transaction_expired', | ||
CANCELED = 'transaction_canceled', | ||
REFUNDED = 'transaction_refunded', | ||
} | ||
|
||
export type { TransactionStatus }; |
51 changes: 51 additions & 0 deletions
51
src/models/payment/transaction/transaction-details-response.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
export interface TransactionDetailsResponse { | ||
success: boolean; | ||
data: Data; | ||
} | ||
|
||
export interface Data { | ||
id: string; | ||
status: string; | ||
amount: Amount; | ||
channel: string; | ||
control: string; | ||
description: string; | ||
redirects: Redirects; | ||
customer: Customer; | ||
billing: Ing; | ||
shipping: Ing; | ||
cart: null; | ||
paid_at: Date; | ||
expires_at: string; | ||
created_at: string; | ||
updated_at: Date; | ||
} | ||
|
||
export interface Amount { | ||
value: number; | ||
currency: string; | ||
commission: number; | ||
} | ||
|
||
export interface Ing { | ||
name: string; | ||
surname: string; | ||
street: string; | ||
building: string; | ||
flat: string; | ||
city: string; | ||
region: string; | ||
postalCode: string; | ||
country: string; | ||
company: string; | ||
} | ||
|
||
export interface Customer { | ||
name: string; | ||
email: string; | ||
} | ||
|
||
export interface Redirects { | ||
success: string; | ||
failure: string; | ||
} |
21 changes: 21 additions & 0 deletions
21
src/models/payment/transaction/transaction-notification.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
interface TransactionNotification { | ||
id: string; | ||
service_id: string; | ||
status: string; | ||
amount: { | ||
value: number; | ||
currency: string; | ||
commission: number; | ||
}; | ||
control: string; | ||
channel: string; | ||
environment: string; | ||
originalAmount: { | ||
value: number; | ||
currency: string; | ||
rate: number; | ||
}; | ||
signature: string; | ||
} | ||
|
||
export type { TransactionNotification }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
interface TransactionRequest { | ||
amount: number; | ||
currency?: string; | ||
description?: string; | ||
control?: string; | ||
customer?: object; | ||
antifraud?: object; | ||
billing?: object; | ||
shipping?: object; | ||
cart?: object[]; | ||
returns?: { | ||
success: string; | ||
failure: string; | ||
}; | ||
directChannel?: string; | ||
channels?: string[]; | ||
channelTypes?: object; | ||
referer?: string; | ||
signature?: string; | ||
} | ||
|
||
export type { TransactionRequest }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import axios, { type AxiosInstance } from 'axios'; | ||
import { sha256 } from '../lib/hashing.js'; | ||
import type { TransactionNotification } from '../models/payment/transaction/transaction-notification.js'; | ||
import type { TransactionRequest } from '../models/payment/transaction/transaction-request.js'; | ||
|
||
export { Payment }; | ||
|
||
class Payment { | ||
private readonly client: AxiosInstance; | ||
|
||
constructor( | ||
private readonly key: string, | ||
private readonly password: string, | ||
) { | ||
this.client = axios.create({ | ||
baseURL: 'https://api.simpay.pl/payment', | ||
headers: { | ||
Authorization: `Bearer ${this.key}`, | ||
}, | ||
}); | ||
} | ||
|
||
// Generating transaction | ||
// https://docs.simpay.pl/#tag/Payment/operation/paymentTransactionCreate | ||
public async createTransaction(serviceId: string, request: TransactionRequest): Promise<any> { | ||
try { | ||
const response = await this.client.post(`/${serviceId}/transactions`, request); | ||
return response.data; | ||
} catch (error) { | ||
console.error('Error creating transaction:', error); | ||
throw error; | ||
} | ||
} | ||
|
||
// Receive transaction details (Webhook) | ||
// https://docs.simpay.pl/#tag/Payment/operation/paymentTransactionNotification | ||
public verifyNotification(key: string, body: TransactionNotification): boolean { | ||
const generatedSignature = this.generateSignatureNotification(key, body); | ||
return body.signature === generatedSignature; | ||
} | ||
|
||
// Generate signature for webhook | ||
private generateSignatureNotification(key: string, request: TransactionNotification): string { | ||
const joinedElements = [ | ||
request.id, | ||
request.service_id, | ||
request.status, | ||
request.amount.value, | ||
request.amount.currency, | ||
request.amount.commission, | ||
request.control, | ||
request.channel, | ||
request.environment, | ||
request.originalAmount.value, | ||
request.originalAmount.currency, | ||
request.originalAmount.rate, | ||
key, | ||
] | ||
.filter((e) => e !== undefined && e !== null) | ||
.join('|'); | ||
|
||
return sha256(joinedElements); | ||
} | ||
|
||
// Get transaction details | ||
// https://docs.simpay.pl/#tag/Payment/operation/paymentGetTransaction | ||
public getTransactionDetails(serviceId: string, transactionId: string): Promise<any> { | ||
return this.client.get(`/${serviceId}/transactions/${transactionId}`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.