Skip to content

Commit

Permalink
feature #66 Log debug ID (Zales0123)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
SirDomin authored Aug 31, 2020
2 parents 91fcc7a + 7e3d4e1 commit 6cf47de
Show file tree
Hide file tree
Showing 15 changed files with 438 additions and 316 deletions.
32 changes: 9 additions & 23 deletions spec/Api/CompleteOrderApiSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,17 @@

namespace spec\Sylius\PayPalPlugin\Api;

use GuzzleHttp\Client;
use PhpSpec\ObjectBehavior;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\PaymentInterface;
use Sylius\PayPalPlugin\Api\CompleteOrderApiInterface;
use Sylius\PayPalPlugin\Client\PayPalClientInterface;

final class CompleteOrderApiSpec extends ObjectBehavior
{
function let(Client $client): void
function let(PayPalClientInterface $client): void
{
$this->beConstructedWith($client, 'https://api.test-paypal.com/', 'PARTNER_ATTRIBUTION_ID');
$this->beConstructedWith($client);
}

function it_implements_complete_order_api_interface(): void
Expand All @@ -34,30 +32,18 @@ function it_implements_complete_order_api_interface(): void
}

function it_completes_pay_pal_order_with_given_id(
Client $client,
PayPalClientInterface $client,
PaymentInterface $payment,
OrderInterface $order,
ResponseInterface $response,
StreamInterface $body
OrderInterface $order
): void {
$payment->getOrder()->willReturn($order);
$payment->getAmount()->willReturn(10000);
$order->getCurrencyCode()->willReturn('PLN');

$client->request(
'POST',
'https://api.test-paypal.com/v2/checkout/orders/123123/capture',
[
'headers' => [
'Authorization' => 'Bearer TOKEN',
'Prefer' => 'return=representation',
'PayPal-Partner-Attribution-Id' => 'PARTNER_ATTRIBUTION_ID',
'Content-Type' => 'application/json',
],
]
)->willReturn($response);
$response->getBody()->willReturn($body);
$body->getContents()->willReturn('{"status": "COMPLETED", "id": 123}');
$client
->post('v2/checkout/orders/123123/capture', 'TOKEN')
->willReturn(['status' => 'COMPLETED', 'id' => 123])
;

$this->complete('TOKEN', '123123')->shouldReturn(['status' => 'COMPLETED', 'id' => 123]);
}
Expand Down
60 changes: 24 additions & 36 deletions spec/Api/CreateOrderApiSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,21 @@

namespace spec\Sylius\PayPalPlugin\Api;

use GuzzleHttp\Client;
use Payum\Core\Model\GatewayConfigInterface;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use Sylius\Component\Core\Model\AddressInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\PaymentInterface;
use Sylius\Component\Core\Model\PaymentMethodInterface;
use Sylius\PayPalPlugin\Api\CreateOrderApiInterface;
use Sylius\PayPalPlugin\Client\PayPalClientInterface;

final class CreateOrderApiSpec extends ObjectBehavior
{
function let(Client $client): void
function let(PayPalClientInterface $client): void
{
$this->beConstructedWith($client, 'https://api.test-paypal.com/', 'PARTNER_ATTRIBUTION_ID');
$this->beConstructedWith($client);
}

function it_implements_create_order_api_interface(): void
Expand All @@ -38,11 +36,9 @@ function it_implements_create_order_api_interface(): void
}

