1
+ use std:: str:: FromStr ;
2
+
1
3
use common_enums:: enums:: CaptureMethod ;
2
4
use common_utils:: types:: MinorUnit ;
5
+ use error_stack:: { report, ResultExt } ;
3
6
use hyperswitch_domain_models:: {
4
7
payment_method_data:: PaymentMethodData ,
5
8
router_data:: { AccessToken , ConnectorAuthType , RouterData } ,
@@ -12,7 +15,7 @@ use hyperswitch_domain_models::{
12
15
} ,
13
16
} ;
14
17
use hyperswitch_interfaces:: errors;
15
- use masking:: Secret ;
18
+ use masking:: { PeekInterface , Secret } ;
16
19
use serde:: { Deserialize , Serialize } ;
17
20
18
21
use crate :: {
@@ -102,8 +105,8 @@ pub struct JpmorganPaymentMethodType {
102
105
#[ derive( Default , Debug , Serialize , Deserialize ) ]
103
106
#[ serde( rename_all = "camelCase" ) ]
104
107
pub struct Expiry {
105
- month : Secret < String > ,
106
- year : Secret < String > ,
108
+ month : Secret < i32 > ,
109
+ year : Secret < i32 > ,
107
110
}
108
111
109
112
#[ derive( Serialize , Debug , Default , Deserialize ) ]
@@ -159,8 +162,15 @@ impl TryFrom<&JpmorganRouterData<&PaymentsAuthorizeRouterData>> for JpmorganPaym
159
162
let merchant = JpmorganMerchant { merchant_software } ;
160
163
161
164
let expiry: Expiry = Expiry {
162
- month : req_card. card_exp_month . clone ( ) ,
163
- year : req_card. get_expiry_year_4_digit ( ) ,
165
+ month : Secret :: new (
166
+ req_card
167
+ . card_exp_month
168
+ . peek ( )
169
+ . clone ( )
170
+ . parse :: < i32 > ( )
171
+ . change_context ( errors:: ConnectorError :: RequestEncodingFailed ) ?,
172
+ ) ,
173
+ year : req_card. get_expiry_year_as_4_digit_i32 ( ) ?,
164
174
} ;
165
175
166
176
let account_number = Secret :: new ( req_card. card_number . to_string ( ) ) ;
@@ -637,12 +647,47 @@ impl TryFrom<RefundsResponseRouterData<RSync, JpmorganRefundSyncResponse>>
637
647
}
638
648
}
639
649
650
+ #[ derive( Debug , Serialize , Deserialize ) ]
651
+ #[ serde( rename_all = "SCREAMING_SNAKE_CASE" ) ]
652
+ pub enum ReversalReason {
653
+ NoResponse ,
654
+ LateResponse ,
655
+ UnableToDeliver ,
656
+ CardDeclined ,
657
+ MacNotVerified ,
658
+ MacSyncError ,
659
+ ZekSyncError ,
660
+ SystemMalfunction ,
661
+ SuspectedFraud ,
662
+ }
663
+
664
+ impl FromStr for ReversalReason {
665
+ type Err = error_stack:: Report < errors:: ConnectorError > ;
666
+
667
+ fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
668
+ match s. to_uppercase ( ) . as_str ( ) {
669
+ "NO_RESPONSE" => Ok ( Self :: NoResponse ) ,
670
+ "LATE_RESPONSE" => Ok ( Self :: LateResponse ) ,
671
+ "UNABLE_TO_DELIVER" => Ok ( Self :: UnableToDeliver ) ,
672
+ "CARD_DECLINED" => Ok ( Self :: CardDeclined ) ,
673
+ "MAC_NOT_VERIFIED" => Ok ( Self :: MacNotVerified ) ,
674
+ "MAC_SYNC_ERROR" => Ok ( Self :: MacSyncError ) ,
675
+ "ZEK_SYNC_ERROR" => Ok ( Self :: ZekSyncError ) ,
676
+ "SYSTEM_MALFUNCTION" => Ok ( Self :: SystemMalfunction ) ,
677
+ "SUSPECTED_FRAUD" => Ok ( Self :: SuspectedFraud ) ,
678
+ _ => Err ( report ! ( errors:: ConnectorError :: InvalidDataFormat {
679
+ field_name: "cancellation_reason" ,
680
+ } ) ) ,
681
+ }
682
+ }
683
+ }
684
+
640
685
#[ derive( Debug , Serialize , Deserialize ) ]
641
686
#[ serde( rename_all = "camelCase" ) ]
642
687
pub struct JpmorganCancelRequest {
643
688
pub amount : Option < i64 > ,
644
689
pub is_void : Option < bool > ,
645
- pub reversal_reason : Option < String > ,
690
+ pub reversal_reason : Option < ReversalReason > ,
646
691
}
647
692
648
693
impl TryFrom < JpmorganRouterData < & PaymentsCancelRouterData > > for JpmorganCancelRequest {
@@ -651,7 +696,13 @@ impl TryFrom<JpmorganRouterData<&PaymentsCancelRouterData>> for JpmorganCancelRe
651
696
Ok ( Self {
652
697
amount : item. router_data . request . amount ,
653
698
is_void : Some ( true ) ,
654
- reversal_reason : item. router_data . request . cancellation_reason . clone ( ) ,
699
+ reversal_reason : item
700
+ . router_data
701
+ . request
702
+ . cancellation_reason
703
+ . as_ref ( )
704
+ . map ( |reason| ReversalReason :: from_str ( reason) )
705
+ . transpose ( ) ?,
655
706
} )
656
707
}
657
708
}
0 commit comments