forked from lucrise-llc/azul-ts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple-payment.ts
84 lines (74 loc) · 2.47 KB
/
simple-payment.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#!/usr/bin/env node
import 'dotenv/config';
import AzulAPI from '../src/azul-api/api';
import { randomUUID } from 'crypto';
import { getCard } from '../tests/fixtures/cards';
/**
* Simple payment workflow demonstration
*/
async function processPayment() {
try {
// Initialize Azul client
const azul = new AzulAPI({
auth1: process.env.AUTH1!,
auth2: process.env.AUTH2!,
merchantId: process.env.MERCHANT_ID!,
certificate: process.env.AZUL_CERT!,
key: process.env.AZUL_KEY!
});
// Test card details
const testCard = getCard('VISA_TEST_CARD');
const orderId = randomUUID();
const amount = 100; // RD$100
const ITBIS = 10; // RD$10
console.log('🔍 Environment Verification:');
console.log('Using Environment:', process.env.AZUL_ENV || 'dev');
console.log(
'Certificate Type:',
process.env.AZUL_CERT?.startsWith('-----') ? 'PEM Content' : 'File Path'
);
console.log(
'Key Type:',
process.env.AZUL_KEY?.startsWith('-----') ? 'PEM Content' : 'File Path'
);
console.log('Certificate Length:', process.env.AZUL_CERT?.length);
console.log('Key Length:', process.env.AZUL_KEY?.length);
console.log('⏳ Creating payment hold...');
const hold = await azul.payments.hold({
cardNumber: testCard.number,
expiration: testCard.expiration,
CVC: testCard.cvv,
customOrderId: orderId,
amount,
ITBIS
});
if (hold.IsoCode !== '00') {
throw new Error(`Hold failed: ${hold.ResponseMessage}`);
}
console.log('✅ Hold created successfully');
console.log(`🆔 Azul Order ID: ${hold.AzulOrderId}`);
console.log('⏳ Posting payment...');
const post = await azul.post({
azulOrderId: hold.AzulOrderId!,
amount,
ITBIS
});
if (post.IsoCode !== '00') {
throw new Error(`Post failed: ${post.ResponseMessage}`);
}
console.log('✅ Payment posted successfully');
console.log('⏳ Verifying transaction...');
const verification = await azul.verifyPayment(orderId);
if (!verification.Found) {
throw new Error('Payment verification failed: Transaction not found');
}
console.log('✅ Payment verified successfully');
console.log('📄 Verification details:', verification);
} catch (error) {
console.error('💥 Payment workflow failed:');
console.error(error instanceof Error ? error.message : error);
process.exit(1);
}
}
// Execute the payment workflow
processPayment();