@cardql/core
Version:
CardQL core SDK for payment processing - cross-platform shared logic, auth and data access
1,275 lines (1,271 loc) • 25.9 kB
JavaScript
// src/client.ts
import { GraphQLClient } from "graphql-request";
var CardQLClient = class {
client;
config;
constructor(config) {
this.config = {
timeout: 3e4,
retries: 3,
...config
};
this.client = new GraphQLClient(this.config.endpoint, {
headers: {
Authorization: `Bearer ${this.config.apiKey}`,
"Content-Type": "application/json",
"User-Agent": "CardQL-SDK/1.0.0"
}
});
}
async request(query, variables) {
try {
const result = await this.client.request(query, variables);
return result;
} catch (error) {
throw this.normalizeError(error);
}
}
async requestWithRetry(query, variables, retries = this.config.retries) {
let lastError;
for (let attempt = 0; attempt <= retries; attempt++) {
try {
return await this.request(query, variables);
} catch (error) {
lastError = error;
if (error.response?.status >= 400 && error.response?.status < 500) {
throw error;
}
if (attempt < retries) {
await this.delay(Math.pow(2, attempt) * 1e3);
}
}
}
throw lastError;
}
setApiKey(apiKey) {
this.config.apiKey = apiKey;
this.client.setHeader("Authorization", `Bearer ${apiKey}`);
}
normalizeError(error) {
if (error.response?.errors?.length > 0) {
const gqlError = error.response.errors[0];
return {
message: gqlError.message,
code: gqlError.extensions?.code,
details: gqlError.extensions
};
}
if (error.response?.status) {
return {
message: `HTTP ${error.response.status}: ${error.response.statusText}`,
code: "HTTP_ERROR",
details: { status: error.response.status }
};
}
return {
message: error.message || "Unknown error occurred",
code: "UNKNOWN_ERROR",
details: error
};
}
delay(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
};
// src/queries.ts
var GET_ACCOUNTS = `
query GetAccounts {
accounts {
id
accountID
name
apps {
id
appID
name
apiKey
}
}
}
`;
var GET_ACCOUNT = `
query GetAccount($accountID: ID!) {
account(accountID: $accountID) {
id
accountID
name
apps {
id
appID
name
apiKey
}
}
}
`;
var CREATE_ACCOUNT = `
mutation CreateAccount($name: String!) {
createAccount(name: $name) {
id
accountID
name
}
}
`;
var UPDATE_ACCOUNT = `
mutation UpdateAccount($id: ID!, $name: String!) {
updateAccount(id: $id, name: $name) {
id
accountID
name
}
}
`;
var DELETE_ACCOUNT = `
mutation DeleteAccount($id: ID!) {
deleteAccount(id: $id)
}
`;
var GET_APPS = `
query GetApps {
apps {
id
appID
name
apiKey
}
}
`;
var GET_APP = `
query GetApp($id: ID!) {
app(id: $id) {
id
appID
name
apiKey
}
}
`;
var CREATE_APP = `
mutation CreateApp($accountID: ID!, $name: String!) {
createApp(accountID: $accountID, name: $name) {
id
appID
name
apiKey
}
}
`;
var UPDATE_APP = `
mutation UpdateApp($id: ID!, $name: String!) {
updateApp(id: $id, name: $name) {
id
appID
name
apiKey
}
}
`;
var DELETE_APP = `
mutation DeleteApp($id: ID!) {
deleteApp(id: $id)
}
`;
var GET_CUSTOMERS = `
query GetCustomers {
customers {
id
firstName
lastName
middleName
email
phone
address
country
status
isDeleted
isEnriched
provider
providerID
cards
merchants
createdAt
updatedAt
}
}
`;
var GET_CUSTOMER = `
query GetCustomer($id: ID!) {
customer(id: $id) {
id
firstName
lastName
middleName
email
phone
address
country
status
isDeleted
isEnriched
provider
providerID
cards
merchants
createdAt
updatedAt
}
}
`;
var CREATE_CUSTOMER = `
mutation CreateCustomer(
$firstName: String
$lastName: String
$middleName: String
$email: String
$phone: String
$address: JSON
$country: String
$status: String
$isDeleted: Boolean
$isEnriched: Boolean
$provider: String
$providerID: String
$cards: JSON
$merchants: JSON
) {
createCustomer(
firstName: $firstName
lastName: $lastName
middleName: $middleName
email: $email
phone: $phone
address: $address
country: $country
status: $status
isDeleted: $isDeleted
isEnriched: $isEnriched
provider: $provider
providerID: $providerID
cards: $cards
merchants: $merchants
) {
id
firstName
lastName
email
phone
status
createdAt
}
}
`;
var UPDATE_CUSTOMER = `
mutation UpdateCustomer(
$id: ID!
$firstName: String
$lastName: String
$middleName: String
$email: String
$phone: String
$address: JSON
$country: String
$status: String
$isDeleted: Boolean
$isEnriched: Boolean
$provider: String
$providerID: String
$cards: JSON
$merchants: JSON
) {
updateCustomer(
id: $id
firstName: $firstName
lastName: $lastName
middleName: $middleName
email: $email
phone: $phone
address: $address
country: $country
status: $status
isDeleted: $isDeleted
isEnriched: $isEnriched
provider: $provider
providerID: $providerID
cards: $cards
merchants: $merchants
) {
id
firstName
lastName
email
phone
status
updatedAt
}
}
`;
var DELETE_CUSTOMER = `
mutation DeleteCustomer($id: ID!) {
deleteCustomer(id: $id)
}
`;
var GET_MERCHANTS = `
query GetMerchants {
merchants {
id
name
descriptor
status
isKyc
isKyb
isDeleted
address
country
currency
beneficiaries
industry
banks
locations
balance
appID
pricingID
createdAt
updatedAt
}
}
`;
var GET_MERCHANT = `
query GetMerchant($id: ID!) {
merchant(id: $id) {
id
name
descriptor
status
isKyc
isKyb
isDeleted
address
country
currency
beneficiaries
industry
banks
locations
balance
appID
pricingID
createdAt
updatedAt
}
}
`;
var CREATE_MERCHANT = `
mutation CreateMerchant(
$name: String!
$descriptor: String
$status: String
$isKyc: Boolean
$isKyb: Boolean
$address: JSON
$country: String
$currency: String
$beneficiaries: JSON
$industry: JSON
$banks: JSON
$locations: JSON
$balance: JSON
$appID: String
$pricingID: String
) {
createMerchant(
name: $name
descriptor: $descriptor
status: $status
isKyc: $isKyc
isKyb: $isKyb
address: $address
country: $country
currency: $currency
beneficiaries: $beneficiaries
industry: $industry
banks: $banks
locations: $locations
balance: $balance
appID: $appID
pricingID: $pricingID
) {
id
name
status
country
currency
createdAt
}
}
`;
var UPDATE_MERCHANT = `
mutation UpdateMerchant(
$id: ID!
$name: String
$descriptor: String
$status: String
$isKyc: Boolean
$isKyb: Boolean
$address: JSON
$country: String
$currency: String
$beneficiaries: JSON
$industry: JSON
$banks: JSON
$locations: JSON
$balance: JSON
$appID: String
$pricingID: String
) {
updateMerchant(
id: $id
name: $name
descriptor: $descriptor
status: $status
isKyc: $isKyc
isKyb: $isKyb
address: $address
country: $country
currency: $currency
beneficiaries: $beneficiaries
industry: $industry
banks: $banks
locations: $locations
balance: $balance
appID: $appID
pricingID: $pricingID
) {
id
name
status
country
currency
updatedAt
}
}
`;
var DELETE_MERCHANT = `
mutation DeleteMerchant($id: ID!) {
deleteMerchant(id: $id)
}
`;
var GET_PAYMENTS = `
query GetPayments {
payments {
id
amount
currency
merchantID
userID
description
status
provider
providerID
providerAccountID
customerID
locationID
terminalID
metadata
isCnp
capturableAmount
capturedAmount
refundableAmount
netAmount
fees
settlementStatus
settledAt
fundsAvailableAt
createdAt
updatedAt
# Enhanced subcollection statistics
authorizationCount
ledgerEntryCount
settlementCount
totalCredits
totalDebits
netLedgerAmount
}
}
`;
var GET_PAYMENT = `
query GetPayment($id: ID!) {
payment(id: $id) {
id
amount
currency
merchantID
userID
description
status
provider
providerID
providerAccountID
customerID
locationID
terminalID
metadata
isCnp
capturableAmount
capturedAmount
refundableAmount
netAmount
fees
settlementStatus
settledAt
fundsAvailableAt
createdAt
updatedAt
# Enhanced subcollection statistics
authorizationCount
ledgerEntryCount
settlementCount
totalCredits
totalDebits
netLedgerAmount
}
}
`;
var GET_PAYMENT_WITH_DETAILS = `
query GetPaymentWithDetails($id: ID!) {
payment(id: $id) {
id
amount
currency
merchantID
userID
description
status
provider
providerID
providerAccountID
customerID
locationID
terminalID
metadata
isCnp
capturableAmount
capturedAmount
refundableAmount
netAmount
fees
settlementStatus
settledAt
fundsAvailableAt
createdAt
updatedAt
# Subcollection data
authorizations {
id
provider
providerId
amount
amountCaptured
amountRefunded
paymentMethodId
network
networkTransactionId
receiptNumber
receiptUrl
authCode
brand
funding
metadata
createdAt
updatedAt
}
ledgerEntries {
id
accountId
amount
currency
description
preBalance
postBalance
isSettled
direction
type
status
reference
settledAt
metadata
createdAt
updatedAt
}
settlements {
id
amount
currency
status
provider
providerId
settledAt
metadata
createdAt
updatedAt
}
# Statistics
authorizationCount
ledgerEntryCount
settlementCount
totalCredits
totalDebits
netLedgerAmount
}
}
`;
var GET_PAYMENT_AUTHORIZATIONS = `
query GetPaymentAuthorizations($paymentId: ID!) {
paymentAuthorizations(paymentId: $paymentId) {
id
provider
providerId
amount
amountCaptured
amountRefunded
paymentMethodId
network
networkTransactionId
receiptNumber
receiptUrl
authCode
brand
funding
metadata
createdAt
updatedAt
}
}
`;
var GET_PAYMENT_LEDGER_ENTRIES = `
query GetPaymentLedgerEntries($paymentId: ID!) {
paymentLedgerEntries(paymentId: $paymentId) {
id
accountId
amount
currency
description
preBalance
postBalance
isSettled
direction
type
status
reference
settledAt
metadata
createdAt
updatedAt
}
}
`;
var GET_PAYMENT_SETTLEMENTS = `
query GetPaymentSettlements($paymentId: ID!) {
paymentSettlements(paymentId: $paymentId) {
id
amount
currency
status
provider
providerId
settledAt
metadata
createdAt
updatedAt
}
}
`;
var CREATE_PAYMENT = `
mutation CreatePayment(
$amount: String!
$currency: String!
$merchantID: String!
$userID: String!
$description: String
$status: String
$provider: String
$providerID: String
$providerAccountID: String
$customerID: String
$locationID: String
$terminalID: String
$metadata: JSON
$isCnp: Boolean
$capturableAmount: String
$capturedAmount: String
$refundableAmount: String
$netAmount: String
$fees: String
$settlementStatus: String
$settledAt: String
$fundsAvailableAt: String
) {
createPayment(
amount: $amount
currency: $currency
merchantID: $merchantID
userID: $userID
description: $description
status: $status
provider: $provider
providerID: $providerID
providerAccountID: $providerAccountID
customerID: $customerID
locationID: $locationID
terminalID: $terminalID
metadata: $metadata
isCnp: $isCnp
capturableAmount: $capturableAmount
capturedAmount: $capturedAmount
refundableAmount: $refundableAmount
netAmount: $netAmount
fees: $fees
settlementStatus: $settlementStatus
settledAt: $settledAt
fundsAvailableAt: $fundsAvailableAt
) {
id
amount
currency
merchantID
userID
status
createdAt
# Enhanced statistics
authorizationCount
ledgerEntryCount
settlementCount
}
}
`;
var UPDATE_PAYMENT = `
mutation UpdatePayment(
$id: ID!
$amount: String
$currency: String
$merchantID: String
$userID: String
$description: String
$status: String
$provider: String
$providerID: String
$providerAccountID: String
$customerID: String
$locationID: String
$terminalID: String
$metadata: JSON
$isCnp: Boolean
$capturableAmount: String
$capturedAmount: String
$refundableAmount: String
$netAmount: String
$fees: String
$settlementStatus: String
$settledAt: String
$fundsAvailableAt: String
) {
updatePayment(
id: $id
amount: $amount
currency: $currency
merchantID: $merchantID
userID: $userID
description: $description
status: $status
provider: $provider
providerID: $providerID
providerAccountID: $providerAccountID
customerID: $customerID
locationID: $locationID
terminalID: $terminalID
metadata: $metadata
isCnp: $isCnp
capturableAmount: $capturableAmount
capturedAmount: $capturedAmount
refundableAmount: $refundableAmount
netAmount: $netAmount
fees: $fees
settlementStatus: $settlementStatus
settledAt: $settledAt
fundsAvailableAt: $fundsAvailableAt
) {
id
amount
currency
status
updatedAt
# Enhanced statistics
authorizationCount
ledgerEntryCount
settlementCount
totalCredits
totalDebits
netLedgerAmount
}
}
`;
var DELETE_PAYMENT = `
mutation DeletePayment($id: ID!) {
deletePayment(id: $id)
}
`;
var CREATE_PAYMENT_AUTHORIZATION = `
mutation CreatePaymentAuthorization(
$paymentId: ID!
$provider: String!
$providerId: String!
$amount: String
$amountCaptured: String
$amountRefunded: String
$paymentMethodId: String!
$network: String
$networkTransactionId: String
$receiptNumber: String
$receiptUrl: String
$authCode: String
$metadata: JSON
) {
createPaymentAuthorization(
paymentId: $paymentId
provider: $provider
providerId: $providerId
amount: $amount
amountCaptured: $amountCaptured
amountRefunded: $amountRefunded
paymentMethodId: $paymentMethodId
network: $network
networkTransactionId: $networkTransactionId
receiptNumber: $receiptNumber
receiptUrl: $receiptUrl
authCode: $authCode
metadata: $metadata
) {
id
provider
providerId
amount
paymentMethodId
authCode
createdAt
}
}
`;
var CREATE_PAYMENT_LEDGER_ENTRY = `
mutation CreatePaymentLedgerEntry(
$paymentId: ID!
$accountId: ID!
$amount: String!
$currency: String!
$description: String
$direction: String!
$type: String!
$status: String
$reference: String
$metadata: JSON
) {
createPaymentLedgerEntry(
paymentId: $paymentId
accountId: $accountId
amount: $amount
currency: $currency
description: $description
direction: $direction
type: $type
status: $status
reference: $reference
metadata: $metadata
) {
id
amount
currency
direction
type
status
createdAt
}
}
`;
var CREATE_PAYMENT_SETTLEMENT = `
mutation CreatePaymentSettlement(
$paymentId: ID!
$amount: String!
$currency: String!
$status: String!
$provider: String
$providerId: String
$settledAt: String
$metadata: JSON
) {
createPaymentSettlement(
paymentId: $paymentId
amount: $amount
currency: $currency
status: $status
provider: $provider
providerId: $providerId
settledAt: $settledAt
metadata: $metadata
) {
id
amount
currency
status
provider
settledAt
createdAt
}
}
`;
var GET_LEDGERS = `
query GetLedgers {
ledgers {
id
amount
currency
merchantID
description
direction
type
status
reference
preBalance
postBalance
isSettled
paymentID
metadata
createdAt
updatedAt
}
}
`;
var GET_LEDGER = `
query GetLedger($id: ID!) {
ledger(id: $id) {
id
amount
currency
merchantID
description
direction
type
status
reference
preBalance
postBalance
isSettled
paymentID
metadata
createdAt
updatedAt
}
}
`;
var CREATE_LEDGER = `
mutation CreateLedger(
$amount: String!
$currency: String!
$merchantID: String!
$description: String
$direction: String
$type: String
$status: String
$reference: String
$preBalance: String
$postBalance: String
$isSettled: Boolean
$paymentID: String
$metadata: JSON
) {
createLedger(
amount: $amount
currency: $currency
merchantID: $merchantID
description: $description
direction: $direction
type: $type
status: $status
reference: $reference
preBalance: $preBalance
postBalance: $postBalance
isSettled: $isSettled
paymentID: $paymentID
metadata: $metadata
) {
id
amount
currency
merchantID
status
createdAt
}
}
`;
var UPDATE_LEDGER = `
mutation UpdateLedger(
$id: ID!
$amount: String
$currency: String
$merchantID: String
$description: String
$direction: String
$type: String
$status: String
$reference: String
$preBalance: String
$postBalance: String
$isSettled: Boolean
$paymentID: String
$metadata: JSON
) {
updateLedger(
id: $id
amount: $amount
currency: $currency
merchantID: $merchantID
description: $description
direction: $direction
type: $type
status: $status
reference: $reference
preBalance: $preBalance
postBalance: $postBalance
isSettled: $isSettled
paymentID: $paymentID
metadata: $metadata
) {
id
amount
currency
status
updatedAt
}
}
`;
var DELETE_LEDGER = `
mutation DeleteLedger($id: ID!) {
deleteLedger(id: $id)
}
`;
// src/api.ts
var CardQLApi = class {
constructor(client) {
this.client = client;
}
// Account methods
async getAccounts() {
const result = await this.client.requestWithRetry(
GET_ACCOUNTS
);
return result.accounts;
}
async getAccount(accountID) {
const result = await this.client.requestWithRetry(GET_ACCOUNT, { accountID });
return result.account;
}
async createAccount(input) {
const result = await this.client.requestWithRetry(CREATE_ACCOUNT, input);
return result.createAccount;
}
async updateAccount(input) {
const result = await this.client.requestWithRetry(UPDATE_ACCOUNT, input);
return result.updateAccount;
}
async deleteAccount(id) {
const result = await this.client.requestWithRetry(DELETE_ACCOUNT, { id });
return result.deleteAccount;
}
// App methods
async getApps() {
const result = await this.client.requestWithRetry(
GET_APPS
);
return result.apps;
}
async getApp(id) {
const result = await this.client.requestWithRetry(
GET_APP,
{ id }
);
return result.app;
}
async createApp(input) {
const result = await this.client.requestWithRetry(
CREATE_APP,
input
);
return result.createApp;
}
async updateApp(input) {
const result = await this.client.requestWithRetry(
UPDATE_APP,
input
);
return result.updateApp;
}
async deleteApp(id) {
const result = await this.client.requestWithRetry(
DELETE_APP,
{ id }
);
return result.deleteApp;
}
// Customer methods
async getCustomers() {
const result = await this.client.requestWithRetry(GET_CUSTOMERS);
return result.customers;
}
async getCustomer(id) {
const result = await this.client.requestWithRetry(GET_CUSTOMER, { id });
return result.customer;
}
async createCustomer(input) {
const result = await this.client.requestWithRetry(CREATE_CUSTOMER, input);
return result.createCustomer;
}
async updateCustomer(input) {
const result = await this.client.requestWithRetry(UPDATE_CUSTOMER, input);
return result.updateCustomer;
}
async deleteCustomer(id) {
const result = await this.client.requestWithRetry(DELETE_CUSTOMER, { id });
return result.deleteCustomer;
}
// Merchant methods
async getMerchants() {
const result = await this.client.requestWithRetry(GET_MERCHANTS);
return result.merchants;
}
async getMerchant(id) {
const result = await this.client.requestWithRetry(GET_MERCHANT, { id });
return result.merchant;
}
async createMerchant(input) {
const result = await this.client.requestWithRetry(CREATE_MERCHANT, input);
return result.createMerchant;
}
async updateMerchant(input) {
const result = await this.client.requestWithRetry(UPDATE_MERCHANT, input);
return result.updateMerchant;
}
async deleteMerchant(id) {
const result = await this.client.requestWithRetry(DELETE_MERCHANT, { id });
return result.deleteMerchant;
}
// Payment methods
async getPayments() {
const result = await this.client.requestWithRetry(
GET_PAYMENTS
);
return result.payments;
}
async getPayment(id) {
const result = await this.client.requestWithRetry(GET_PAYMENT, { id });
return result.payment;
}
async createPayment(input) {
const result = await this.client.requestWithRetry(CREATE_PAYMENT, input);
return result.createPayment;
}
async updatePayment(input) {
const result = await this.client.requestWithRetry(UPDATE_PAYMENT, input);
return result.updatePayment;
}
async deletePayment(id) {
const result = await this.client.requestWithRetry(DELETE_PAYMENT, { id });
return result.deletePayment;
}
// Ledger methods
async getLedgers() {
const result = await this.client.requestWithRetry(
GET_LEDGERS
);
return result.ledgers;
}
async getLedger(id) {
const result = await this.client.requestWithRetry(GET_LEDGER, { id });
return result.ledger;
}
async createLedger(input) {
const result = await this.client.requestWithRetry(
CREATE_LEDGER,
input
);
return result.createLedger;
}
async updateLedger(input) {
const result = await this.client.requestWithRetry(
UPDATE_LEDGER,
input
);
return result.updateLedger;
}
async deleteLedger(id) {
const result = await this.client.requestWithRetry(
DELETE_LEDGER,
{ id }
);
return result.deleteLedger;
}
};
// src/index.ts
var CardQL = class {
client;
api;
constructor(config) {
this.client = new CardQLClient(config);
this.api = new CardQLApi(this.client);
}
// Convenience method to update API key
setApiKey(apiKey) {
this.client.setApiKey(apiKey);
}
};
var index_default = CardQL;
export {
CREATE_ACCOUNT,
CREATE_APP,
CREATE_CUSTOMER,
CREATE_LEDGER,
CREATE_MERCHANT,
CREATE_PAYMENT,
CREATE_PAYMENT_AUTHORIZATION,
CREATE_PAYMENT_LEDGER_ENTRY,
CREATE_PAYMENT_SETTLEMENT,
CardQL,
CardQLApi,
CardQLClient,
DELETE_ACCOUNT,
DELETE_APP,
DELETE_CUSTOMER,
DELETE_LEDGER,
DELETE_MERCHANT,
DELETE_PAYMENT,
GET_ACCOUNT,
GET_ACCOUNTS,
GET_APP,
GET_APPS,
GET_CUSTOMER,
GET_CUSTOMERS,
GET_LEDGER,
GET_LEDGERS,
GET_MERCHANT,
GET_MERCHANTS,
GET_PAYMENT,
GET_PAYMENTS,
GET_PAYMENT_AUTHORIZATIONS,
GET_PAYMENT_LEDGER_ENTRIES,
GET_PAYMENT_SETTLEMENTS,
GET_PAYMENT_WITH_DETAILS,
UPDATE_ACCOUNT,
UPDATE_APP,
UPDATE_CUSTOMER,
UPDATE_LEDGER,
UPDATE_MERCHANT,
UPDATE_PAYMENT,
index_default as default
};