44import com .pensio .Amount ;
55import com .pensio .TaxType ;
66import com .pensio .api .generated .APIResponse ;
7+ import com .pensio .api .generated .Session ;
78import com .pensio .api .request .*;
89
10+ import com .pensio .response .CheckoutSessionResponse ;
911import jakarta .xml .bind .JAXBElement ;
1012import jakarta .xml .bind .JAXBException ;
1113
@@ -29,21 +31,20 @@ public PensioMerchantAPI(String baseURL, String username, String password)
2931 public boolean login () throws PensioAPIException
3032 {
3133 APIResponse response = getAPIResponse ("login" ,
32- HttpMethod .POST ,
33- new HashMap <String , String >());
34+ HttpMethod .POST ,
35+ new HashMap <>());
3436 return "OK" .equals (response .getBody ().getResult ());
3537 }
3638
3739 public PaymentRequestResponse createPaymentRequest (PaymentRequest paymentRequest ) throws PensioAPIException
3840 {
3941 try
4042 {
41- HashMap <String , String > params = new HashMap <String , String >();
43+ HashMap <String , String > params = new HashMap <>();
4244 setPaymentRequestParameters (paymentRequest , params );
4345
4446 APIResponse response = getAPIResponse ("createPaymentRequest" , HttpMethod .POST , params );
4547
46-
4748 PaymentRequestResponseImpl paymentRequestResponseImpl = new PaymentRequestResponseImpl ()
4849 .setUrl (new URL (response .getBody ().getUrl ()))
4950 .setPaymentRequestId (response .getBody ().getPaymentRequestId ());
@@ -58,12 +59,11 @@ public PaymentRequestResponse createPaymentRequest(PaymentRequest paymentRequest
5859 {
5960 throw new PensioAPIException (e );
6061 }
61-
6262 }
6363
6464 public APIResponse createInvoiceReservation (CreateInvoiceReservationRequest request ) throws PensioAPIException
6565 {
66- HashMap <String , String > params = new HashMap <String , String >();
66+ HashMap <String , String > params = new HashMap <>();
6767
6868 setInvoiceReservationRequestParameters (request , params );
6969
@@ -72,7 +72,7 @@ public APIResponse createInvoiceReservation(CreateInvoiceReservationRequest requ
7272
7373 public APIResponse updateOrder (UpdateOrderRequest request ) throws PensioAPIException
7474 {
75- HashMap <String , String > params = new HashMap <String , String >();
75+ HashMap <String , String > params = new HashMap <>();
7676
7777 setUpdateOrderRequestParameters (request , params );
7878
@@ -81,7 +81,7 @@ public APIResponse updateOrder(UpdateOrderRequest request) throws PensioAPIExcep
8181
8282 public APIResponse reservation (PaymentReservationRequest request ) throws PensioAPIException
8383 {
84- HashMap <String , String > params = new HashMap <String , String >();
84+ HashMap <String , String > params = new HashMap <>();
8585 setPaymentRequestParameters (request , params );
8686 setCreditCardRequestParameters (request , params );
8787 setPaymentSource (request , params );
@@ -98,33 +98,33 @@ private void setPaymentSource(PaymentReservationRequest request,
9898 }
9999 }
100100
101- public APIResponse capture (CaptureReservationRequest request ) throws PensioAPIException
101+ public APIResponse capture (CaptureReservationRequest request ) throws PensioAPIException
102102 {
103- HashMap <String , String > params = new HashMap <String , String >();
103+ HashMap <String , String > params = new HashMap <>();
104104 addParam (params , "transaction_id" , request .getTransactionId ());
105105 addParam (params , "amount" , request .getAmountString ());
106106 addParam (params , "reconciliation_identifier" , request .getReconciliationIdentifier ());
107107 addParam (params , "invoice_number" , request .getInvoiceNumber ());
108108 addParam (params , "sales_tax" , request .getSalesTax ());
109109 OrderLine [] orderLines = request .getOrderLines ();
110- addOrderLines ("orderLines" , params , Arrays .asList (orderLines ));
110+ addOrderLines ("orderLines" , params , Arrays .asList (orderLines ));
111111 return getAPIResponse ("captureReservation" , HttpMethod .POST , params );
112112 }
113113
114- public APIResponse refund (RefundRequest request ) throws PensioAPIException
114+ public APIResponse refund (RefundRequest request ) throws PensioAPIException
115115 {
116- HashMap <String , String > params = new HashMap <String , String >();
116+ HashMap <String , String > params = new HashMap <>();
117117 addParam (params , "transaction_id" , request .getPaymentId ());
118118 addParam (params , "amount" , request .getAmountString ());
119119 addParam (params , "reconciliation_identifier" , request .getReconciliationIdentifier ());
120120 OrderLine [] orderLines = request .getOrderLines ();
121- addOrderLines ("orderLines" , params , Arrays .asList (orderLines ));
121+ addOrderLines ("orderLines" , params , Arrays .asList (orderLines ));
122122 return getAPIResponse ("refundCapturedReservation" , HttpMethod .POST , params );
123123 }
124-
125- public APIResponse chargeSubscription (ChargeSubscriptionRequest request ) throws PensioAPIException
124+
125+ public APIResponse chargeSubscription (ChargeSubscriptionRequest request ) throws PensioAPIException
126126 {
127- HashMap <String , String > params = new HashMap <String , String >();
127+ HashMap <String , String > params = new HashMap <>();
128128 addParam (params , "agreement[id]" , request .getAgreementId ());
129129 addParam (params , "amount" , request .getAmountString ());
130130 addParam (params , "reconciliation_identifier" , request .getReconciliationIdentifier ());
@@ -153,10 +153,10 @@ public APIResponse chargeSubscription(ChargeSubscriptionRequest request) throws
153153
154154 return getAPIResponse ("chargeSubscription" , HttpMethod .POST , params );
155155 }
156-
157- public APIResponse reserveSubscriptionCharge (ReserveSubscriptionChargeRequest request ) throws PensioAPIException
156+
157+ public APIResponse reserveSubscriptionCharge (ReserveSubscriptionChargeRequest request ) throws PensioAPIException
158158 {
159- HashMap <String , String > params = new HashMap <String , String >();
159+ HashMap <String , String > params = new HashMap <>();
160160 addParam (params , "agreement[id]" , request .getAgreementId ());
161161 addParam (params , "amount" , request .getAmountString ());
162162
@@ -184,36 +184,36 @@ public APIResponse reserveSubscriptionCharge(ReserveSubscriptionChargeRequest re
184184
185185 return getAPIResponse ("reserveSubscriptionCharge" , HttpMethod .POST , params );
186186 }
187-
188- public APIResponse fundingList (FundingListRequest request ) throws PensioAPIException
187+
188+ public APIResponse fundingList (FundingListRequest request ) throws PensioAPIException
189189 {
190- HashMap <String , String > params = new HashMap <String , String >();
190+ HashMap <String , String > params = new HashMap <>();
191191 addParam (params , "page" , String .valueOf (request .getPage ()));
192-
192+
193193 return getAPIResponse ("fundingList" , HttpMethod .GET , params );
194194 }
195195
196196 public APIResponse getTerminals () throws PensioAPIException {
197- return getAPIResponse ("getTerminals" , HttpMethod .GET , new HashMap <String , String >());
197+ return getAPIResponse ("getTerminals" , HttpMethod .GET , new HashMap <>());
198198 }
199-
199+
200200 public List <FundingRecord > downloadFunding (String downloadLink ) throws PensioAPIException
201201 {
202202 try
203203 {
204- InputStream inStream = this .httpHelper .doPost (downloadLink , new HashMap <String , String >(), username , password , getSdkVersion ());
204+ InputStream inStream = this .httpHelper .doPost (downloadLink , new HashMap <>(), username , password , getSdkVersion ());
205205
206206 CsvReader reader = new CsvReader (inStream ,';' , Charset .forName ("UTF-8" ));
207- ArrayList <FundingRecord > result = new ArrayList <FundingRecord >();
208-
207+ ArrayList <FundingRecord > result = new ArrayList <>();
208+
209209 if (reader .readHeaders ())
210210 {
211211 while (reader .readRecord ())
212212 {
213213 FundingRecord record = new FundingRecord ();
214214 String date = reader .get ("Date" );
215215 System .out .println ("Date: " +date );
216-
216+
217217 if (date .matches ("[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}" ))
218218 {
219219 record .setFundingDate (DateHelper .parseDate ("yyyy-MM-dd hh:mm:ss" , date ));
@@ -222,7 +222,7 @@ public List<FundingRecord> downloadFunding(String downloadLink) throws PensioAPI
222222 {
223223 record .setFundingDate (DateHelper .parseDate ("yyyy-MM-dd" , date ));
224224 }
225-
225+
226226 record .setRecordType (reader .get ("Type" ));
227227 record .setId (reader .get ("ID" ));
228228 record .setReconciliationIdentifier (reader .get ("Reconciliation Identifier" ));
@@ -232,7 +232,7 @@ public List<FundingRecord> downloadFunding(String downloadLink) throws PensioAPI
232232 record .setShop (reader .get ("Shop" ));
233233 record .setPaymentAmount (Amount .get (reader .get ("Transaction Amount" ), reader .get ("Transaction Currency" )));
234234 record .setFundingAmount (Amount .get (reader .get ("Settlement Amount" ), reader .get ("Settlement Currency" )));
235-
235+
236236 String exRate = reader .get ("Exchange Rate" );
237237 if (exRate == null || exRate .length () == 0 )
238238 {
@@ -242,38 +242,37 @@ public List<FundingRecord> downloadFunding(String downloadLink) throws PensioAPI
242242 {
243243 record .setExchangeRate (Double .parseDouble (exRate ));
244244 }
245-
245+
246246 record .setFixedFeeAmount (Amount .get (reader .get ("Fixed Fee" ), reader .get ("Settlement Currency" )));
247247 record .setFixedFeeVatAmount (Amount .get (reader .get ("Fixed Fee VAT" ), reader .get ("Settlement Currency" )));
248248 record .setRateBasedFeeAmount (Amount .get (reader .get ("Rate Based Fee" ), reader .get ("Settlement Currency" )));
249249 record .setRateBasedFeeVatAmount (Amount .get (reader .get ("Rate Based Fee VAT" ), reader .get ("Settlement Currency" )));
250-
250+
251251 result .add (record );
252-
252+
253253 }
254254 }
255255 reader .close ();
256-
257-
256+
258257 return result ;
259258 }
260259 catch (Exception e )
261260 {
262261 throw new PensioAPIException (e );
263262 }
264263 }
265-
266- public APIResponse release (ReleaseReservationRequest request ) throws PensioAPIException
264+
265+ public APIResponse release (ReleaseReservationRequest request ) throws PensioAPIException
267266 {
268- HashMap <String , String > params = new HashMap <String , String >();
267+ HashMap <String , String > params = new HashMap <>();
269268 addParam (params , "transaction_id" , request .getPaymentId ());
270-
269+
271270 return getAPIResponse ("releaseReservation" , HttpMethod .POST , params );
272271 }
273-
274- public APIResponse queryGiftCard (PaymentReservationRequest request ) throws PensioAPIException
272+
273+ public APIResponse queryGiftCard (PaymentReservationRequest request ) throws PensioAPIException
275274 {
276- HashMap <String , String > params = new HashMap <String , String >();
275+ HashMap <String , String > params = new HashMap <>();
277276 if (request .getGiftCard () == null ){
278277 addParam (params , "terminal" , request .getTerminal ());
279278 addParam (params , "giftcard[token]" , request .getGiftCardToken ());
@@ -285,22 +284,22 @@ public APIResponse queryGiftCard(PaymentReservationRequest request) throws Pensi
285284
286285 return getAPIResponse ("queryGiftCard" , HttpMethod .POST , params );
287286 }
288-
287+
289288 private void setCreditCardRequestParameters (
290289 PaymentReservationRequest request , HashMap <String , String > params )
291290 {
292291 if (request .getCreditCard () == null )
293292 {
294293 return ;
295294 }
296-
295+
297296 addParam (params , "credit_card_token" , request .getCreditCard ().getToken ());
298297 addParam (params , "cardnum" , request .getCreditCard ().getCardNumber ());
299298 addParam (params , "emonth" , request .getCreditCard ().getExpiryMonth ());
300299 addParam (params , "eyear" , request .getCreditCard ().getExpiryYear ());
301-
300+
302301 addParam (params , "cvc" , request .getCreditCard ().getCvc ());
303-
302+
304303 }
305304
306305 protected void setPaymentRequestParameters (
@@ -434,8 +433,7 @@ protected APIResponse getAPIResponse(String method,
434433 {
435434 throw new PensioAPIException (response .getHeader ());
436435 }
437-
438-
436+
439437 return response ;
440438 }
441439 catch (Exception e )
@@ -448,7 +446,7 @@ public PaymentRequestResponse createMultiPaymentRequest(MultiPaymentRequestParen
448446 {
449447 try
450448 {
451- HashMap <String , String > params = new HashMap <String , String >();
449+ HashMap <String , String > params = new HashMap <>();
452450 setMultiPaymentRequestParameters (multiPaymentRequest , params );
453451
454452 APIResponse response = getAPIResponse ("createMultiPaymentRequest" , HttpMethod .POST , params );
@@ -485,7 +483,7 @@ public APIResponse parsePostBackXMLParameter(String xmlParameter) throws PensioA
485483
486484 public APIResponse transactions (TransactionsRequest request ) throws PensioAPIException
487485 {
488- HashMap <String , String > params = new HashMap <String , String >();
486+ HashMap <String , String > params = new HashMap <>();
489487 addParam (params , "transaction_id" , request .getPaymentId ());
490488
491489 return getAPIResponse ("payments" , HttpMethod .GET , params );
@@ -672,7 +670,7 @@ public APIResponse cardWalletSession(CardWalletSessionRequest request) throws Pe
672670
673671 public APIResponse cardWalletAuthorize (CardWalletAuthorizeRequest request ) throws PensioAPIException
674672 {
675- HashMap <String , String > params = new HashMap <String , String >();
673+ HashMap <String , String > params = new HashMap <>();
676674 addParam (params , "provider_data" , request .getProviderData ());
677675 addParam (params , "sale_reconciliation_identifier" , request .getSaleReconciliationIdentifier ());
678676 addParam (params , "sale_invoice_number" , request .getSaleInvoiceNumber ());
@@ -683,6 +681,47 @@ public APIResponse cardWalletAuthorize(CardWalletAuthorizeRequest request) throw
683681 return getAPIResponse ("cardWallet/authorize" , HttpMethod .POST , params );
684682 }
685683
684+ public CheckoutSessionResponse createCheckoutSession (CreateCheckoutSessionRequest createCheckoutSessionRequest ) throws PensioAPIException
685+ {
686+ String apiUrl = "checkoutSession" ;
687+
688+ HashMap <String , String > params = new HashMap <>();
689+ if (createCheckoutSessionRequest != null && createCheckoutSessionRequest .getSessionId () != null ) {
690+ addParam (params , "session_id" , createCheckoutSessionRequest .getSessionId ());
691+ }
692+
693+ return checkoutSession (apiUrl , params );
694+ }
695+
696+ public CheckoutSessionResponse updateCheckoutSession (UpdateCheckoutSessionRequest updateCheckoutSessionRequest ) throws PensioAPIException
697+ {
698+ if (updateCheckoutSessionRequest == null || updateCheckoutSessionRequest .sessionId () == null ) {
699+ throw new PensioAPIException ("updateCheckoutSessionRequest.sessionId cannot be null for updateCheckoutSession" );
700+ }
701+ if (updateCheckoutSessionRequest .status () == null ) {
702+ throw new PensioAPIException ("updateCheckoutSessionRequest.sessionStatus cannot be null for updateCheckoutSession" );
703+ }
704+
705+ String apiUrl = "checkoutSession/" + updateCheckoutSessionRequest .sessionId ();
706+
707+ HashMap <String , String > params = new HashMap <>();
708+ addParam (params , "session_status" , updateCheckoutSessionRequest .status ().name ());
709+
710+ return checkoutSession (apiUrl , params );
711+ }
712+
713+ private CheckoutSessionResponse checkoutSession (String apiUrl , HashMap <String , String > params ) throws PensioAPIException {
714+ APIResponse response = getAPIResponse (apiUrl , HttpMethod .POST , params );
715+
716+ CheckoutSessionResponse checkoutSessionResponse = new CheckoutSessionResponse ();
717+ if (response .getBody ().getSession () != null ) {
718+ Session session = response .getBody ().getSession ();
719+ checkoutSessionResponse .setSessionId (session .getId ());
720+ checkoutSessionResponse .setSessionStatus (SessionStatus .valueOf (session .getStatus ()));
721+ }
722+ return checkoutSessionResponse ;
723+ }
724+
686725 protected String getAppAPIPath ()
687726 {
688727 return "merchant/API/" ;
0 commit comments