1+ import { expect } from "chai" ;
2+ import nock from "nock" ;
3+ import Checkout from '../../src/Checkout.js'
4+ import { NotFoundError , ValidationError , AuthenticationError , ActionNotAllowed } from "../../src/services/errors.js" ;
5+
6+ afterEach ( ( ) => {
7+ nock . cleanAll ( ) ;
8+ nock . enableNetConnect ( ) ;
9+ } ) ;
10+
11+ const cko = new Checkout ( process . env . CHECKOUT_DEFAULT_SECRET_KEY ) ;
12+ const processingChannelId = process . env . CHECKOUT_PROCESSING_CHANNEL_ID ;
13+
14+ describe ( 'Integration::Payment-Setups' , ( ) => {
15+ const createRequest = {
16+ processing_channel_id : processingChannelId ,
17+ amount : 1000 ,
18+ currency : "GBP" ,
19+ payment_type : "Regular" ,
20+ reference : `TEST-REF-${ Date . now ( ) } ` ,
21+ description : "Integration test payment setup" ,
22+ settings : {
23+ success_url : "https://example.com/success" ,
24+ failure_url : "https://example.com/failure"
25+ } ,
26+ customer : {
27+ name : "John Smith" ,
28+ email : {
29+ address : `john.smith+${ Date . now ( ) } @example.com` ,
30+ verified : true
31+ } ,
32+ phone : {
33+ country_code : "+44" ,
34+ number : "207 946 0000"
35+ } ,
36+ device : {
37+ locale : "en_GB"
38+ }
39+ } ,
40+ payment_methods : {
41+ klarna : {
42+ initialization : "disabled" ,
43+ account_holder : {
44+ billing_address : {
45+ address_line1 : "123 High Street" ,
46+ city : "London" ,
47+ zip : "SW1A 1AA" ,
48+ country : "GB"
49+ }
50+ }
51+ }
52+ }
53+ } ;
54+
55+ const updateRequest = {
56+ processing_channel_id : processingChannelId ,
57+ amount : 1500 ,
58+ currency : "GBP" ,
59+ payment_type : "Regular" ,
60+ reference : `TEST-REF-UPDATED-${ Date . now ( ) } ` ,
61+ description : "Updated integration test payment setup" ,
62+ settings : {
63+ success_url : "https://example.com/success-updated" ,
64+ failure_url : "https://example.com/failure-updated"
65+ } ,
66+ customer : {
67+ name : "John Smith Updated" ,
68+ email : {
69+ address : `john.smith.updated+${ Date . now ( ) } @example.com` ,
70+ verified : true
71+ } ,
72+ phone : {
73+ country_code : "+44" ,
74+ number : "207 946 0001"
75+ } ,
76+ device : {
77+ locale : "en_US"
78+ }
79+ } ,
80+ payment_methods : {
81+ klarna : {
82+ initialization : "disabled" ,
83+ account_holder : {
84+ billing_address : {
85+ address_line1 : "456 Updated Street" ,
86+ city : "Manchester" ,
87+ zip : "M1 2AB" ,
88+ country : "GB"
89+ }
90+ }
91+ }
92+ }
93+ } ;
94+
95+ describe ( 'Create and manage Payment Setup lifecycle' , ( ) => {
96+
97+ it ( 'should create a payment setup' , async ( ) => {
98+ const response = await cko . paymentSetups . createAPaymentSetup ( createRequest ) ;
99+
100+ expect ( response . id ) . not . to . be . null ;
101+ expect ( response . amount ) . to . equal ( 1000 ) ;
102+ expect ( response . currency ) . to . equal ( 'GBP' ) ;
103+ expect ( response . customer . email . address ) . to . include ( 'john.smith+' ) ;
104+ } ) ;
105+
106+ it ( 'should get a payment setup' , async ( ) => {
107+ // First create a payment setup
108+ const createResponse = await cko . paymentSetups . createAPaymentSetup ( createRequest ) ;
109+
110+ // Then retrieve it
111+ const response = await cko . paymentSetups . getAPaymentSetup ( createResponse . id ) ;
112+
113+ expect ( response . id ) . to . equal ( createResponse . id ) ;
114+ expect ( response . amount ) . to . equal ( 1000 ) ;
115+ expect ( response . currency ) . to . equal ( 'GBP' ) ;
116+ expect ( response . customer . email . address ) . to . include ( 'john.smith+' ) ;
117+ } ) ;
118+
119+ it ( 'should update a payment setup' , async ( ) => {
120+ // First create a payment setup
121+ const createResponse = await cko . paymentSetups . createAPaymentSetup ( createRequest ) ;
122+
123+ // Then update it
124+ const response = await cko . paymentSetups . updateAPaymentSetup ( createResponse . id , updateRequest ) ;
125+
126+ expect ( response . id ) . to . equal ( createResponse . id ) ;
127+ expect ( response . amount ) . to . equal ( 1500 ) ;
128+ expect ( response . customer . name ) . to . equal ( 'John Smith Updated' ) ;
129+ } ) ;
130+
131+ it ( 'should confirm a payment setup with a payment method option' , async ( ) => {
132+ try {
133+ // First create a payment setup
134+ const createResponse = await cko . paymentSetups . createAPaymentSetup ( createRequest ) ;
135+
136+ // Get the payment setup to find available payment method options
137+ const getResponse = await cko . paymentSetups . getAPaymentSetup ( createResponse . id ) ;
138+
139+ // Extract first available payment method option ID (if available)
140+ const paymentMethodOptions = getResponse . payment_method_options || [ ] ;
141+ if ( paymentMethodOptions . length > 0 ) {
142+ const paymentMethodOptionId = paymentMethodOptions [ 0 ] . id ;
143+
144+ // Confirm the payment setup
145+ const response = await cko . paymentSetups . confirmAPaymentSetup ( createResponse . id , paymentMethodOptionId ) ;
146+
147+ expect ( response . id ) . not . to . be . null ;
148+ expect ( response . status ) . not . to . be . null ;
149+ expect ( response . approved ) . to . be . a ( 'boolean' ) ;
150+ } else {
151+ // If no payment method options available, skip this test
152+ console . log ( 'Skipping confirm test - no payment method options available' ) ;
153+ }
154+ } catch ( error ) {
155+ // Payment setups might not be fully configured in test environment
156+ // so we expect certain validation errors
157+ expect ( error ) . to . be . instanceOf ( ValidationError ) ;
158+ }
159+ } ) ;
160+ } ) ;
161+
162+ describe ( 'Error handling' , ( ) => {
163+ it ( 'should throw NotFoundError when getting non-existent payment setup' , async ( ) => {
164+ try {
165+ await cko . paymentSetups . getAPaymentSetup ( 'psu_not_found' ) ;
166+ } catch ( err ) {
167+ expect ( err ) . to . be . instanceOf ( NotFoundError ) ;
168+ }
169+ } ) ;
170+
171+ it ( 'should throw NotFoundError when updating non-existent payment setup' , async ( ) => {
172+ const updateRequest = {
173+ amount : 1500 ,
174+ currency : "GBP"
175+ } ;
176+
177+ try {
178+ await cko . paymentSetups . updateAPaymentSetup ( 'psu_not_found' , updateRequest ) ;
179+ } catch ( err ) {
180+ expect ( err ) . to . be . instanceOf ( NotFoundError ) ;
181+ }
182+ } ) ;
183+
184+ it ( 'should throw NotFoundError when confirming non-existent payment setup' , async ( ) => {
185+ try {
186+ await cko . paymentSetups . confirmAPaymentSetup ( 'psu_not_found' , 'pmo_not_found' ) ;
187+ } catch ( err ) {
188+ expect ( err ) . to . be . instanceOf ( NotFoundError ) ;
189+ }
190+ } ) ;
191+
192+ it ( 'should throw ValidationError when creating payment setup with invalid data' , async ( ) => {
193+ const invalidRequest = {
194+ // Missing required fields
195+ amount : 'invalid_amount' ,
196+ currency : 'INVALID'
197+ } ;
198+
199+ try {
200+ await cko . paymentSetups . createAPaymentSetup ( invalidRequest ) ;
201+ } catch ( err ) {
202+ expect ( err . name ) . to . equal ( 'ValueError' ) ;
203+ expect ( err . body ) . to . equal ( 'The currency value is not valid.' ) ;
204+ }
205+ } ) ;
206+
207+ it ( 'should throw ValidationError when updating payment setup with invalid data' , async ( ) => {
208+ // First create a valid payment setup
209+ const createResponse = await cko . paymentSetups . createAPaymentSetup ( createRequest ) ;
210+
211+ const invalidUpdateRequest = {
212+ amount : 'invalid_amount' ,
213+ currency : 'INVALID'
214+ } ;
215+
216+ try {
217+ await cko . paymentSetups . updateAPaymentSetup ( createResponse . id , invalidUpdateRequest ) ;
218+ } catch ( err ) {
219+ expect ( err . name ) . to . equal ( 'ValueError' ) ;
220+ expect ( err . body ) . to . equal ( 'The currency value is not valid.' ) ;
221+ }
222+ } ) ;
223+ } ) ;
224+ } ) ;
0 commit comments