55use Omnipay \Common \Message \NotificationInterface ;
66use Omnipay \NewebPay \Message \AcceptNotificationRequest ;
77use Omnipay \Tests \TestCase ;
8+ use Symfony \Component \HttpFoundation \Request ;
89
910class AcceptNotificationRequestTest extends TestCase
1011{
12+ private $ hashKey = 'Fs5cX1TGqYM2PpdbE14a9H83YQSQF5jn ' ;
13+ private $ hashIV = 'C6AcmfqJILwgnhIP ' ;
14+ private $ merchantID = 'MS127874575 ' ;
15+
16+ /**
17+ * Encrypted period notification data for successful authorization.
18+ * Decrypted JSON: {"Status":"SUCCESS","Message":"每期授權成功","Result":{"MerchantID":"MS127874575","MerchantOrderNo":"myorder1700033460","OrderNo":"myorder1700033460_2","TradeNo":"23111515321368339","PeriodNo":"P231115153213aMDNWZ","TotalTimes":12,"AlreadyTimes":2,"AuthAmt":100,"AuthDate":"2023-12-15","NextAuthDate":"2024-01-15","AuthCode":"230297","RespondCode":"00","CardNo":"400022******1111","EscrowBank":"HNCB","AuthBank":"KGI"}}
19+ */
20+ private $ encryptedPeriod = '1c666a338762a97c40f28d5d7fe5dfa5040a0bdb8759c385421e808c79b3685bfb1133b1182a5ccb4833733ae70362b32402dc595f0d74c94a95f3ad735374e51e5c059c6361157b3540928ad55ba94f1715eb7a1316ca1aba47db6475b8214d92bcf2099020110b0a64b1f91ba662ef51fc3d869b3318a72a0f2f0aff63d525795baead177e51322949e6d9d9bc71c4f57c5eff9fc8f0479f6d0acf3d05401bd4c496399e80e4cb78583d78850f727ccf9372fd4517cf4edcb716ba3d733c52c70085455dfb822268b05b4a69fce53fd7a972258396c7c5468b87792a2f66992f153a87ab9e239a13e1cc1ac06d56106f8197101d9ff2bd5e2028ce92cb270614b11e597986c0a6ff5772885d9bc202499a11de14345de58236d17902cb149f1af206ba51280829470625294cb4b9e1550f795f64dc5976cd48553eb69fea006e89d181e080d74b739b2c9ee962dcd08ea8086fc104be4fa42d676a250b5002d03cb55172b955f882c5a9d2430e274cb3404099f8f4baf62cbed47f0464f31e90b227952309aad5a1e0530df2ccaae5a0364d25651697892ac9369a18f2553d1017515b9c0405e50eea2b30edfd1b80 ' ;
21+
22+ // 一般交易測試
1123 public function testGetData (): void
1224 {
1325 $ httpRequest = $ this ->getHttpRequest ();
@@ -20,14 +32,123 @@ public function testGetData(): void
2032 ]);
2133 $ request = new AcceptNotificationRequest ($ this ->getHttpClient (), $ httpRequest );
2234 $ request ->initialize ([
23- 'HashKey ' => ' Fs5cX1TGqYM2PpdbE14a9H83YQSQF5jn ' ,
24- 'HashIV ' => ' C6AcmfqJILwgnhIP ' ,
25- 'MerchantID ' => ' MS127874575 ' ,
35+ 'HashKey ' => $ this -> hashKey ,
36+ 'HashIV ' => $ this -> hashIV ,
37+ 'MerchantID ' => $ this -> merchantID ,
2638 'testMode ' => false ,
2739 ]);
2840
2941 self ::assertEquals ('23092714215835071 ' , $ request ->getTransactionReference ());
3042 self ::assertEquals (NotificationInterface::STATUS_COMPLETED , $ request ->getTransactionStatus ());
3143 self ::assertEquals ('授權成功 ' , $ request ->getMessage ());
3244 }
45+
46+ // 定期定額測試
47+ public function testPeriodGetTransactionStatus (): void
48+ {
49+ $ httpRequest = Request::create ('/ ' , 'POST ' , ['Period ' => $ this ->encryptedPeriod ]);
50+ $ request = new AcceptNotificationRequest ($ this ->getHttpClient (), $ httpRequest );
51+ $ request ->initialize ([
52+ 'HashKey ' => $ this ->hashKey ,
53+ 'HashIV ' => $ this ->hashIV ,
54+ 'MerchantID ' => $ this ->merchantID ,
55+ ]);
56+
57+ self ::assertEquals (NotificationInterface::STATUS_COMPLETED , $ request ->getTransactionStatus ());
58+ }
59+
60+ public function testPeriodGetTransactionReference (): void
61+ {
62+ $ httpRequest = Request::create ('/ ' , 'POST ' , ['Period ' => $ this ->encryptedPeriod ]);
63+ $ request = new AcceptNotificationRequest ($ this ->getHttpClient (), $ httpRequest );
64+ $ request ->initialize ([
65+ 'HashKey ' => $ this ->hashKey ,
66+ 'HashIV ' => $ this ->hashIV ,
67+ 'MerchantID ' => $ this ->merchantID ,
68+ ]);
69+
70+ self ::assertEquals ('23111515321368339 ' , $ request ->getTransactionReference ());
71+ }
72+
73+ public function testPeriodGetTransactionId (): void
74+ {
75+ $ httpRequest = Request::create ('/ ' , 'POST ' , ['Period ' => $ this ->encryptedPeriod ]);
76+ $ request = new AcceptNotificationRequest ($ this ->getHttpClient (), $ httpRequest );
77+ $ request ->initialize ([
78+ 'HashKey ' => $ this ->hashKey ,
79+ 'HashIV ' => $ this ->hashIV ,
80+ 'MerchantID ' => $ this ->merchantID ,
81+ ]);
82+
83+ self ::assertEquals ('myorder1700033460 ' , $ request ->getTransactionId ());
84+ }
85+
86+ public function testPeriodGetMessage (): void
87+ {
88+ $ httpRequest = Request::create ('/ ' , 'POST ' , ['Period ' => $ this ->encryptedPeriod ]);
89+ $ request = new AcceptNotificationRequest ($ this ->getHttpClient (), $ httpRequest );
90+ $ request ->initialize ([
91+ 'HashKey ' => $ this ->hashKey ,
92+ 'HashIV ' => $ this ->hashIV ,
93+ 'MerchantID ' => $ this ->merchantID ,
94+ ]);
95+
96+ self ::assertEquals ('每期授權成功 ' , $ request ->getMessage ());
97+ }
98+
99+ public function testPeriodSendReturnsAcceptNotificationResponse (): void
100+ {
101+ $ httpRequest = Request::create ('/ ' , 'POST ' , ['Period ' => $ this ->encryptedPeriod ]);
102+ $ request = new AcceptNotificationRequest ($ this ->getHttpClient (), $ httpRequest );
103+ $ request ->initialize ([
104+ 'HashKey ' => $ this ->hashKey ,
105+ 'HashIV ' => $ this ->hashIV ,
106+ 'MerchantID ' => $ this ->merchantID ,
107+ ]);
108+
109+ $ response = $ request ->send ();
110+
111+ self ::assertTrue ($ response ->isSuccessful ());
112+ self ::assertEquals ('SUCCESS ' , $ response ->getCode ());
113+ self ::assertEquals ('myorder1700033460_2 ' , $ response ->getOrderNo ());
114+ self ::assertEquals ('23111515321368339 ' , $ response ->getTransactionReference ());
115+ self ::assertEquals (12 , $ response ->getTotalTimes ());
116+ self ::assertEquals (2 , $ response ->getAlreadyTimes ());
117+ self ::assertEquals (100 , $ response ->getAuthAmt ());
118+ self ::assertEquals ('2023-12-15 ' , $ response ->getAuthDate ());
119+ self ::assertEquals ('2024-01-15 ' , $ response ->getNextAuthDate ());
120+ }
121+
122+ public function testPeriodFailedNotification (): void
123+ {
124+ // Encrypted failed notification data
125+ $ failedData = $ this ->encryptData ([
126+ 'Status ' => 'AUTH_FAILED ' ,
127+ 'Message ' => '授權失敗 ' ,
128+ 'Result ' => [
129+ 'MerchantID ' => 'MS127874575 ' ,
130+ 'MerchantOrderNo ' => 'myorder1700033460 ' ,
131+ 'OrderNo ' => 'myorder1700033460_2 ' ,
132+ 'TradeNo ' => '23111515321368339 ' ,
133+ ],
134+ ]);
135+
136+ $ httpRequest = Request::create ('/ ' , 'POST ' , ['Period ' => $ failedData ]);
137+ $ request = new AcceptNotificationRequest ($ this ->getHttpClient (), $ httpRequest );
138+ $ request ->initialize ([
139+ 'HashKey ' => $ this ->hashKey ,
140+ 'HashIV ' => $ this ->hashIV ,
141+ 'MerchantID ' => $ this ->merchantID ,
142+ ]);
143+
144+ self ::assertEquals (NotificationInterface::STATUS_FAILED , $ request ->getTransactionStatus ());
145+ self ::assertEquals ('授權失敗 ' , $ request ->getMessage ());
146+ }
147+
148+ private function encryptData (array $ data )
149+ {
150+ $ json = json_encode ($ data , JSON_UNESCAPED_UNICODE );
151+
152+ return bin2hex (openssl_encrypt ($ json , 'AES-256-CBC ' , $ this ->hashKey , OPENSSL_RAW_DATA , $ this ->hashIV ));
153+ }
33154}
0 commit comments