@easyflow/javascript-sdk
Version:
Enterprise-grade JavaScript SDK for Easyflow payment processing platform with enhanced credit card validation, comprehensive TypeScript definitions, and Lovable.dev integration support
422 lines (390 loc) • 12 kB
TypeScript
declare module '@easyflow/javascript-sdk' {
export interface EasyflowSDKConfig {
businessId: string
environment?: 'production' | 'sandbox'
debug?: boolean
timeout?: number
}
export interface CustomerData {
name: string
email: string
document: {
type: 'CPF' | 'CNPJ'
number: string
}
phone?: {
areaCode: string
ddd: string
number: string
isMobile: boolean
}
address?: {
zipCode: string
street: string
number: string
complement?: string
neighborhood: string
city: string
state: string
}
deliveryAddress?: {
zipCode: string
street: string
number: string
complement?: string
neighborhood: string
city: string
state: string
}
}
export interface PaymentMethod {
method: 'credit-card' | 'pix' | 'bank-billet'
numberInstallments: number
creditCard?: {
number: string
holderName: string
month: string
year: string
cvv: string
}
creditCardId?: string
}
export interface OrderData {
customer: CustomerData
payments: PaymentMethod[]
items?: Array<{
name: string
price: number
quantity?: number
}>
metadata?: Record<string, any>
}
export interface CreditCardData {
number: string
holderName: string
month: string
year: string
cvv: string
}
export interface Customer {
id: string
name: string
email: string
document: {
type: 'CPF' | 'CNPJ'
number: string
}
phone?: {
areaCode: string
ddd: string
number: string
isMobile: boolean
}
address?: {
zipCode: string
street: string
number: string
complement?: string
neighborhood: string
city: string
state: string
}
deliveryAddress?: {
zipCode: string
street: string
number: string
complement?: string
neighborhood: string
city: string
state: string
}
createdAt?: string
updatedAt?: string
}
export interface CreditCard {
creditCardId: string
brand?: string
last4Numbers?: string
expiresAtMonth?: string
expiresAtYear?: string
holderName?: string
}
export interface Order {
id: string
status: string
total: number
customer: Customer
payments: PaymentMethod[]
items?: Array<{
name: string
price: number
quantity: number
}>
createdAt?: string
updatedAt?: string
}
export interface Offer {
id: string
name: string
price: number
description?: string
items: Array<{
name: string
price: number
quantity: number
}>
}
export interface PixData {
qrCode: string
copyAndPasteCode: string
expirationDate: string
}
export interface BankBilletData {
link: string
line: string
barCode: string
expirationDate: string
}
export interface ValidationResult {
isValid: boolean
errors?: string[]
}
export class EasyflowSDK {
static readonly version: string
constructor(config: EasyflowSDKConfig | string)
// Core Methods
placeOrder(
offerId: string,
data: OrderData,
headers?: Record<string, string>
): Promise<string>
charge(
data: OrderData,
headers?: Record<string, string>
): Promise<string>
encrypt(
creditCardData: CreditCardData,
headers?: Record<string, string>
): Promise<string>
// Offer & Order Management
getOffer(
offerId: string,
headers?: Record<string, string>
): Promise<Offer>
getOrder(
orderId: string,
headers?: Record<string, string>
): Promise<Order>
// Payment Methods
getPix(
orderId: string,
headers?: Record<string, string>
): Promise<PixData | null>
getBankBillet(
orderId: string,
headers?: Record<string, string>
): Promise<BankBilletData | null>
// Customer Management
createCustomer(
customerData: CustomerData,
headers?: Record<string, string>
): Promise<Customer>
getCustomer(
customerId: string,
headers?: Record<string, string>
): Promise<Customer>
updateCustomer(
customerId: string,
updateData: Partial<CustomerData>,
headers?: Record<string, string>
): Promise<ValidationResult>
// Credit Card Management
addCreditCard(
customerId: string,
creditCardToken: string,
headers?: Record<string, string>
): Promise<CreditCard>
getCreditCard(
customerId: string,
creditCardId: string,
headers?: Record<string, string>
): Promise<CreditCard>
removeCreditCard(
customerId: string,
creditCardId: string,
headers?: Record<string, string>
): Promise<ValidationResult>
// Event System
on(
event: 'customerCreated' | 'orderPlaced' | 'paymentProcessed',
callback: (data: any) => void
): void
off(
event: 'customerCreated' | 'orderPlaced' | 'paymentProcessed',
callback: (data: any) => void
): void
}
// Validator Class
export class Validator {
static validateEmail(email: string): boolean
static validateCPF(cpf: string): boolean
static validateCNPJ(cnpj: string): boolean
static validateLegalDocument(document: {
type: 'CPF' | 'CNPJ'
number: string
}): boolean
static validatePhone(phone: {
areaCode: string
ddd: string
number: string
isMobile: boolean
}): boolean
static validateAddress(address: {
zipCode: string
street: string
number: string
complement?: string
neighborhood: string
city: string
state: string
}): boolean
static validateCustomer(customer: CustomerData): boolean
static validatePagination(page: number, limit: number): boolean
static validateBusinessId(businessId: string): boolean
static validateCreditCardToken(token: string): boolean
static validateOrderId(orderId: string): boolean
static validateOfferId(offerId: string): boolean
static validateCustomerId(customerId: string): boolean
static validateCreditCardId(creditCardId: string): boolean
static validateOrderData(orderData: OrderData): boolean
}
// Constants
export const PAYMENT_METHODS: {
readonly CREDIT_CARD: 'credit-card'
readonly PIX: 'pix'
readonly BANK_BILLET: 'bank-billet'
}
export const TARGETS: {
readonly CHARGE: 'charge'
readonly PLACE_ORDER: 'place-order'
readonly ENCRYPT: 'encrypt'
readonly GET_OFFER: 'get-offer'
readonly GET_ORDER: 'get-order'
}
export const HTTP_STATUS_CODES: {
readonly OK: 200
readonly CREATED: 201
readonly BAD_REQUEST: 400
readonly UNAUTHORIZED: 401
readonly FORBIDDEN: 403
readonly NOT_FOUND: 404
readonly INTERNAL_SERVER_ERROR: 500
}
export const HTTP_REQUEST_METHODS: {
readonly GET: 'GET'
readonly POST: 'POST'
readonly PUT: 'PUT'
readonly DELETE: 'DELETE'
}
// Error Classes
export class EasyflowError extends Error {
constructor(message: string, status?: number, code?: string)
status: number
code: string
}
export class SecurityError extends EasyflowError {
constructor(message: string)
}
export class ValidationError extends EasyflowError {
constructor(message: string)
}
export class NetworkError extends EasyflowError {
constructor(message: string)
}
export const ERROR_CODES: {
readonly VALIDATION_ERROR: 'VALIDATION_ERROR'
readonly SECURITY_ERROR: 'SECURITY_ERROR'
readonly NETWORK_ERROR: 'NETWORK_ERROR'
readonly UNKNOWN_ERROR: 'UNKNOWN_ERROR'
}
// Default export
export default EasyflowSDK
}
// Global instance for browser environments
declare global {
interface Window {
easyflowSDK: {
on(event: string, callback: (data: any) => void): void
off(event: string, callback: (data: any) => void): void
placeOrder(
offerId: string,
data: any,
headers?: Record<string, string>
): Promise<string>
charge(data: any, headers?: Record<string, string>): Promise<string>
encrypt(
creditCardData: any,
headers?: Record<string, string>
): Promise<string>
getOffer(
offerId: string,
headers?: Record<string, string>
): Promise<any>
getOrder(
orderId: string,
headers?: Record<string, string>
): Promise<any>
getPix(
orderId: string,
headers?: Record<string, string>
): Promise<any>
getBankBillet(
orderId: string,
headers?: Record<string, string>
): Promise<any>
createCustomer(
customerData: any,
headers?: Record<string, string>
): Promise<any>
getCustomer(
customerId: string,
headers?: Record<string, string>
): Promise<any>
updateCustomer(
customerId: string,
updateData: any,
headers?: Record<string, string>
): Promise<any>
addCreditCard(
customerId: string,
creditCardToken: string,
headers?: Record<string, string>
): Promise<any>
getCreditCard(
customerId: string,
creditCardId: string,
headers?: Record<string, string>
): Promise<any>
removeCreditCard(
customerId: string,
creditCardId: string,
headers?: Record<string, string>
): Promise<any>
validate: {
email(email: string): boolean
cpf(cpf: string): boolean
cnpj(cnpj: string): boolean
phone(phone: any): boolean
address(address: any): boolean
customer(customer: any): boolean
orderId(orderId: string): boolean
offerId(offerId: string): boolean
customerId(customerId: string): boolean
creditCardId(creditCardId: string): boolean
}
version: string
PAYMENT_METHODS: any
}
EasyflowSDK: typeof import('@easyflow/javascript-sdk').EasyflowSDK
}
}