Skip to content

Commit 6cf47de

Browse files
authored
feature #66 Log debug ID (Zales0123)
This PR was merged into the 1.0-dev branch. Discussion ---------- It should be checked carefully, as these changes affect all the logic we have in the plugin 🚀 Commits ------- f60c4c0 Extract PayPalClient that logs debug_id from failed requests 9abfe84 Use new PayPal client in already implemented API services 22189f0 Handle PATCH requests in the new PayPal client 6b95950 CS fixes 7e3d4e1 Use tracking id from env variable
2 parents 91fcc7a + 7e3d4e1 commit 6cf47de

15 files changed

Lines changed: 438 additions & 316 deletions

spec/Api/CompleteOrderApiSpec.php

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,17 @@
1313

1414
namespace spec\Sylius\PayPalPlugin\Api;
1515

16-
use GuzzleHttp\Client;
1716
use PhpSpec\ObjectBehavior;
18-
use Psr\Http\Message\ResponseInterface;
19-
use Psr\Http\Message\StreamInterface;
2017
use Sylius\Component\Core\Model\OrderInterface;
2118
use Sylius\Component\Core\Model\PaymentInterface;
2219
use Sylius\PayPalPlugin\Api\CompleteOrderApiInterface;
20+
use Sylius\PayPalPlugin\Client\PayPalClientInterface;
2321

2422
final class CompleteOrderApiSpec extends ObjectBehavior
2523
{
26-
function let(Client $client): void
24+
function let(PayPalClientInterface $client): void
2725
{
28-
$this->beConstructedWith($client, 'https://api.test-paypal.com/', 'PARTNER_ATTRIBUTION_ID');
26+
$this->beConstructedWith($client);
2927
}
3028

3129
function it_implements_complete_order_api_interface(): void
@@ -34,30 +32,18 @@ function it_implements_complete_order_api_interface(): void
3432
}
3533

3634
function it_completes_pay_pal_order_with_given_id(
37-
Client $client,
35+
PayPalClientInterface $client,
3836
PaymentInterface $payment,
39-
OrderInterface $order,
40-
ResponseInterface $response,
41-
StreamInterface $body
37+
OrderInterface $order
4238
): void {
4339
$payment->getOrder()->willReturn($order);
4440
$payment->getAmount()->willReturn(10000);
4541
$order->getCurrencyCode()->willReturn('PLN');
4642

47-
$client->request(
48-
'POST',
49-
'https://api.test-paypal.com/v2/checkout/orders/123123/capture',
50-
[
51-
'headers' => [
52-
'Authorization' => 'Bearer TOKEN',
53-
'Prefer' => 'return=representation',
54-
'PayPal-Partner-Attribution-Id' => 'PARTNER_ATTRIBUTION_ID',
55-
'Content-Type' => 'application/json',
56-
],
57-
]
58-
)->willReturn($response);
59-
$response->getBody()->willReturn($body);
60-
$body->getContents()->willReturn('{"status": "COMPLETED", "id": 123}');
43+
$client
44+
->post('v2/checkout/orders/123123/capture', 'TOKEN')
45+
->willReturn(['status' => 'COMPLETED', 'id' => 123])
46+
;
6147

6248
$this->complete('TOKEN', '123123')->shouldReturn(['status' => 'COMPLETED', 'id' => 123]);
6349
}

spec/Api/CreateOrderApiSpec.php

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,21 @@
1313

1414
namespace spec\Sylius\PayPalPlugin\Api;
1515

16-
use GuzzleHttp\Client;
1716
use Payum\Core\Model\GatewayConfigInterface;
1817
use PhpSpec\ObjectBehavior;
1918
use Prophecy\Argument;
20-
use Psr\Http\Message\ResponseInterface;
21-
use Psr\Http\Message\StreamInterface;
2219
use Sylius\Component\Core\Model\AddressInterface;
2320
use Sylius\Component\Core\Model\OrderInterface;
2421
use Sylius\Component\Core\Model\PaymentInterface;
2522
use Sylius\Component\Core\Model\PaymentMethodInterface;
2623
use Sylius\PayPalPlugin\Api\CreateOrderApiInterface;
24+
use Sylius\PayPalPlugin\Client\PayPalClientInterface;
2725

2826
final class CreateOrderApiSpec extends ObjectBehavior
2927
{
30-
function let(Client $client): void
28+
function let(PayPalClientInterface $client): void
3129
{
32-
$this->beConstructedWith($client, 'https://api.test-paypal.com/', 'PARTNER_ATTRIBUTION_ID');
30+
$this->beConstructedWith($client);
3331
}
3432

3533
function it_implements_create_order_api_interface(): void
@@ -38,11 +36,9 @@ function it_implements_create_order_api_interface(): void
3836
}
3937

