Skip to content

Commit

Permalink
Added negative testing headers
Browse files Browse the repository at this point in the history
  • Loading branch information
L3RAZ committed Sep 13, 2024
1 parent e556f20 commit af9f9a9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 18 deletions.
1 change: 1 addition & 0 deletions config/http-clients.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ services:
public: true
arguments:
- "@ps_checkout.http.client"
- '@PrestaShop\Module\PrestashopCheckout\Configuration\PrestaShopConfiguration'

PrestaShop\Module\PrestashopCheckout\Http\CheckoutHttpClient:
class: 'PrestaShop\Module\PrestashopCheckout\Http\CheckoutHttpClient'
Expand Down
58 changes: 40 additions & 18 deletions src/Http/MaaslandHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Http\Client\Exception\NetworkException;
use Http\Client\Exception\RequestException;
use Http\Client\Exception\TransferException;
use PrestaShop\Module\PrestashopCheckout\Configuration\PrestaShopConfiguration;
use PrestaShop\Module\PrestashopCheckout\Exception\HttpTimeoutException;
use PrestaShop\Module\PrestashopCheckout\Exception\PayPalException;
use PrestaShop\Module\PrestashopCheckout\PayPalError;
Expand All @@ -37,15 +38,20 @@ class MaaslandHttpClient implements HttpClientInterface
* @var HttpClientInterface
*/
private $httpClient;
/**
* @var PrestaShopConfiguration
*/
private $configuration;

public function __construct(HttpClientInterface $httpClient)
public function __construct(HttpClientInterface $httpClient, PrestaShopConfiguration $configuration)
{
$this->httpClient = $httpClient;
$this->configuration = $configuration;
}

/**
* @param array $payload
* @param array $options
* @param array $headers
*
* @return ResponseInterface
*
Expand All @@ -56,14 +62,18 @@ public function __construct(HttpClientInterface $httpClient)
* @throws PayPalException
* @throws HttpTimeoutException
*/
public function createOrder(array $payload, array $options = [])
public function createOrder(array $payload, array $headers = [])
{
return $this->sendRequest(new Request('POST', '/payments/order/create', $options, json_encode($payload)));
if ($ntHeader = $this->configuration->get('PS_CHECKOUT_NT_CREATE_ORDER')) {
$headers['NT-PayPalOrderCreate'] = $ntHeader;
}

return $this->sendRequest(new Request('POST', '/payments/order/create', $headers, json_encode($payload)));
}

/**
* @param array $payload
* @param array $options
* @param array $headers
*
* @return ResponseInterface
*
Expand All @@ -74,14 +84,18 @@ public function createOrder(array $payload, array $options = [])
* @throws PayPalException
* @throws HttpTimeoutException
*/
public function updateOrder(array $payload, array $options = [])
public function updateOrder(array $payload, array $headers = [])
{
return $this->sendRequest(new Request('POST', '/payments/order/update', $options, json_encode($payload)));
if ($ntHeader = $this->configuration->get('PS_CHECKOUT_NT_UPDATE_ORDER')) {
$headers['NT-PayPalOrderUpdate'] = $ntHeader;
}

return $this->sendRequest(new Request('POST', '/payments/order/update', $headers, json_encode($payload)));
}

/**
* @param array $payload
* @param array $options
* @param array $headers
*
* @return ResponseInterface
*
Expand All @@ -92,14 +106,18 @@ public function updateOrder(array $payload, array $options = [])
* @throws PayPalException
* @throws HttpTimeoutException
*/
public function fetchOrder(array $payload, array $options = [])
public function fetchOrder(array $payload, array $headers = [])
{
return $this->sendRequest(new Request('POST', '/payments/order/fetch', $options, json_encode($payload)));
if ($ntHeader = $this->configuration->get('PS_CHECKOUT_NT_FETCH_ORDER')) {
$headers['NT-PayPalOrderFetch'] = $ntHeader;
}

return $this->sendRequest(new Request('POST', '/payments/order/fetch', $headers, json_encode($payload)));
}

/**
* @param array $payload
* @param array $options
* @param array $headers
*
* @return ResponseInterface
*
Expand All @@ -110,14 +128,18 @@ public function fetchOrder(array $payload, array $options = [])
* @throws PayPalException
* @throws HttpTimeoutException
*/
public function captureOrder(array $payload, array $options = [])
public function captureOrder(array $payload, array $headers = [])
{
return $this->sendRequest(new Request('POST', '/payments/order/capture', $options, json_encode($payload)));
if ($ntHeader = $this->configuration->get('PS_CHECKOUT_NT_CAPTURE_ORDER')) {
$headers['NT-PayPalOrderCapture'] = $ntHeader;
}

return $this->sendRequest(new Request('POST', '/payments/order/capture', $headers, json_encode($payload)));
}

/**
* @param array $payload
* @param array $options
* @param array $headers
*
* @return ResponseInterface
*
Expand All @@ -128,9 +150,9 @@ public function captureOrder(array $payload, array $options = [])
* @throws PayPalException
* @throws HttpTimeoutException
*/
public function refundOrder(array $payload, array $options = [])
public function refundOrder(array $payload, array $headers = [])
{
return $this->sendRequest(new Request('POST', '/payments/order/refund', $options, json_encode($payload)));
return $this->sendRequest(new Request('POST', '/payments/order/refund', $headers, json_encode($payload)));
}

/**
Expand Down Expand Up @@ -202,9 +224,9 @@ private function extractMessage(array $body)
*
* @return array
*/
public function getShopSignature(array $payload, array $options = [])
public function getShopSignature(array $payload, array $headers = [])
{
$response = $this->sendRequest(new Request('POST', '/payments/shop/verify_webhook_signature', $options, json_encode($payload)));
$response = $this->sendRequest(new Request('POST', '/payments/shop/verify_webhook_signature', $headers, json_encode($payload)));

return json_decode($response->getBody(), true);
}
Expand Down

0 comments on commit af9f9a9

Please sign in to comment.