|
9 | 9 | use WooCommerce\PayPalCommerce\ApiClient\Entity\Address; |
10 | 10 | use WooCommerce\PayPalCommerce\ApiClient\Entity\Capture; |
11 | 11 | use WooCommerce\PayPalCommerce\ApiClient\Entity\CaptureStatus; |
| 12 | +use WooCommerce\PayPalCommerce\ApiClient\Entity\FraudProcessorResponse; |
12 | 13 | use WooCommerce\PayPalCommerce\ApiClient\Entity\ExperienceContext; |
13 | 14 | use WooCommerce\PayPalCommerce\ApiClient\Entity\Order; |
14 | 15 | use WooCommerce\PayPalCommerce\ApiClient\Entity\OrderStatus; |
@@ -1202,5 +1203,187 @@ function ($url, $args) use ($rawResponse, $host) { |
1202 | 1203 | $payer->expects('to_array')->andReturn(['payer']); |
1203 | 1204 | $testee->create([$purchaseUnit], ExperienceContext::SHIPPING_PREFERENCE_GET_FROM_FILE, $payer); |
1204 | 1205 | } |
| 1206 | + |
| 1207 | + public function testCaptureDeclinedWithFraudResponseCodeThrowsDetailedMessage(): void |
| 1208 | + { |
| 1209 | + expect('wp_json_encode')->andReturnUsing('json_encode'); |
| 1210 | + $orderId = 'id'; |
| 1211 | + $orderToCaptureStatus = Mockery::mock(OrderStatus::class); |
| 1212 | + $orderToCaptureStatus->expects('is')->with('COMPLETED')->andReturn(false); |
| 1213 | + $orderToCapture = Mockery::mock(Order::class); |
| 1214 | + $orderToCapture->expects('status')->andReturn($orderToCaptureStatus); |
| 1215 | + $orderToCapture->expects('id')->andReturn($orderId); |
| 1216 | + $headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class); |
| 1217 | + $headers->shouldReceive('getAll'); |
| 1218 | + $rawResponse = [ |
| 1219 | + 'body' => '{"id":"order123"}', |
| 1220 | + 'headers' => $headers, |
| 1221 | + ]; |
| 1222 | + $expectedOrder = Mockery::mock(Order::class); |
| 1223 | + $host = 'https://example.com/'; |
| 1224 | + $token = Mockery::mock(Token::class); |
| 1225 | + $token->expects('token')->andReturn('bearer'); |
| 1226 | + $bearer = Mockery::mock(Bearer::class); |
| 1227 | + $bearer->expects('bearer')->andReturn($token); |
| 1228 | + $orderFactory = Mockery::mock(OrderFactory::class); |
| 1229 | + $orderFactory->expects('from_paypal_response')->andReturn($expectedOrder); |
| 1230 | + $patchCollectionFactory = Mockery::mock(PatchCollectionFactory::class); |
| 1231 | + $logger = Mockery::mock(LoggerInterface::class); |
| 1232 | + $logger->shouldNotReceive('warning'); |
| 1233 | + $logger->shouldReceive('debug'); |
| 1234 | + $subscription_helper = Mockery::mock(SubscriptionHelper::class); |
| 1235 | + $fraudnet = Mockery::mock(FraudNet::class); |
| 1236 | + |
| 1237 | + $testee = new OrderEndpoint( |
| 1238 | + $host, |
| 1239 | + $bearer, |
| 1240 | + $orderFactory, |
| 1241 | + $patchCollectionFactory, |
| 1242 | + 'CAPTURE', |
| 1243 | + $logger, |
| 1244 | + $subscription_helper, |
| 1245 | + false, |
| 1246 | + $fraudnet |
| 1247 | + ); |
| 1248 | + |
| 1249 | + expect('wp_remote_get')->andReturn($rawResponse); |
| 1250 | + expect('is_wp_error')->with($rawResponse)->andReturn(false); |
| 1251 | + expect('wp_remote_retrieve_response_code')->with($rawResponse)->andReturn(201); |
| 1252 | + |
| 1253 | + $fraud = new FraudProcessorResponse(null, null, '9500'); |
| 1254 | + $purchaseUnit = Mockery::mock(PurchaseUnit::class); |
| 1255 | + $payment = Mockery::mock(Payments::class); |
| 1256 | + $capture = Mockery::mock(Capture::class); |
| 1257 | + $expectedOrder->shouldReceive('purchase_units')->once()->andReturn([$purchaseUnit]); |
| 1258 | + $purchaseUnit->shouldReceive('payments')->once()->andReturn($payment); |
| 1259 | + $payment->shouldReceive('captures')->once()->andReturn([$capture]); |
| 1260 | + $capture->shouldReceive('status')->once()->andReturn(new CaptureStatus(CaptureStatus::DECLINED)); |
| 1261 | + $capture->shouldReceive('fraud_processor_response')->once()->andReturn($fraud); |
| 1262 | + |
| 1263 | + $this->expectException(RuntimeException::class); |
| 1264 | + $this->expectExceptionMessageMatches('/9500: Suspected Fraud/'); |
| 1265 | + $testee->capture($orderToCapture); |
| 1266 | + } |
| 1267 | + |
| 1268 | + public function testCaptureDeclinedWithNullFraudThrowsGenericMessage(): void |
| 1269 | + { |
| 1270 | + expect('wp_json_encode')->andReturnUsing('json_encode'); |
| 1271 | + $orderId = 'id'; |
| 1272 | + $orderToCaptureStatus = Mockery::mock(OrderStatus::class); |
| 1273 | + $orderToCaptureStatus->expects('is')->with('COMPLETED')->andReturn(false); |
| 1274 | + $orderToCapture = Mockery::mock(Order::class); |
| 1275 | + $orderToCapture->expects('status')->andReturn($orderToCaptureStatus); |
| 1276 | + $orderToCapture->expects('id')->andReturn($orderId); |
| 1277 | + $headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class); |
| 1278 | + $headers->shouldReceive('getAll'); |
| 1279 | + $rawResponse = [ |
| 1280 | + 'body' => '{"id":"order123"}', |
| 1281 | + 'headers' => $headers, |
| 1282 | + ]; |
| 1283 | + $expectedOrder = Mockery::mock(Order::class); |
| 1284 | + $host = 'https://example.com/'; |
| 1285 | + $token = Mockery::mock(Token::class); |
| 1286 | + $token->expects('token')->andReturn('bearer'); |
| 1287 | + $bearer = Mockery::mock(Bearer::class); |
| 1288 | + $bearer->expects('bearer')->andReturn($token); |
| 1289 | + $orderFactory = Mockery::mock(OrderFactory::class); |
| 1290 | + $orderFactory->expects('from_paypal_response')->andReturn($expectedOrder); |
| 1291 | + $patchCollectionFactory = Mockery::mock(PatchCollectionFactory::class); |
| 1292 | + $logger = Mockery::mock(LoggerInterface::class); |
| 1293 | + $logger->shouldNotReceive('warning'); |
| 1294 | + $logger->shouldReceive('debug'); |
| 1295 | + $subscription_helper = Mockery::mock(SubscriptionHelper::class); |
| 1296 | + $fraudnet = Mockery::mock(FraudNet::class); |
| 1297 | + |
| 1298 | + $testee = new OrderEndpoint( |
| 1299 | + $host, |
| 1300 | + $bearer, |
| 1301 | + $orderFactory, |
| 1302 | + $patchCollectionFactory, |
| 1303 | + 'CAPTURE', |
| 1304 | + $logger, |
| 1305 | + $subscription_helper, |
| 1306 | + false, |
| 1307 | + $fraudnet |
| 1308 | + ); |
| 1309 | + |
| 1310 | + expect('wp_remote_get')->andReturn($rawResponse); |
| 1311 | + expect('is_wp_error')->with($rawResponse)->andReturn(false); |
| 1312 | + expect('wp_remote_retrieve_response_code')->with($rawResponse)->andReturn(201); |
| 1313 | + |
| 1314 | + $purchaseUnit = Mockery::mock(PurchaseUnit::class); |
| 1315 | + $payment = Mockery::mock(Payments::class); |
| 1316 | + $capture = Mockery::mock(Capture::class); |
| 1317 | + $expectedOrder->shouldReceive('purchase_units')->once()->andReturn([$purchaseUnit]); |
| 1318 | + $purchaseUnit->shouldReceive('payments')->once()->andReturn($payment); |
| 1319 | + $payment->shouldReceive('captures')->once()->andReturn([$capture]); |
| 1320 | + $capture->shouldReceive('status')->once()->andReturn(new CaptureStatus(CaptureStatus::DECLINED)); |
| 1321 | + $capture->shouldReceive('fraud_processor_response')->once()->andReturn(null); |
| 1322 | + |
| 1323 | + $this->expectException(RuntimeException::class); |
| 1324 | + $this->expectExceptionMessage('Payment provider declined the payment, please use a different payment method.'); |
| 1325 | + $testee->capture($orderToCapture); |
| 1326 | + } |
| 1327 | + |
| 1328 | + public function testCaptureDeclinedWithEmptyResponseCodeThrowsGenericMessage(): void |
| 1329 | + { |
| 1330 | + expect('wp_json_encode')->andReturnUsing('json_encode'); |
| 1331 | + $orderId = 'id'; |
| 1332 | + $orderToCaptureStatus = Mockery::mock(OrderStatus::class); |
| 1333 | + $orderToCaptureStatus->expects('is')->with('COMPLETED')->andReturn(false); |
| 1334 | + $orderToCapture = Mockery::mock(Order::class); |
| 1335 | + $orderToCapture->expects('status')->andReturn($orderToCaptureStatus); |
| 1336 | + $orderToCapture->expects('id')->andReturn($orderId); |
| 1337 | + $headers = Mockery::mock(Requests_Utility_CaseInsensitiveDictionary::class); |
| 1338 | + $headers->shouldReceive('getAll'); |
| 1339 | + $rawResponse = [ |
| 1340 | + 'body' => '{"id":"order123"}', |
| 1341 | + 'headers' => $headers, |
| 1342 | + ]; |
| 1343 | + $expectedOrder = Mockery::mock(Order::class); |
| 1344 | + $host = 'https://example.com/'; |
| 1345 | + $token = Mockery::mock(Token::class); |
| 1346 | + $token->expects('token')->andReturn('bearer'); |
| 1347 | + $bearer = Mockery::mock(Bearer::class); |
| 1348 | + $bearer->expects('bearer')->andReturn($token); |
| 1349 | + $orderFactory = Mockery::mock(OrderFactory::class); |
| 1350 | + $orderFactory->expects('from_paypal_response')->andReturn($expectedOrder); |
| 1351 | + $patchCollectionFactory = Mockery::mock(PatchCollectionFactory::class); |
| 1352 | + $logger = Mockery::mock(LoggerInterface::class); |
| 1353 | + $logger->shouldNotReceive('warning'); |
| 1354 | + $logger->shouldReceive('debug'); |
| 1355 | + $subscription_helper = Mockery::mock(SubscriptionHelper::class); |
| 1356 | + $fraudnet = Mockery::mock(FraudNet::class); |
| 1357 | + |
| 1358 | + $testee = new OrderEndpoint( |
| 1359 | + $host, |
| 1360 | + $bearer, |
| 1361 | + $orderFactory, |
| 1362 | + $patchCollectionFactory, |
| 1363 | + 'CAPTURE', |
| 1364 | + $logger, |
| 1365 | + $subscription_helper, |
| 1366 | + false, |
| 1367 | + $fraudnet |
| 1368 | + ); |
| 1369 | + |
| 1370 | + expect('wp_remote_get')->andReturn($rawResponse); |
| 1371 | + expect('is_wp_error')->with($rawResponse)->andReturn(false); |
| 1372 | + expect('wp_remote_retrieve_response_code')->with($rawResponse)->andReturn(201); |
| 1373 | + |
| 1374 | + $fraud = new FraudProcessorResponse(null, null, null); |
| 1375 | + $purchaseUnit = Mockery::mock(PurchaseUnit::class); |
| 1376 | + $payment = Mockery::mock(Payments::class); |
| 1377 | + $capture = Mockery::mock(Capture::class); |
| 1378 | + $expectedOrder->shouldReceive('purchase_units')->once()->andReturn([$purchaseUnit]); |
| 1379 | + $purchaseUnit->shouldReceive('payments')->once()->andReturn($payment); |
| 1380 | + $payment->shouldReceive('captures')->once()->andReturn([$capture]); |
| 1381 | + $capture->shouldReceive('status')->once()->andReturn(new CaptureStatus(CaptureStatus::DECLINED)); |
| 1382 | + $capture->shouldReceive('fraud_processor_response')->once()->andReturn($fraud); |
| 1383 | + |
| 1384 | + $this->expectException(RuntimeException::class); |
| 1385 | + $this->expectExceptionMessage('Payment provider declined the payment, please use a different payment method.'); |
| 1386 | + $testee->capture($orderToCapture); |
| 1387 | + } |
1205 | 1388 | } |
1206 | 1389 |
|
0 commit comments