function it_creates_pay_pal_order_based_on_given_payment(
Client $client,
PayPalClientInterface $client,
PaymentInterface $payment,
OrderInterface $order,
ResponseInterface $response,
StreamInterface $body,
PaymentMethodInterface $paymentMethod,
GatewayConfigInterface $gatewayConfig
): void {
Expand All @@ -58,30 +54,25 @@ function it_creates_pay_pal_order_based_on_given_payment(
['merchant_id' => 'merchant-id', 'sylius_merchant_id' => 'sylius-merchant-id']
);

$client->request(
'POST',
'https://api.test-paypal.com/v2/checkout/orders',
$client->post(
'v2/checkout/orders',
'TOKEN',
Argument::that(function (array $data): bool {
return
$data['headers']['Authorization'] === 'Bearer TOKEN' &&
$data['json']['intent'] === 'CAPTURE' &&
$data['json']['purchase_units'][0]['amount']['value'] === 100 &&
$data['json']['purchase_units'][0]['amount']['currency_code'] === 'PLN'
$data['intent'] === 'CAPTURE' &&
$data['purchase_units'][0]['amount']['value'] === 100 &&
$data['purchase_units'][0]['amount']['currency_code'] === 'PLN'
;
})
)->willReturn($response);
$response->getBody()->willReturn($body);
$body->getContents()->willReturn('{"status": "CREATED", "id": 123}');
)->willReturn(['status' => 'CREATED', 'id' => 123]);

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

function it_creates_pay_pal_order_with_shipping_address_based_on_given_payment(
Client $client,
PayPalClientInterface $client,
PaymentInterface $payment,
OrderInterface $order,
ResponseInterface $response,
StreamInterface $body,
PaymentMethodInterface $paymentMethod,
GatewayConfigInterface $gatewayConfig,
AddressInterface $shippingAddress
Expand All @@ -104,25 +95,22 @@ function it_creates_pay_pal_order_with_shipping_address_based_on_given_payment(
['merchant_id' => 'merchant-id', 'sylius_merchant_id' => 'sylius-merchant-id']
);

$client->request(
'POST',
'https://api.test-paypal.com/v2/checkout/orders',
$client->post(
'v2/checkout/orders',
'TOKEN',
Argument::that(function (array $data): bool {
return
$data['headers']['Authorization'] === 'Bearer TOKEN' &&
$data['json']['intent'] === 'CAPTURE' &&
$data['json']['purchase_units'][0]['amount']['value'] === 100 &&
$data['json']['purchase_units'][0]['amount']['currency_code'] === 'PLN' &&
$data['json']['purchase_units'][0]['shipping']['name']['full_name'] === 'Gandalf The Grey' &&
$data['json']['purchase_units'][0]['shipping']['address']['address_line_1'] === 'Hobbit St. 123' &&
$data['json']['purchase_units'][0]['shipping']['address']['admin_area_2'] === 'Minas Tirith' &&
$data['json']['purchase_units'][0]['shipping']['address']['postal_code'] === '000' &&
$data['json']['purchase_units'][0]['shipping']['address']['country_code'] === 'US'
$data['intent'] === 'CAPTURE' &&
$data['purchase_units'][0]['amount']['value'] === 100 &&
$data['purchase_units'][0]['amount']['currency_code'] === 'PLN' &&
$data['purchase_units'][0]['shipping']['name']['full_name'] === 'Gandalf The Grey' &&
$data['purchase_units'][0]['shipping']['address']['address_line_1'] === 'Hobbit St. 123' &&
$data['purchase_units'][0]['shipping']['address']['admin_area_2'] === 'Minas Tirith' &&
$data['purchase_units'][0]['shipping']['address']['postal_code'] === '000' &&
$data['purchase_units'][0]['shipping']['address']['country_code'] === 'US'
;
})
)->willReturn($response);
$response->getBody()->willReturn($body);
$body->getContents()->willReturn('{"status": "CREATED", "id": 123}');
)->willReturn(['status' => 'CREATED', 'id' => 123]);

$this->create('TOKEN', $payment)->shouldReturn(['status' => 'CREATED', 'id' => 123]);
}
Expand Down
35 changes: 9 additions & 26 deletions spec/Api/OrderDetailsApiSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,45 +13,28 @@

namespace spec\Sylius\PayPalPlugin\Api;

use GuzzleHttp\Client;
use PhpSpec\ObjectBehavior;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use Sylius\PayPalPlugin\Api\AuthorizeClientApiInterface;
use Sylius\PayPalPlugin\Api\OrderDetailsApiInterface;
use Sylius\PayPalPlugin\Client\PayPalClientInterface;

final class OrderDetailsApiSpec extends ObjectBehavior
{
function let(Client $client): void
function let(PayPalClientInterface $client): void
{
$this->beConstructedWith($client, 'https://api.test-paypal.com/', 'PARTNER_ATTRIBUTION_ID');
$this->beConstructedWith($client);
}

function it_implements_pay_pal_order_details_provider_interface(): void
{
$this->shouldImplement(OrderDetailsApiInterface::class);
}

function it_provides_details_about_pay_pal_order(
Client $client,
AuthorizeClientApiInterface $authorizeClientApi,
ResponseInterface $detailsResponse,
StreamInterface $detailsBody
): void {
$client->request(
'GET',
'https://api.test-paypal.com/v2/checkout/orders/123123',
[
'headers' => [
'Authorization' => 'Bearer TOKEN',
'Content-Type' => 'application/json',
'Accept' => 'application/json',
'PayPal-Partner-Attribution-Id' => 'PARTNER_ATTRIBUTION_ID',
],
]
)->willReturn($detailsResponse);
$detailsResponse->getBody()->willReturn($detailsBody);
$detailsBody->getContents()->willReturn('{"total": 1111}');
function it_provides_details_about_pay_pal_order(PayPalClientInterface $client): void
{
$client
->get('v2/checkout/orders/123123', 'TOKEN')
->willReturn(['total' => 1111])
;

$this->get('TOKEN', '123123')->shouldReturn(['total' => 1111]);
}
Expand Down
42 changes: 9 additions & 33 deletions spec/Api/RefundPaymentApiSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,52 +13,28 @@

namespace spec\Sylius\PayPalPlugin\Api;

use GuzzleHttp\Client;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use Sylius\Component\Core\Model\OrderInterface;
use Sylius\Component\Core\Model\PaymentInterface;
use Sylius\PayPalPlugin\Api\RefundPaymentApiInterface;
use Sylius\PayPalPlugin\Client\PayPalClientInterface;

final class RefundPaymentApiSpec extends ObjectBehavior
{
function let(Client $client): void
function let(PayPalClientInterface $client): void
{
$this->beConstructedWith($client, 'https://api.test-paypal.com/', 'PARTNER_ATTRIBUTION_ID');
$this->beConstructedWith($client);
}

function it_implements_refund_order_api_interface(): void
{
$this->shouldImplement(RefundPaymentApiInterface::class);
}

function it_refunds_pay_pal_payment_with_given_id(
Client $client,
PaymentInterface $payment,
OrderInterface $order,
ResponseInterface $response,
StreamInterface $body
): void {
$payment->getOrder()->willReturn($order);
$payment->getAmount()->willReturn(10000);
$order->getCurrencyCode()->willReturn('PLN');

$client->request(
'POST',
'https://api.test-paypal.com/v2/payments/captures/123123/refund',
Argument::that(function (array $options): bool {
return
$options['headers']['Authorization'] === 'Bearer TOKEN' &&
$options['headers']['PayPal-Partner-Attribution-Id'] === 'PARTNER_ATTRIBUTION_ID' &&
$options['headers']['Content-Type'] === 'application/json' &&
is_string($options['headers']['PayPal-Request-Id'])
;
})
)->willReturn($response);
$response->getBody()->willReturn($body);
$body->getContents()->willReturn('{"status": "COMPLETED", "id": "123123"}');
function it_refunds_pay_pal_payment_with_given_id(PayPalClientInterface $client): void
{
$client
->post('v2/payments/captures/123123/refund', 'TOKEN')
->willReturn(['status' => 'COMPLETED', 'id' => '123123'])
;

$this->refund('TOKEN', '123123')->shouldReturn(['status' => 'COMPLETED', 'id' => '123123']);
}
Expand Down
59 changes: 13 additions & 46 deletions spec/Api/UpdateOrderApiSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,71 +13,38 @@

namespace spec\Sylius\PayPalPlugin\Api;

use GuzzleHttp\Client;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Psr\Http\Message\ResponseInterface;
use Sylius\PayPalPlugin\Api\UpdateOrderApiInterface;
use Sylius\PayPalPlugin\Exception\PayPalOrderUpdateException;
use Sylius\PayPalPlugin\Client\PayPalClientInterface;

final class UpdateOrderApiSpec extends ObjectBehavior
{
function let(Client $client): void
function let(PayPalClientInterface $client): void
{
$this->beConstructedWith($client, 'https://api.test-paypal.com/', 'PARTNER-ATTRIBUTION-ID');
$this->beConstructedWith($client);
}

function it_implements_update_order_api_interface(): void
{
$this->shouldImplement(UpdateOrderApiInterface::class);
}

function it_updates_pay_pal_order_with_given_new_total(
Client $client,
ResponseInterface $response
): void {
$client->request(
'PATCH',
'https://api.test-paypal.com/v2/checkout/orders/ORDER-ID',
function it_updates_pay_pal_order_with_given_new_total(PayPalClientInterface $client): void
{
$client->patch(
'v2/checkout/orders/ORDER-ID',
'TOKEN',
Argument::that(function (array $data): bool {
return
$data['headers']['Authorization'] === 'Bearer TOKEN' &&
$data['headers']['PayPal-Partner-Attribution-Id'] === 'PARTNER-ATTRIBUTION-ID' &&
$data['json'][0]['op'] === 'replace' &&
$data['json'][0]['path'] === '/purchase_units/@reference_id==\'default\'/amount' &&
$data['json'][0]['value']['value'] === '11.22' &&
$data['json'][0]['value']['currency_code'] === 'USD'
$data[0]['op'] === 'replace' &&
$data[0]['path'] === '/purchase_units/@reference_id==\'default\'/amount' &&
$data[0]['value']['value'] === '11.22' &&
$data[0]['value']['currency_code'] === 'USD'
;
})
)->willReturn($response);
$response->getStatusCode()->willReturn(204);
)->shouldBeCalled();

$this->update('TOKEN', 'ORDER-ID', '11.22', 'USD');
}

function it_throws_an_exception_if_update_is_not_successful(
Client $client,
ResponseInterface $response
): void {
$client->request(
'PATCH',
'https://api.test-paypal.com/v2/checkout/orders/ORDER-ID',
Argument::that(function (array $data): bool {
return
$data['headers']['Authorization'] === 'Bearer TOKEN' &&
$data['headers']['PayPal-Partner-Attribution-Id'] === 'PARTNER-ATTRIBUTION-ID' &&
$data['json'][0]['op'] === 'replace' &&
$data['json'][0]['path'] === '/purchase_units/@reference_id==\'default\'/amount' &&
$data['json'][0]['value']['value'] === '11.22' &&
$data['json'][0]['value']['currency_code'] === 'USD'
;
})
)->willReturn($response);
$response->getStatusCode()->willReturn(500);

$this
->shouldThrow(PayPalOrderUpdateException::class)
->during('update', ['TOKEN', 'ORDER-ID', '11.22', 'USD'])
;
}
}
Loading

0 comments on commit 6cf47de

Please sign in to comment.