UNPKG

@gaian-sdk/payments

Version:

A powerful SDK for processing cryptocurrency payments via QR codes on the Solana blockchain.

153 lines 6.51 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.InitializeScanToPay = void 0; const axios_1 = __importDefault(require("axios")); const web3_js_1 = require("@solana/web3.js"); const ParseQr_1 = require("./requests/ParseQr"); const rate_1 = require("./requests/rate"); const status_1 = require("./requests/status"); const verifyOrder_1 = require("./requests/verifyOrder"); const placeOrder_1 = require("./requests/placeOrder"); const kyc_1 = require("./requests/kyc"); const user_1 = require("./requests/user"); const transaction_1 = require("./utils/transaction"); const getHistoryByUserName_1 = require("./requests/userInfo/getHistoryByUserName"); const getHistoryByWalletAddress_1 = require("./requests/userInfo/getHistoryByWalletAddress"); class InitializeScanToPay { paymentsHttpClient; userHttpClient; solanaConnection; config; qrRequests; exchangeRequests; statusRequests; verifyOrderRequests; placeOrderRequests; kycRequests; userRequests; userOrderRequestByName; userOrderRequestByWallet; constructor(config = {}) { this.config = { paymentsUrl: config.paymentsUrl, userUrl: config.userUrl, timeout: 10000, retries: 3, solanaRpcUrl: "https://api.mainnet-beta.solana.com", ...config, }; this.paymentsHttpClient = axios_1.default.create({ baseURL: this.config.paymentsUrl, timeout: this.config.timeout, headers: { "Content-Type": "application/json", }, }); this.userHttpClient = axios_1.default.create({ baseURL: this.config.userUrl, timeout: this.config.timeout, headers: { "Content-Type": "application/json", }, }); if (this.config.solanaRpcUrl) { this.solanaConnection = new web3_js_1.Connection(this.config.solanaRpcUrl, "confirmed"); } this.setupInterceptors(); this.qrRequests = new ParseQr_1.QrRequests(this.paymentsHttpClient); this.exchangeRequests = new rate_1.ExchangeRequests(this.paymentsHttpClient); this.statusRequests = new status_1.StatusRequests(this.paymentsHttpClient); this.verifyOrderRequests = new verifyOrder_1.VerifyOrderRequests(this.paymentsHttpClient); this.placeOrderRequests = new placeOrder_1.PlaceOrderRequests(this.paymentsHttpClient); this.kycRequests = new kyc_1.KYCRequests(this.userHttpClient); this.userRequests = new user_1.UserRequests(this.userHttpClient); this.userOrderRequestByName = new getHistoryByUserName_1.UserOrdersRequestsByName(this.userHttpClient); this.userOrderRequestByWallet = new getHistoryByWalletAddress_1.UserOrdersRequestsByWallet(this.userHttpClient); } setupInterceptors() { this.paymentsHttpClient.interceptors.request.use((config) => { return config; }, (error) => Promise.reject(error)); this.paymentsHttpClient.interceptors.response.use((response) => response, (error) => { if (error.response) { throw new Error(error.response.data?.message || "API Error"); } else if (error.request) { throw new Error("Network request failed"); } else { throw new Error("Request setup failed"); } }); } async parseQr(data) { const response = await this.qrRequests.parseQr(data); return response.data; } async calculateExchange(data) { const response = await this.exchangeRequests.calculateExchange(data); return response.data; } async getStatus(data) { const response = await this.statusRequests.getStatus(data); return response.data; } async verifyOrder(data) { const response = await this.verifyOrderRequests.verifyOrder(data); return response.data; } async placeOrder(data) { const response = await this.placeOrderRequests.placeOrder(data); return response.data; } async generateKYCLink(data) { const response = await this.kycRequests.generateKYCLink(data); return response.data; } async registerUser(data) { const response = await this.userRequests.registerUser(data); return response.data; } async getOrderHistoryByUserName(data) { const response = await this.userOrderRequestByName.getUserOrdersWithRequest(data); return response.data; } async getOrderHistoryByWallet(data) { const response = await this.userOrderRequestByWallet.getUserOrdersByWalletWithRequest(data); return response.data; } getSolanaConnection() { if (!this.solanaConnection) { throw new Error("Solana connection not initialized"); } return this.solanaConnection; } decodeTransaction = async (data) => { if (!this.solanaConnection) throw new Error("Solana connection not defined"); const { blockhash } = await this.solanaConnection.getLatestBlockhash("confirmed"); const transferInstruction = await (0, transaction_1.buildSolanaSendTransactionInstruction)(new web3_js_1.PublicKey(data.cryptoTransferInfo.fromAddress), new web3_js_1.PublicKey(data.cryptoTransferInfo.toAddress), new web3_js_1.PublicKey(data.cryptoTransferInfo.token), data.cryptoTransferInfo.amount); const transaction = new web3_js_1.Transaction(); transaction.add(transferInstruction); transaction.recentBlockhash = blockhash; transaction.feePayer = new web3_js_1.PublicKey(data.cryptoTransferInfo.fromAddress); return transaction; }; updateConfig(newConfig) { this.config = { ...this.config, ...newConfig }; if (newConfig.paymentsUrl) { this.paymentsHttpClient.defaults.baseURL = newConfig.paymentsUrl; } if (newConfig.timeout) { this.paymentsHttpClient.defaults.timeout = newConfig.timeout; } if (newConfig.solanaRpcUrl) { this.solanaConnection = new web3_js_1.Connection(newConfig.solanaRpcUrl, "confirmed"); } } } exports.InitializeScanToPay = InitializeScanToPay; //# sourceMappingURL=initialize.js.map