An unofficial TypeScript/JavaScript SDK for integrating with the Payriff payment gateway.
npm install payriff-sdk-js
or
yarn add payriff-sdk-js
or
bun add payriff-sdk-js
P.S. Alternatively, you can use the SDK without installing it by copying the PayriffSDK.ts
file into your project.
Initialize the SDK with your merchant credentials:
import { PayriffSDK } from 'payriff-sdk-js'
const payriff = new PayriffSDK()
import { PayriffSDK } from 'payriff-sdk-js'
const payriff = new PayriffSDK({
// optional, defaults to https://api.payriff.com/api/v3
baseUrl: 'https://api.payriff.com/api/v3',
// optional, defaults to PAYRIFF_SECRET_KEY environment variable
secretKey: 'XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX',
// optional, defaults to PAYRIFF_CALLBACK_URL environment variable
defaultCallbackUrl: 'https://example.com/webhook',
// optional, defaults to 'AZ'
defaultLanguage: 'AZ',
// optional, defaults to 'AZN'
defaultCurrency: 'AZN',
})
Create a new payment order:
const order = await payriff.createOrder({
amount: 10.99,
description: 'Product purchase',
})
const order = await payriff.createOrder({
amount: 10.99,
language: 'EN', // optional, defaults to 'AZ'
currency: 'USD', // optional, defaults to 'AZN'
description: 'Product purchase',
callbackUrl: 'https://example.com/webhook', // optional, defaults to PAYRIFF_CALLBACK_URL environment variable
cardSave: true, // optional, defaults to false
operation: 'PURCHASE', // optional, defaults to 'PURCHASE'
})
Retrieve details about an existing order:
const orderInfo = await payriff.getOrderInfo('ORDER_ID')
Refund a completed payment:
const refund = await payriff.refund({
orderId: 'ORDER_ID',
amount: 10.99,
})
Complete a pre-authorized payment:
const complete = await payriff.complete({
orderId: 'ORDER_ID',
amount: 10.99,
})
Process payment using saved card details:
const autoPay = await payriff.autoPay({
cardUuid: 'CARD_UUID',
amount: 10.99,
description: 'Subscription renewal',
})
const autoPay = await payriff.autoPay({
cardUuid: 'CARD_UUID',
amount: 10.99,
currency: 'USD', // optional, defaults to 'AZN'
description: 'Subscription renewal',
callbackUrl: 'https://example.com/webhook', // optional, defaults to PAYRIFF_CALLBACK_URL environment variable
operation: 'PURCHASE', // optional, defaults to 'PURCHASE'
})
The SDK includes TypeScript definitions for all request and response types:
PayriffConfig
- SDK initialization optionsbaseUrl?
- API base URLsecretKey?
- Merchant secret keydefaultLanguage?
- Default payment page languagedefaultCurrency?
- Default currencydefaultCallbackUrl?
- Default callback URL for notifications
CreateOrderRequest
- Parameters for creating new orders- Required:
amount
- Payment amountdescription
- Order description
- Optional:
language
- Payment page language (defaults to 'AZ')currency
- Payment currency (defaults to 'AZN')cardSave
- Enable card saving (defaults to false)operation
- Payment operation type (defaults to 'PURCHASE')callbackUrl
- Result notification URL
- Required:
RefundRequest
- Parameters for refund operationsamount
- Refund amountorderId
- Order to refund
AutoPayRequest
- Parameters for automatic payments- Required:
cardUuid
- Saved card identifieramount
- Payment amountdescription
- Payment description
- Optional:
currency
- Payment currencycallbackUrl
- Result notification URLoperation
- Payment operation type
- Required:
PayriffResponse<T>
- Generic API response wrapperOrderPayload
- Created order detailsOrderInfo
- Detailed order informationTransaction
- Transaction detailsCardDetails
- Payment card information
-
PayriffResultCodes
- API response codesSUCCESS
: '00000'SUCCESS_GATEWAY
: '00'SUCCESS_GATEWAY_APPROVE
: 'APPROVED'SUCCESS_GATEWAY_PREAUTH_APPROVE
: 'PREAUTH-APPROVED'WARNING
: '01000'ERROR
: '15000'INVALID_PARAMETERS
: '15400'UNAUTHORIZED
: '14010'TOKEN_NOT_PRESENT
: '14013'INVALID_TOKEN
: '14014'
-
PayriffTypes
- Available optionsLanguages
: ['AZ', 'EN', 'RU']Currencies
: ['AZN', 'USD', 'EUR']Operations
: ['PURCHASE', 'PRE_AUTH']
All API operations return responses in this format:
{
code: string // Response status code
message: string // Human-readable message
route: string // API endpoint path
internalMessage: string | null // Additional details
responseId: string // Unique response ID
payload: T // Operation-specific data
}
isSuccessful(code: string)
- Check if an operation was successful based on the response code
MIT
Contributions are welcome! Please feel free to submit a Pull Request.