type Convertkit = {
apiSecret: string
}
export async function main(
resource: Convertkit,
payload: {
transactionId: string
emailAddress: string
firstName?: string
currency: string
transactionTime: string
subtotal: number
tax: number
shipping: number
discount: number
total: number
status: string
product: {
pid: number
lid: number
name: string
sku: string
unitPrice: number
quantity: number
}
}
) {
const endpoint = `https://api.convertkit.com/v3/purchases`
const body = {
api_secret: resource.apiSecret,
purchase: {
transaction_id: payload.transactionId,
email_address: payload.emailAddress,
first_name: payload.firstName,
currency: payload.currency,
transaction_time: payload.transactionTime,
subtotal: payload.subtotal,
tax: payload.tax,
shipping: payload.shipping,
discount: payload.discount,
total: payload.total,
status: payload.status,
products: [payload.product]
}
}
const response = await fetch(endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(body)
})
if (!response.ok) {
const errorData = await response.json()
console.error('Error response:', errorData)
throw new Error(`HTTP error! status: ${response.status}`)
}
const data = await response.json()
return data
}
Submitted by hugo697 81 days ago