simpay-typescript-api
Version:
SimPay.pl API wrapper
63 lines • 2.15 kB
JavaScript
import axios, {} from 'axios';
import { sha256 } from '../lib/hashing.js';
export { Payment };
class Payment {
key;
password;
client;
constructor(key, password) {
this.key = key;
this.password = password;
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
async createTransaction(serviceId, request) {
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
verifyNotification(key, body) {
const generatedSignature = this.generateSignatureNotification(key, body);
return body.signature === generatedSignature;
}
// Generate signature for webhook
generateSignatureNotification(key, request) {
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
getTransactionDetails(serviceId, transactionId) {
return this.client.get(`/${serviceId}/transactions/${transactionId}`);
}
}
//# sourceMappingURL=payment.js.map