Zum Inhalt springen
CoinsSendEntwickler
Payments-Dokumentation

Node.js Example: Creating an Invoice

Markdown öffnenAgent einrichten

Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.

Run this code on a trusted server. Never expose the merchant API key in browser JavaScript.

const crypto = require('node:crypto')
function canonicalJson(payload) {
return JSON.stringify(payload)
.replace(/\//g, '\\/')
.replace(/[\u0080-\uFFFF]/g, character =>
`\\u${character.charCodeAt(0).toString(16).padStart(4, '0')}`
)
}
function legacySign(body, apiKey) {
const payloadBase64 = Buffer.from(body, 'utf8').toString('base64')
return crypto.createHash('md5').update(payloadBase64 + apiKey).digest('hex')
}
async function createInvoice({ merchantId, apiKey, orderId, amount }) {
const payload = {
order_id: orderId,
amount,
is_customer_fee: false,
is_customer_network_fee: false,
allow_card_payments: true,
success_url: 'https://merchant.example/payment-success',
cancel_url: 'https://merchant.example/payment-cancelled'
}
const body = canonicalJson(payload)
const response = await fetch('https://api.coinssend.com/v1/invoices', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Merchant: merchantId,
Sign: legacySign(body, apiKey)
},
body
})
const result = await response.json()
if (!response.ok) {
throw new Error(result.error || result.message || `HTTP ${response.status}`)
}
return result.data
}
const invoice = await createInvoice({
merchantId: process.env.COINSSEND_MERCHANT_ID,
apiKey: process.env.COINSSEND_API_KEY,
orderId: `order_${Date.now()}`,
amount: '100.50'
})
console.log(invoice.code, invoice.url)

Persist id, order_id, code, and url before redirecting the buyer. If the request outcome is unknown, do not generate a new order ID and retry blindly. A repeated accepted order_id returns 409 without replaying the original invoice response.