4038
function it_creates_pay_pal_order_based_on_given_payment(
41-
Client $client,
39+
PayPalClientInterface $client,
4240
PaymentInterface $payment,
4341
OrderInterface $order,
44-
ResponseInterface $response,
45-
StreamInterface $body,
4642
PaymentMethodInterface $paymentMethod,
4743
GatewayConfigInterface $gatewayConfig
4844
): void {
@@ -58,30 +54,25 @@ function it_creates_pay_pal_order_based_on_given_payment(
5854
['merchant_id' => 'merchant-id', 'sylius_merchant_id' => 'sylius-merchant-id']
5955
);
6056

61-
$client->request(
62-
'POST',
63-
'https://api.test-paypal.com/v2/checkout/orders',
57+
$client->post(
58+
'v2/checkout/orders',
59+
'TOKEN',
6460
Argument::that(function (array $data): bool {
6561
return
66-
$data['headers']['Authorization'] === 'Bearer TOKEN' &&
67-
$data['json']['intent'] === 'CAPTURE' &&
68-
$data['json']['purchase_units'][0]['amount']['value'] === 100 &&
69-
$data['json']['purchase_units'][0]['amount']['currency_code'] === 'PLN'
62+
$data['intent'] === 'CAPTURE' &&
63+
$data['purchase_units'][0]['amount']['value'] === 100 &&
64+
$data['purchase_units'][0]['amount']['currency_code'] === 'PLN'
7065
;
7166
})
72-
)->willReturn($response);
73-
$response->getBody()->willReturn($body);
74-
$body->getContents()->willReturn('{"status": "CREATED", "id": 123}');
67+
)->willReturn(['status' => 'CREATED', 'id' => 123]);
7568

7669
$this->create('TOKEN', $payment)->shouldReturn(['status' => 'CREATED', 'id' => 123]);
7770
}
7871

7972
function it_creates_pay_pal_order_with_shipping_address_based_on_given_payment(
80-
Client $client,
73+
PayPalClientInterface $client,
8174
PaymentInterface $payment,
8275
OrderInterface $order,
83-
ResponseInterface $response,
84-
StreamInterface $body,
8576
PaymentMethodInterface $paymentMethod,
8677
GatewayConfigInterface $gatewayConfig,
8778
AddressInterface $shippingAddress
@@ -104,25 +95,22 @@ function it_creates_pay_pal_order_with_shipping_address_based_on_given_payment(
10495
['merchant_id' => 'merchant-id', 'sylius_merchant_id' => 'sylius-merchant-id']
10596
);
10697

107-
$client->request(
108-
'POST',
109-
'https://api.test-paypal.com/v2/checkout/orders',
98+
$client->post(
99+
'v2/checkout/orders',
100+
'TOKEN',
110101
Argument::that(function (array $data): bool {
111102
return
112-
$data['headers']['Authorization'] === 'Bearer TOKEN' &&
113-
$data['json']['intent'] === 'CAPTURE' &&
114-
$data['json']['purchase_units'][0]['amount']['value'] === 100 &&
115-
$data['json']['purchase_units'][0]['amount']['currency_code'] === 'PLN' &&
116-
$data['json']['purchase_units'][0]['shipping']['name']['full_name'] === 'Gandalf The Grey' &&
117-
$data['json']['purchase_units'][0]['shipping']['address']['address_line_1'] === 'Hobbit St. 123' &&
118-
$data['json']['purchase_units'][0]['shipping']['address']['admin_area_2'] === 'Minas Tirith' &&
119-
$data['json']['purchase_units'][0]['shipping']['address']['postal_code'] === '000' &&
120-
$data['json']['purchase_units'][0]['shipping']['address']['country_code'] === 'US'
103+
$data['intent'] === 'CAPTURE' &&
104+
$data['purchase_units'][0]['amount']['value'] === 100 &&
105+
$data['purchase_units'][0]['amount']['currency_code'] === 'PLN' &&
106+
$data['purchase_units'][0]['shipping']['name']['full_name'] === 'Gandalf The Grey' &&
107+
$data['purchase_units'][0]['shipping']['address']['address_line_1'] === 'Hobbit St. 123' &&
108+
$data['purchase_units'][0]['shipping']['address']['admin_area_2'] === 'Minas Tirith' &&
109+
$data['purchase_units'][0]['shipping']['address']['postal_code'] === '000' &&
110+
$data['purchase_units'][0]['shipping']['address']['country_code'] === 'US'
121111
;
122112
})
123-
)->willReturn($response);
124-
$response->getBody()->willReturn($body);
125-
$body->getContents()->willReturn('{"status": "CREATED", "id": 123}');
113+
)->willReturn(['status' => 'CREATED', 'id' => 123]);
126114

127115
$this->create('TOKEN', $payment)->shouldReturn(['status' => 'CREATED', 'id' => 123]);
128116
}

