wallet-pay-sdk
v1.5.1
Published
Wallet pay sdk
Downloads
95
Maintainers
Readme
wallet-pay-sdk
Package for using Wallet Pay API https://docs.wallet.tg/pay/
Supported endpoints
Create order /wpay/store-api/v1/order
Get order preview /wpay/store-api/v1/order/preview
Get order list /wpay/store-api/v1/reconciliation/order-list
Get order amount /wpay/store-api/v1/reconciliation/order-amount
Installation
npm i wallet-pay-sdk --save
Usage examples
init wallet pay sdk
import { WalletPaySDK } from 'wallet-pay-sdk';
const wp = new WalletPaySDK({
apiKey: 'secret_api_key',
timeoutSeconds: 60 * 60 * 3, // Default value = Order TTL, if the order is not paid within the timeout period
});
...
create new order
import { WalletPaySDK } from 'wallet-pay-sdk';
import { CreateOrderDto } from 'wallet-pay-sdk/lib/dto';
import { ECurrencyCode, ICreateOrderResponse, IResponseError } from 'wallet-pay-sdk/lib/type';
const wp = new WalletPaySDK({
apiKey: 'secret_api_key',
timeoutSeconds: 60 * 60 * 3, // Default value = Order TTL, if the order is not paid within the timeout period
});
const newOrder = {
amount: {
currencyCode: ECurrencyCode.TON,
amount: '10.67',
},
description: 'My first order', // Description of the order
returnUrl: 'https://example.com', // Url to redirect after paying order
failReturnUrl: 'https://example.com', // Url to redirect after unsuccessful order completion (expiration/cancelation/etc)
externalId: '5cfaf283-8242-4ddd-ae00-c9ecd6966245',
// timeoutSeconds: 200000; // If you want, you can override the value of the "timeoutSeconds" variable here
customerTelegramUserId: 12238398, // The customer's telegram id (User_id)
};
const result = await wp.createOrder(newOrder); // { response?: ICreateOrderResponse, error?: any }
if (result?.error) {
throw result?.error;
} else {
// success ...
}
get order preview
/wpay/store-api/v1/order/preview
import { WalletPaySDK } from 'wallet-pay-sdk';
import { IGetOrderPreviewResponse, IResponseError } from 'wallet-pay-sdk/lib/type';
const wp = new WalletPaySDK({
apiKey: 'secret_api_key',
timeoutSeconds: 60 * 60 * 3, // Default value = Order TTL, if the order is not paid within the timeout period
});
const orderId = '10797500785491970';
const result = await wp.getPreviewOrder(orderId) // { response?: IGetOrderPreviewResponse, error?: any }
if (result?.error) {
throw result?.error;
} else {
// success ...
}
get order list
/wpay/store-api/v1/reconciliation/order-list
import { WalletPaySDK } from 'wallet-pay-sdk';
import { GetOrderListDto } from 'wallet-pay-sdk/lib/dto';
import { IGetOrderListResponse } from 'wallet-pay-sdk/lib/type';
const wp = new WalletPaySDK({
apiKey: 'secret_api_key',
timeoutSeconds: 60 * 60 * 3, // Default value = Order TTL, if the order is not paid within the timeout period
});
const params: GetOrderListDto = {
offset: 0, // Specifying the amount of excluded from a response the first N orders
count: 10, // Specifying the limit of orders for the request
};
const result = await wp.getOrderList(params); // { response?: IGetOrderListResponse, error?: any }
if (result?.error) {
throw result?.error;
} else {
// success ...
}
get order amount
/wpay/store-api/v1/reconciliation/order-amount
import { WalletPaySDK } from 'wallet-pay-sdk';
import { IGetOrderAmountResponse, IResponseError } from 'wallet-pay-sdk/lib/type';
const wp = new WalletPaySDK({
apiKey: 'secret_api_key',
timeoutSeconds: 60 * 60 * 3, // Default value = Order TTL, if the order is not paid within the timeout period
});
const result = await wp.getOrderAmount(); // { response?: IGetOrderAmountResponse, error?: any }
if (result?.error) {
throw result?.error;
} else {
// success ...
}
webhook hashes verification
import { WalletPaySDK } from 'wallet-pay-sdk';
import { IWebhook, IWebhookRequest, IWebhookRequestSign, IResponseError } from 'wallet-pay-sdk/lib/type';
const wp = new WalletPaySDK({
apiKey: 'secret_api_key',
timeoutSeconds: 60 * 60 * 3, // Default value = Order TTL, if the order is not paid within the timeout period
});
/**
* Most of the data described below comes
* when Wallet Pay calls your endpoint.
* Only two parameters depend on you:
* - originalUrl
* - method
*/
const update: IWebhookRequest = {
body: [...], // webhook request body you can see type 'IWebhook' in types file
originalUrl: '/api/wallet-pay/webhook', // URI path exactly the same as set in the personal account
method: 'POST',
}
const signParams: IWebhookRequestSign = {
timestamp: '168824905680291', // HEADER: 'WalletPay-Timestamp'
signature: 'B7gy92BjFxILVctGG32fWBDEy4WW5iGzWs1kziNFGys', // HEADER: 'WalletPay-Signature'
}
const result: boolean | IResponseError = wp.webhookVerifyHash(update, signParams)
// if return TRUE - webhook call is verificated