-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature #106 Order refund webhook fix (SirDomin)
This PR was merged into the 1.0-dev branch. Discussion ---------- paypal doesnt send order id in webhook, we need to make calls to get order payment and then order with its id Based on #105 Commits ------- ca0e8db handle order refund webhook fixed 8cebe3a getting order data from webhook id 55740fa pr-fix
- Loading branch information
Showing
19 changed files
with
314 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace spec\Sylius\PayPalPlugin\Provider; | ||
|
||
use PhpSpec\ObjectBehavior; | ||
use Sylius\Component\Core\Model\PaymentMethodInterface; | ||
use Sylius\PayPalPlugin\Api\CacheAuthorizeClientApiInterface; | ||
use Sylius\PayPalPlugin\Api\GenericApiInterface; | ||
use Sylius\PayPalPlugin\Exception\PayPalWrongDataException; | ||
use Sylius\PayPalPlugin\Provider\PayPalPaymentMethodProviderInterface; | ||
|
||
final class PayPalRefundDataProviderSpec extends ObjectBehavior | ||
{ | ||
public function let( | ||
CacheAuthorizeClientApiInterface $authorizeClientApi, | ||
GenericApiInterface $genericApi, | ||
PayPalPaymentMethodProviderInterface $payPalPaymentMethodProvider | ||
) { | ||
$this->beConstructedWith($authorizeClientApi, $genericApi, $payPalPaymentMethodProvider); | ||
} | ||
|
||
public function it_provides_data_from_provided_url( | ||
PayPalPaymentMethodProviderInterface $payPalPaymentMethodProvider, | ||
PaymentMethodInterface $paymentMethod, | ||
CacheAuthorizeClientApiInterface $authorizeClientApi, | ||
GenericApiInterface $genericApi | ||
): void { | ||
$payPalPaymentMethodProvider->provide()->willReturn($paymentMethod); | ||
$authorizeClientApi->authorize($paymentMethod)->willReturn('TOKEN'); | ||
$genericApi->get('TOKEN', 'https://get-refund-data.com')->willReturn( | ||
[ | ||
'links' => [ | ||
['rel' => 'self', 'href' => 'https://self.url.com'], | ||
['rel' => 'up', 'href' => 'https://up.url.com'], | ||
], | ||
], | ||
); | ||
|
||
$genericApi->get('TOKEN', 'https://up.url.com')->shouldBeCalled(); | ||
|
||
$this->provide('https://get-refund-data.com'); | ||
} | ||
|
||
public function it_throws_error_if_paypal_data_doesnt_contain_url( | ||
PayPalPaymentMethodProviderInterface $payPalPaymentMethodProvider, | ||
PaymentMethodInterface $paymentMethod, | ||
CacheAuthorizeClientApiInterface $authorizeClientApi, | ||
GenericApiInterface $genericApi | ||
): void { | ||
$payPalPaymentMethodProvider->provide()->willReturn($paymentMethod); | ||
$authorizeClientApi->authorize($paymentMethod)->willReturn('TOKEN'); | ||
$genericApi->get('TOKEN', 'https://get-refund-data.com')->willReturn( | ||
[ | ||
'links' => [ | ||
['rel' => 'self', 'href' => 'https://self.url.com'], | ||
['rel' => 'get', 'href' => 'https://get.url.com'], | ||
], | ||
], | ||
); | ||
|
||
$this->shouldThrow(PayPalWrongDataException::class)->during('provide', ['https://get-refund-data.com']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\PayPalPlugin\Api; | ||
|
||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
|
||
final class GenericApi implements GenericApiInterface | ||
{ | ||
/** @var HttpClientInterface */ | ||
private $client; | ||
|
||
public function __construct(HttpClientInterface $client) | ||
{ | ||
$this->client = $client; | ||
} | ||
|
||
public function get(string $token, string $url): array | ||
{ | ||
$options = [ | ||
'headers' => [ | ||
'Authorization' => 'Bearer ' . $token, | ||
'Content-Type' => 'application/json', | ||
'Accept' => 'application/json', | ||
], | ||
]; | ||
|
||
return $this->client->request('GET', $url, $options)->toArray(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\PayPalPlugin\Api; | ||
|
||
interface GenericApiInterface | ||
{ | ||
public function get(string $token, string $url): array; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\PayPalPlugin\Exception; | ||
|
||
final class PayPalPaymentMethodNotFoundException extends \Exception | ||
{ | ||
public function __construct() | ||
{ | ||
parent::__construct('PayPal payment method not found'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\PayPalPlugin\Exception; | ||
|
||
final class PayPalWrongDataException extends \Exception | ||
{ | ||
public function __construct() | ||
{ | ||
parent::__construct('PayPal data does not contain links to order'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\PayPalPlugin\Provider; | ||
|
||
use Sylius\Bundle\PayumBundle\Model\GatewayConfigInterface; | ||
use Sylius\Component\Core\Model\PaymentMethodInterface; | ||
use Sylius\Component\Core\Repository\PaymentMethodRepositoryInterface; | ||
use Sylius\PayPalPlugin\Exception\PayPalPaymentMethodNotFoundException; | ||
|
||
final class PayPalPaymentMethodProvider implements PayPalPaymentMethodProviderInterface | ||
{ | ||
/** @var PaymentMethodRepositoryInterface */ | ||
private $paymentMethodRepository; | ||
|
||
public function __construct(PaymentMethodRepositoryInterface $paymentMethodRepository) | ||
{ | ||
$this->paymentMethodRepository = $paymentMethodRepository; | ||
} | ||
|
||
public function provide(): PaymentMethodInterface | ||
{ | ||
$payments = $this->paymentMethodRepository->findAll(); | ||
|
||
/** @var PaymentMethodInterface $payment */ | ||
foreach ($payments as $payment) { | ||
/** @var GatewayConfigInterface $gatewayConfig */ | ||
$gatewayConfig = $payment->getGatewayConfig(); | ||
|
||
if ($gatewayConfig->getFactoryName() === 'sylius.pay_pal') { | ||
return $payment; | ||
} | ||
} | ||
|
||
throw new PayPalPaymentMethodNotFoundException(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\PayPalPlugin\Provider; | ||
|
||
use Sylius\Component\Core\Model\PaymentMethodInterface; | ||
use Sylius\PayPalPlugin\Exception\PayPalPaymentMethodNotFoundException; | ||
|
||
interface PayPalPaymentMethodProviderInterface | ||
{ | ||
/** @throws PayPalPaymentMethodNotFoundException */ | ||
public function provide(): PaymentMethodInterface; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\PayPalPlugin\Provider; | ||
|
||
use Sylius\PayPalPlugin\Api\CacheAuthorizeClientApiInterface; | ||
use Sylius\PayPalPlugin\Api\GenericApiInterface; | ||
use Sylius\PayPalPlugin\Exception\PayPalWrongDataException; | ||
|
||
final class PayPalRefundDataProvider implements PayPalRefundDataProviderInterface | ||
{ | ||
/** @var CacheAuthorizeClientApiInterface */ | ||
private $authorizeClientApi; | ||
|
||
/** @var PayPalPaymentMethodProviderInterface */ | ||
private $payPalPaymentMethodProvider; | ||
|
||
/** @var GenericApiInterface */ | ||
private $genericApi; | ||
|
||
public function __construct( | ||
CacheAuthorizeClientApiInterface $authorizeClientApi, | ||
GenericApiInterface $genericApi, | ||
PayPalPaymentMethodProviderInterface $payPalPaymentMethodProvider | ||
) { | ||
$this->authorizeClientApi = $authorizeClientApi; | ||
$this->genericApi = $genericApi; | ||
$this->payPalPaymentMethodProvider = $payPalPaymentMethodProvider; | ||
} | ||
|
||
public function provide(string $url): array | ||
{ | ||
$paymentMethod = $this->payPalPaymentMethodProvider->provide(); | ||
$token = $this->authorizeClientApi->authorize($paymentMethod); | ||
|
||
$refundData = $this->genericApi->get($token, $url); | ||
|
||
/** @var string[] $link */ | ||
foreach ($refundData['links'] as $link) { | ||
if ($link['rel'] === 'up') { | ||
return $this->genericApi->get($token, $link['href']); | ||
} | ||
} | ||
|
||
throw new PayPalWrongDataException(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\PayPalPlugin\Provider; | ||
|
||
interface PayPalRefundDataProviderInterface | ||
{ | ||
public function provide(string $refundUrl): array; | ||
} |
Oops, something went wrong.