33
44require_once dirname (__DIR__ ).'/test_helper.php ' ;
55
6+ use \Mockery as m ;
7+
68class PaymentNotificationTest extends HyperchargeTestCase {
79
10+ function setUp () {
11+ Config::setIdSeparator (false );
12+ XmlSerializer::$ sort = false ;
13+
14+ $ this ->curl = $ curl = m::mock ('Curl ' );
15+ $ factory = m::mock (new Factory ());
16+ $ factory
17+ ->shouldReceive ('createHttpsClient ' )
18+ ->with ('the user ' , 'the passw ' )
19+ ->andReturn ($ curl );
20+
21+ // Scheduler calls without params - so defaults are used
22+ $ factory
23+ ->shouldReceive ('createHttpsClient ' )
24+ ->andReturn ($ curl );
25+
26+ Config::setFactory ($ factory );
27+ Config::set ('the user ' , 'the passw ' , Config::ENV_SANDBOX );
28+ }
29+
30+ function tearDown () {
31+ m::close ();
32+ Config::setFactory (new Factory );
33+ }
34+
835 /**
936 * example 1 from Hypercharge API doc
1037 * 4.2 Notifications -> "Notification signature examples"
11- * unique_id : 26aa150ee68b1b2d6758a0e6c44fce4c
12- * api passord : b5af4c9cf497662e00b78550fd87e65eb415f42f
13- * signature : 3d82fef85cb60...
38+ * unique_id : 26aa150ee68b1b2d6758a0e6c44fce4c
39+ * api password : b5af4c9cf497662e00b78550fd87e65eb415f42f
40+ * signature : 3d82fef85cb60...
1441 */
1542 function testVerifyValidSignatureBogus () {
1643 $ postData = $ this ->schemaNotification ('payment_notification_with_transaction.json ' );
@@ -23,16 +50,16 @@ function testVerifyValidSignatureBogus() {
2350 /**
2451 * example 2 from Hypercharge API doc
2552 * 6.2.3 Notification -> "Notification signature examples"
26- * unique_id : 3f760162ef57...
27- * api passord : 50fd87e65eb4...
28- * signature : 14519d0db2f7...
53+ * unique_id : 3f760162ef57...
54+ * api password : 50fd87e65eb4...
55+ * signature : 14519d0db2f7...
2956 */
3057 function testVerifyValidSignatureTest123 () {
3158 $ postData = $ this ->schemaNotification ('payment_notification_with_transaction.json ' );
3259 $ postData ['payment_unique_id ' ] = '3f760162ef57a829011e5e2379b3fa17 ' ;
3360 $ postData ['signature ' ] = '14519d0db2f7f8f407efccc9b099c5303f55c0262e3b9132e5bcc97f7febf5f9ab19df03929c1ead271be79807b4086321a023743d2b6b1278c2082b61cf3ff0 ' ;
3461 $ notification = new PaymentNotification ($ postData );
35- $ this ->assertEqual ($ notification ->getPayment ()-> unique_id , '3f760162ef57a829011e5e2379b3fa17 ' );
62+ $ this ->assertEqual ($ notification ->payment_unique_id , '3f760162ef57a829011e5e2379b3fa17 ' );
3663 $ apiPassword = '50fd87e65eb415f42fb5af4c9cf497662e00b785 ' ;
3764 $ notification ->verify ($ apiPassword );
3865 $ this ->assertTrue ($ notification ->isVerified ());
@@ -91,57 +118,119 @@ function testIsApprovedWithStatusCanceled() {
91118
92119 function testGetXWithTransaction () {
93120 $ postData = $ this ->schemaNotification ('payment_notification_with_transaction.json ' );
121+ $ paymentId = $ postData ['payment_unique_id ' ];
122+ $ postData ['payment_transaction_unique_id ' ] = '5e2cbbad71d2b1343232abc3c208223a ' ;
123+ $ channelToken = $ postData ['payment_transaction_channel_token ' ];
94124 $ notification = new PaymentNotification ($ postData );
125+ $ notification ->verify ('b5af4c9cf497662e00b78550fd87e65eb415f42f ' );
126+ $ this ->assertTrue ($ notification ->isVerified ());
127+
128+ $ request = $ this ->schemaRequest ('reconcile.xml ' );
129+ $ response = $ this ->schemaResponse ('WpfPayment_find.xml ' );
130+ $ request = preg_replace ('/<unique_id>[0-9a-f]+<\/unique_id>/ ' , "<unique_id> $ paymentId</unique_id> " , $ request );
131+ $ response = preg_replace ('/<unique_id>[0-9a-f]+<\/unique_id>/ ' , "<unique_id> $ paymentId</unique_id> " , $ response );
132+
133+ $ this ->curl
134+ ->shouldReceive ('xmlPost ' )
135+ ->with ('https://testpayment.hypercharge.net/payment/reconcile ' , $ request )
136+ ->andReturn ($ response );
95137
96138 $ payment = $ notification ->getPayment ();
97- $ this ->assertIsA ($ payment , 'stdClass ' );
98- $ this ->assertEqual ($ payment ->type , 'WpfPayment ' );
99- $ this ->assertEqual ($ payment ->unique_id , '26aa150ee68b1b2d6758a0e6c44fce4c ' );
139+ $ this ->assertIsA ($ payment , 'Hypercharge\Payment ' );
140+ $ this ->assertEqual ($ payment ->unique_id , $ paymentId );
100141 $ this ->assertEqual ($ payment ->status , 'approved ' );
101- $ this ->assertEqual ($ payment ->transaction_id , 'the-id-provided-by-merchant-aadfasdfadf ' );
142+ $ this ->assertEqual ($ payment ->transaction_id , '0AF671AF-4134-4BE7-BDF0-26E38B74106E---d8981080a4f701303cf4542696cde09d ' );
102143
103144 $ this ->assertTrue ($ notification ->hasTransaction ());
145+
146+ $ request = $ this ->schemaRequest ('reconcile.xml ' );
147+ $ response = $ this ->schemaResponse ('sale.xml ' );
148+ $ request = preg_replace ('/<unique_id>[0-9a-f]+<\/unique_id>/ ' , '<unique_id>5e2cbbad71d2b1343232abc3c208223a</unique_id> ' , $ request );
149+ $ this ->curl
150+ ->shouldReceive ('xmlPost ' )
151+ ->with ('https://test.hypercharge.net/reconcile/ ' .$ channelToken , $ request )
152+ ->andReturn ($ response );
153+
104154 $ trx = $ notification ->getTransaction ();
105- $ this ->assertIsA ($ trx , 'stdClass ' );
106- $ this ->assertEqual ($ trx ->transaction_type , 'sale ' );
107- $ this ->assertEqual ($ trx ->unique_id , 'bad08183a9ec545daf0f24c48361aa10 ' );
108- $ this ->assertEqual ($ trx ->channel_token , 'the-channel-token-o234uouizeiz2492834792 ' );
155+ $ this ->assertIsA ($ trx , 'Hypercharge\Transaction ' );
156+ $ this ->assertEqual ($ trx ->getType (), 'sale ' );
157+ $ this ->assertTrue ($ trx ->isApproved ());
158+ $ this ->assertEqual ($ trx ->amount , 5000 );
159+ $ this ->assertEqual ($ trx ->currency , 'USD ' );
160+ $ this ->assertEqual ($ trx ->unique_id , '5e2cbbad71d2b1343232abc3c208223a ' );
109161
110162 $ this ->assertFalse ($ notification ->hasSchedule ());
111163 $ this ->assertEqual ($ notification ->getSchedule (), null );
112164 }
113165
114166 function testGetXWithSchedule () {
115167 $ postData = $ this ->schemaNotification ('payment_notification_with_schedule.json ' );
168+ $ paymentId = $ postData ['payment_unique_id ' ];
169+ $ scheduleId = $ postData ['schedule_unique_id ' ];
116170 $ notification = new PaymentNotification ($ postData );
171+ $ notification ->verify ('b5af4c9cf497662e00b78550fd87e65eb415f42f ' );
172+ $ this ->assertTrue ($ notification ->isVerified ());
173+
174+
175+ $ request = $ this ->schemaRequest ('reconcile.xml ' );
176+ $ response = $ this ->schemaResponse ('WpfPayment_find.xml ' );
177+ $ request = preg_replace ('/<unique_id>[0-9a-f]+<\/unique_id>/ ' , "<unique_id> $ paymentId</unique_id> " , $ request );
178+ $ response = preg_replace ('/<unique_id>[0-9a-f]+<\/unique_id>/ ' , "<unique_id> $ paymentId</unique_id> " , $ response );
179+
180+ $ this ->curl
181+ ->shouldReceive ('xmlPost ' )
182+ ->with ('https://testpayment.hypercharge.net/payment/reconcile ' , $ request )
183+ ->andReturn ($ response );
117184
118185 $ payment = $ notification ->getPayment ();
119- $ this ->assertIsA ($ payment , 'stdClass ' );
120- $ this ->assertEqual ($ payment ->type , 'WpfPayment ' );
121- $ this ->assertEqual ($ payment ->unique_id , '26aa150ee68b1b2d6758a0e6c44fce4c ' );
186+ $ this ->assertIsA ($ payment , 'Hypercharge\Payment ' );
187+ $ this ->assertEqual ($ payment ->unique_id , $ paymentId );
122188 $ this ->assertEqual ($ payment ->status , 'approved ' );
123- $ this ->assertEqual ($ payment ->transaction_id , 'the-id-provided-by-merchant-aadfasdfadf ' );
189+ $ this ->assertEqual ($ payment ->transaction_id , '0AF671AF-4134-4BE7-BDF0-26E38B74106E---d8981080a4f701303cf4542696cde09d ' );
124190
125191 $ this ->assertFalse ($ notification ->hasTransaction ());
126192 $ this ->assertEqual ($ notification ->getTransaction (), null );
127193
128194 $ this ->assertTrue ($ notification ->hasSchedule ());
195+
196+ $ response = $ this ->schemaResponse ('scheduler.json ' );
197+ $ response ['unique_id ' ] = $ scheduleId ;
198+ $ this ->expect_Curl_jsonRequest ()
199+ ->with ('GET ' , 'https://test.hypercharge.net/v2/scheduler/ ' .$ scheduleId )
200+ ->andReturn ($ response );
201+
129202 $ schedule = $ notification ->getSchedule ();
130- $ this ->assertIsA ($ schedule , 'stdClass ' );
131- $ this ->assertEqual ($ schedule ->unique_id , 'bad08183a9ec545daf0f24c48361aa10 ' );
132- $ this ->assertEqual ($ schedule ->end_date , '2013-01-21 00:00:00 ' );
203+ $ this ->assertIsA ($ schedule , 'Hypercharge\Scheduler ' );
204+ $ this ->assertEqual ($ schedule ->type , 'DateRecurringSchedule ' );
205+ $ this ->assertEqual ($ schedule ->unique_id , $ scheduleId );
206+ $ this ->assertEqual ($ schedule ->start_date , '2013-11-13 ' );
207+ $ this ->assertEqual ($ schedule ->end_date , '2014-06-30 ' );
208+ $ this ->assertEqual ($ schedule ->interval , 'monthly ' );
133209 }
134210
135- function testGetX () {
211+ function testGetXPaymentOnly () {
136212 $ postData = $ this ->schemaNotification ('payment_notification.json ' );
213+ $ paymentId = $ postData ['payment_unique_id ' ];
214+
137215 $ notification = new PaymentNotification ($ postData );
216+ $ notification ->verify ('b5af4c9cf497662e00b78550fd87e65eb415f42f ' );
217+ $ this ->assertTrue ($ notification ->isVerified ());
218+
219+ $ request = $ this ->schemaRequest ('reconcile.xml ' );
220+ $ response = $ this ->schemaResponse ('WpfPayment_find.xml ' );
221+ $ request = preg_replace ('/<unique_id>[0-9a-f]+<\/unique_id>/ ' , "<unique_id> $ paymentId</unique_id> " , $ request );
222+ $ response = preg_replace ('/<unique_id>[0-9a-f]+<\/unique_id>/ ' , "<unique_id> $ paymentId</unique_id> " , $ response );
223+
224+ $ this ->curl
225+ ->shouldReceive ('xmlPost ' )
226+ ->with ('https://testpayment.hypercharge.net/payment/reconcile ' , $ request )
227+ ->andReturn ($ response );
138228
139229 $ payment = $ notification ->getPayment ();
140- $ this ->assertIsA ($ payment , 'stdClass ' );
141- $ this ->assertEqual ($ payment ->type , 'WpfPayment ' );
142- $ this ->assertEqual ($ payment ->unique_id , '26aa150ee68b1b2d6758a0e6c44fce4c ' );
143- $ this ->assertEqual ($ payment ->status , 'canceled ' );
144- $ this ->assertEqual ($ payment ->transaction_id , 'the-id-provided-by-merchant-aadfasdfadf ' );
230+ $ this ->assertIsA ($ payment , 'Hypercharge\Payment ' );
231+ $ this ->assertEqual ($ payment ->unique_id , $ paymentId );
232+ $ this ->assertEqual ($ payment ->status , 'approved ' );
233+ $ this ->assertEqual ($ payment ->transaction_id , '0AF671AF-4134-4BE7-BDF0-26E38B74106E---d8981080a4f701303cf4542696cde09d ' );
145234
146235 $ this ->assertFalse ($ notification ->hasTransaction ());
147236 $ this ->assertEqual ($ notification ->getTransaction (), null );
0 commit comments