better-payment
Version:
Unified payment gateway library for Turkish payment providers
176 lines (174 loc) • 4.38 kB
JavaScript
;
// src/client/index.ts
var ProviderClient = class {
constructor(provider, config) {
this.provider = provider;
this.config = config;
}
/**
* Get fetch implementation
*/
get fetch() {
return this.config.fetch || globalThis.fetch;
}
/**
* Build full URL for endpoint
*/
buildUrl(path) {
const baseUrl = this.config.baseUrl.replace(/\/$/, "");
const cleanPath = path.replace(/^\//, "");
return `${baseUrl}/${this.provider}/${cleanPath}`;
}
/**
* Make HTTP request
*/
async request(method, path, body) {
const url = this.buildUrl(path);
const headers = {
"Content-Type": "application/json",
...this.config.headers
};
const options = {
method,
headers
};
if (body && method !== "GET") {
options.body = JSON.stringify(body);
}
const response = await this.fetch(url, options);
if (!response.ok) {
const error = await response.json().catch(() => ({
message: `HTTP ${response.status}: ${response.statusText}`
}));
throw new Error(error.message || "Request failed");
}
return response.json();
}
/**
* Create payment
*
* @example
* ```typescript
* const result = await client.iyzico.createPayment({
* price: '1.00',
* paidPrice: '1.00',
* currency: 'TRY',
* // ... other fields
* });
* ```
*/
async createPayment(request) {
return this.request("POST", "payment", request);
}
/**
* Initialize 3D Secure payment
*
* @example
* ```typescript
* const result = await client.iyzico.initThreeDSPayment({
* price: '1.00',
* paidPrice: '1.00',
* currency: 'TRY',
* callbackUrl: 'https://example.com/callback',
* // ... other fields
* });
*
* // Render 3DS HTML content
* document.getElementById('threeds-container').innerHTML = result.threeDSHtmlContent;
* ```
*/
async initThreeDSPayment(request) {
return this.request("POST", "payment/init-3ds", request);
}
/**
* Complete 3D Secure payment (callback handler)
*
* @example
* ```typescript
* // In your callback page:
* const callbackData = { ... }; // Data from provider
* const result = await client.iyzico.completeThreeDSPayment(callbackData);
* ```
*/
async completeThreeDSPayment(callbackData) {
return this.request("POST", "payment/complete-3ds", callbackData);
}
/**
* Refund payment
*
* @example
* ```typescript
* const result = await client.iyzico.refund({
* paymentTransactionId: '123456',
* price: '0.50',
* currency: 'TRY',
* });
* ```
*/
async refund(request) {
return this.request("POST", "refund", request);
}
/**
* Cancel payment
*
* @example
* ```typescript
* const result = await client.iyzico.cancel({
* paymentId: '123456',
* });
* ```
*/
async cancel(request) {
return this.request("POST", "cancel", request);
}
/**
* Get payment details
*
* @example
* ```typescript
* const payment = await client.iyzico.getPayment('payment-id-123');
* ```
*/
async getPayment(paymentId) {
return this.request("GET", `payment/${paymentId}`);
}
};
var BetterPayClient = class {
constructor(config) {
this.config = config;
this.iyzico = new ProviderClient("iyzico" /* IYZICO */, config);
this.paytr = new ProviderClient("paytr" /* PAYTR */, config);
}
/**
* Health check endpoint
*
* @example
* ```typescript
* const health = await client.health();
* console.log(health.status); // 'ok'
* console.log(health.providers); // ['iyzico', 'paytr']
* ```
*/
async health() {
const fetch = this.config.fetch || globalThis.fetch;
const url = `${this.config.baseUrl.replace(/\/$/, "")}/health`;
const response = await fetch(url, {
method: "GET",
headers: {
"Content-Type": "application/json",
...this.config.headers
}
});
if (!response.ok) {
throw new Error(`Health check failed: ${response.statusText}`);
}
return response.json();
}
};
function createBetterPayClient(config) {
return new BetterPayClient(config);
}
exports.BetterPayClient = BetterPayClient;
exports.createBetterPayClient = createBetterPayClient;
//# sourceMappingURL=index.js.map
//# sourceMappingURL=index.js.map