spec/Api/OrderDetailsApiSpec.php

Lines changed: 9 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,45 +13,28 @@
1313

1414
namespace spec\Sylius\PayPalPlugin\Api;
1515

16-
use GuzzleHttp\Client;
1716
use PhpSpec\ObjectBehavior;
18-
use Psr\Http\Message\ResponseInterface;
19-
use Psr\Http\Message\StreamInterface;
20-
use Sylius\PayPalPlugin\Api\AuthorizeClientApiInterface;
2117
use Sylius\PayPalPlugin\Api\OrderDetailsApiInterface;
18+
use Sylius\PayPalPlugin\Client\PayPalClientInterface;
2219

2320
final class OrderDetailsApiSpec extends ObjectBehavior
2421
{
25-
function let(Client $client): void
22+
function let(PayPalClientInterface $client): void
2623
{
27-
$this->beConstructedWith($client, 'https://api.test-paypal.com/', 'PARTNER_ATTRIBUTION_ID');
24+
$this->beConstructedWith($client);
2825
}
2926

3027
function it_implements_pay_pal_order_details_provider_interface(): void
3128
{
3229
$this->shouldImplement(OrderDetailsApiInterface::class);
3330
}
3431

35-
function it_provides_details_about_pay_pal_order(
36-
Client $client,
37-
AuthorizeClientApiInterface $authorizeClientApi,
38-
ResponseInterface $detailsResponse,
39-
StreamInterface $detailsBody
40-
): void {
41-
$client->request(
42-
'GET',
43-
'https://api.test-paypal.com/v2/checkout/orders/123123',
44-
[
45-
'headers' => [
46-
'Authorization' => 'Bearer TOKEN',
47-
'Content-Type' => 'application/json',
48-
'Accept' => 'application/json',
49-
'PayPal-Partner-Attribution-Id' => 'PARTNER_ATTRIBUTION_ID',
50-
],
51-
]
52-
)->willReturn($detailsResponse);
53-
$detailsResponse->getBody()->willReturn($detailsBody);
54-
$detailsBody->getContents()->willReturn('{"total": 1111}');
32+
function it_provides_details_about_pay_pal_order(PayPalClientInterface $client): void
33+
{
34+
$client
35+
->get('v2/checkout/orders/123123', 'TOKEN')
36+
->willReturn(['total' => 1111])
37+
;
5538

5639
$this->get('TOKEN', '123123')->shouldReturn(['total' => 1111]);
5740
}

spec/Api/RefundPaymentApiSpec.php

Lines changed: 9 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,52 +13,28 @@
1313

1414
namespace spec\Sylius\PayPalPlugin\Api;
1515

16-
use GuzzleHttp\Client;
1716
use PhpSpec\ObjectBehavior;
18-
use Prophecy\Argument;
19-
use Psr\Http\Message\ResponseInterface;
20-
use Psr\Http\Message\StreamInterface;
21-
use Sylius\Component\Core\Model\OrderInterface;
22-
use Sylius\Component\Core\Model\PaymentInterface;
2317
use Sylius\PayPalPlugin\Api\RefundPaymentApiInterface;
18+
use Sylius\PayPalPlugin\Client\PayPalClientInterface;
2419

2520
final class RefundPaymentApiSpec extends ObjectBehavior
2621
{
27-
function let(Client $client): void
22+
function let(PayPalClientInterface $client): void
2823
{
29-
$this->beConstructedWith($client, 'https://api.test-paypal.com/', 'PARTNER_ATTRIBUTION_ID');
24+
$this->beConstructedWith($client);
3025
}
3126

3227
function it_implements_refund_order_api_interface(): void
3328
{
3429
$this->shouldImplement(RefundPaymentApiInterface::class);
3530
}
3631

37-
function it_refunds_pay_pal_payment_with_given_id(
38-
Client $client,
39-
PaymentInterface $payment,
40-
OrderInterface $order,
41-
ResponseInterface $response,
42-
StreamInterface $body
43-
): void {
44-
$payment->getOrder()->willReturn($order);
45-
$payment->getAmount()->willReturn(10000);
46-
$order->getCurrencyCode()->willReturn('PLN');
47-
48-
$client->request(
49-
'POST',
50-
'https://api.test-paypal.com/v2/payments/captures/123123/refund',
51-
Argument::that(function (array $options): bool {
52-
return
53-
$options['headers']['Authorization'] === 'Bearer TOKEN' &&
54-
$options['headers']['PayPal-Partner-Attribution-Id'] === 'PARTNER_ATTRIBUTION_ID' &&
55-
$options['headers']['Content-Type'] === 'application/json' &&
56-
is_string($options['headers']['PayPal-Request-Id'])
57-
;
58-
})
59-
)->willReturn($response);
60-
$response->getBody()->willReturn($body);
61-
$body->getContents()->willReturn('{"status": "COMPLETED", "id": "123123"}');
32+
function it_refunds_pay_pal_payment_with_given_id(PayPalClientInterface $client): void
33+
{
34+
$client
35+
->post('v2/payments/captures/123123/refund', 'TOKEN')
36+
->willReturn(['status' => 'COMPLETED', 'id' => '123123'])
37+
;
6238

6339
$this->refund('TOKEN', '123123')->shouldReturn(['status' => 'COMPLETED', 'id' => '123123']);
6440
}

spec/Api/UpdateOrderApiSpec.php

Lines changed: 13 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -13,71 +13,38 @@
1313

1414
namespace spec\Sylius\PayPalPlugin\Api;
1515

16-
use GuzzleHttp\Client;
1716
use PhpSpec\ObjectBehavior;
1817
use Prophecy\Argument;
19-
use Psr\Http\Message\ResponseInterface;
2018
use Sylius\PayPalPlugin\Api\UpdateOrderApiInterface;
21-
use Sylius\PayPalPlugin\Exception\PayPalOrderUpdateException;
19+
use Sylius\PayPalPlugin\Client\PayPalClientInterface;
2220

2321
final class UpdateOrderApiSpec extends ObjectBehavior
2422
{
25-
function let(Client $client): void
23+
function let(PayPalClientInterface $client): void
2624
{
27-
$this->beConstructedWith($client, 'https://api.test-paypal.com/', 'PARTNER-ATTRIBUTION-ID');
25+
$this->beConstructedWith($client);
2826
}
2927

3028
function it_implements_update_order_api_interface(): void
3129
{
3230
$this->shouldImplement(UpdateOrderApiInterface::class);
3331
}
3432

35-
function it_updates_pay_pal_order_with_given_new_total(
36-
Client $client,
37-
ResponseInterface $response
38-
): void {
39-
$client->request(
40-
'PATCH',
41-
'https://api.test-paypal.com/v2/checkout/orders/ORDER-ID',
33+
function it_updates_pay_pal_order_with_given_new_total(PayPalClientInterface $client): void
34+
{
35+
$client->patch(
36+
'v2/checkout/orders/ORDER-ID',
37+
'TOKEN',
4238
Argument::that(function (array $data): bool {
4339
return
44-
$data['headers']['Authorization'] === 'Bearer TOKEN' &&
45-
$data['headers']['PayPal-Partner-Attribution-Id'] === 'PARTNER-ATTRIBUTION-ID' &&
46-
$data['json'][0]['op'] === 'replace' &&
47-
$data['json'][0]['path'] === '/purchase_units/@reference_id==\'default\'/amount' &&
48-
$data['json'][0]['value']['value'] === '11.22' &&
49-
$data['json'][0]['value']['currency_code'] === 'USD'
40+
$data[0]['op'] === 'replace' &&
41+
$data[0]['path'] === '/purchase_units/@reference_id==\'default\'/amount' &&
42+
$data[0]['value']['value'] === '11.22' &&
43+
$data[0]['value']['currency_code'] === 'USD'
5044
;
5145
})
52-
)->willReturn($response);
53-
$response->getStatusCode()->willReturn(204);
46+
)->shouldBeCalled();
5447

5548
$this->update('TOKEN', 'ORDER-ID', '11.22', 'USD');
5649
}
57-
58-
function it_throws_an_exception_if_update_is_not_successful(
59-
Client $client,
60-
ResponseInterface $response
61-
): void {
62-
$client->request(
63-
'PATCH',
64-
'https://api.test-paypal.com/v2/checkout/orders/ORDER-ID',
65-
Argument::that(function (array $data): bool {
66-
return
67-
$data['headers']['Authorization'] === 'Bearer TOKEN' &&
68-
$data['headers']['PayPal-Partner-Attribution-Id'] === 'PARTNER-ATTRIBUTION-ID' &&
69-
$data['json'][0]['op'] === 'replace' &&
70-
$data['json'][0]['path'] === '/purchase_units/@reference_id==\'default\'/amount' &&
71-
$data['json'][0]['value']['value'] === '11.22' &&
72-
$data['json'][0]['value']['currency_code'] === 'USD'
73-
;
74-
})
75-
)->willReturn($response);
76-
$response->getStatusCode()->willReturn(500);
77-
78-
$this
79-
->shouldThrow(PayPalOrderUpdateException::class)
80-
->during('update', ['TOKEN', 'ORDER-ID', '11.22', 'USD'])
81-
;
82-
}
8350
}

0 commit comments

Comments
 (0)