-
Notifications
You must be signed in to change notification settings - Fork 25
/
main.ts
116 lines (96 loc) · 3.16 KB
/
main.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import {
ParsedAuthorizationResponse,
ParsedCancelResponse,
ParsedCaptureResponse,
} from '@primer-io/app-framework';
import StripeConnection from './Stripe';
(async () => {
console.log('\n=== TEST: authorization ===');
await testAuthTransaction();
console.log('\n=== TEST: capture ===');
await testCaptureTransaction();
console.log('\n=== TEST: cancel ===');
await testCancelTransaction();
})();
async function testAuthTransaction(): Promise<ParsedAuthorizationResponse> {
console.log(`Authorizing payment using "${StripeConnection.name}"`);
let response: ParsedAuthorizationResponse | null = null;
try {
response = await StripeConnection.authorize({
processorConfig: StripeConnection.configuration,
amount: 100,
currencyCode: 'GBP',
paymentMethod: {
expiryMonth: 4,
expiryYear: 2022,
cardholderName: 'Mr Foo Bar',
cvv: '020',
cardNumber: '4111111111111111',
},
});
} catch (e) {
console.error('Error while authorizing transaction:');
console.error(e);
process.exit(1);
}
console.log(
`Authorization request complete: "${response.transactionStatus}"`,
);
if (response.transactionStatus === 'FAILED') {
console.log(`Authorization Request failed: ${response.errorMessage}`);
process.exit(1);
}
if (response.transactionStatus === 'DECLINED') {
console.log(`Authorization was declined: ${response.declineReason}`);
process.exit(1);
}
return response;
}
async function testCancelTransaction(): Promise<void> {
const authResponse = await testAuthTransaction();
console.log('Cancelling authorized payment...');
if (authResponse.transactionStatus !== 'AUTHORIZED') {
console.error('Transaction must be AUTHORIZED in order to cancel it');
process.exit(1);
}
let response: ParsedCancelResponse | null = null;
try {
response = await StripeConnection.cancel({
processorTransactionId: authResponse.processorTransactionId,
processorConfig: StripeConnection.configuration,
});
} catch (e) {
console.error('Error while cancelling transaction:');
console.error(e);
process.exit(1);
}
if (response.transactionStatus !== 'CANCELLED') {
console.error(
`Expected transaction status to be "CANCELLED" but received "${response.transactionStatus}"`,
);
}
}
async function testCaptureTransaction(): Promise<void> {
const authResponse = await testAuthTransaction();
console.log('Capturing authorized payment...');
if (authResponse.transactionStatus !== 'AUTHORIZED') {
console.error('Transaction must be AUTHORIZED in order to capture it');
process.exit(1);
}
let response: ParsedCaptureResponse | null = null;
try {
response = await StripeConnection.capture({
processorTransactionId: authResponse.processorTransactionId,
processorConfig: StripeConnection.configuration,
});
} catch (e) {
console.error('Error while capturing transaction:');
console.error(e);
process.exit(1);
}
if (response.transactionStatus !== 'SETTLED') {
console.error(
`Expected transaction status to be "SETTLED" but received "${response.transactionStatus}"`,
);
}
}