Skip to content

Commit

Permalink
Merge pull request #6 from MyOnlineStore/merge-upstream
Browse files Browse the repository at this point in the history
Merge upstream
fred-jan authored Nov 19, 2019

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
2 parents 74c4f32 + d7ac171 commit c79dba6
Showing 5 changed files with 148 additions and 4 deletions.
34 changes: 34 additions & 0 deletions src/Gateway.php
Original file line number Diff line number Diff line change
@@ -40,6 +40,11 @@
*/
class Gateway extends AbstractGateway
{
/**
* Version of our gateway.
*/
const GATEWAY_VERSION = "5.2";

/**
* @return string
*/
@@ -75,6 +80,35 @@ public function setApiKey($value)
return $this->setParameter('apiKey', $value);
}

/**
* @return array|null
*/
public function getVersionStrings()
{
return $this->getParameter('versionStrings');
}

/**
* @param string $value
* @return $this
*/
public function setVersionStrings(array $values)
{
return $this->setParameter('versionStrings', $values);
}

/**
* @param string $value
* @return $this
*/
public function addVersionString($value)
{
$versionStrings = $this->getVersionStrings() ?: [];
$versionStrings[] = str_replace([" ", "\t", "\n", "\r"], '-', $value);

return $this->setVersionStrings($versionStrings);
}

/**
* @param array $parameters
* @return FetchIssuersRequest
41 changes: 38 additions & 3 deletions src/Message/Request/AbstractMollieRequest.php
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@

use Omnipay\Common\ItemBag;
use Omnipay\Common\Message\AbstractRequest;
use Omnipay\Mollie\Gateway;
use Omnipay\Mollie\Item;

/**
@@ -44,6 +45,23 @@ public function setApiKey($value)
return $this->setParameter('apiKey', $value);
}

/**
* @return array|null
*/
public function getVersionStrings()
{
return $this->getParameter('versionStrings');
}

/**
* @param string $value
* @return $this
*/
public function setVersionStrings(array $values)
{
return $this->setParameter('versionStrings', $values);
}

/**
* @param string $value
* @return $this
@@ -89,12 +107,29 @@ public function setItems($items)
*/
protected function sendRequest($method, $endpoint, array $data = null)
{
$versions = [
'Omnipay-Mollie/' . Gateway::GATEWAY_VERSION,
'PHP/' . phpversion(),
];

if ($customVersions = $this->getParameter('versionStrings')) {
$versions = array_merge($versions, $customVersions);
}

$headers = [
'Accept' => "application/json",
'Authorization' => 'Bearer ' . $this->getApiKey(),
'User-Agent' => implode(' ', $versions),
];

if (function_exists("php_uname")) {
$headers['X-Mollie-Client-Info'] = php_uname();
}

$response = $this->httpClient->request(
$method,
$this->baseUrl . $this->apiVersion . $endpoint,
[
'Authorization' => 'Bearer ' . $this->getApiKey()
],
$headers,
($data === null || $data === []) ? null : json_encode($data)
);

8 changes: 8 additions & 0 deletions src/Message/Response/FetchTransactionResponse.php
Original file line number Diff line number Diff line change
@@ -114,6 +114,14 @@ public function isPartialRefunded()
&& $this->data['amountRemaining']['value'] > 0;
}

/**
* @return boolean
*/
public function hasChargebacks()
{
return !empty($this->data['_links']['chargebacks']);
}

/**
* @return string|null
*/
65 changes: 65 additions & 0 deletions tests/Message/AbstractMollieRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Omnipay\Mollie\Test\Message;

use GuzzleHttp\Psr7\Request;
use Omnipay\Mollie\Gateway;
use Omnipay\Mollie\Message\Request\CompleteOrderRequest;
use Omnipay\Mollie\Message\Request\CompletePurchaseRequest;
use Omnipay\Mollie\Message\Response\CompleteOrderResponse;
use Omnipay\Mollie\Message\Response\CompletePurchaseResponse;
use Omnipay\Tests\TestCase;

class AbstractMollieRequestTest extends TestCase
{
/**
* @var Gateway
*/
protected $gateway;

public function setUp()
{
$this->gateway = new Gateway($this->getHttpClient());
}


public function testVersionString()
{
$request = $this->gateway->fetchIssuers();
$request->send();

/** @var \Psr\Http\Message\RequestInterface $httpRequest */
$httpRequest = $this->getMockedRequests()[0];

$versionString = 'Omnipay-Mollie/'.Gateway::GATEWAY_VERSION.' PHP/' . phpversion();
$this->assertEquals($versionString, $httpRequest->getHeaderLine('User-Agent'));
}

public function testCustomVersionStrings()
{
$this->gateway->initialize([
'versionStrings' => ['Acme/6.84']
]);
$request = $this->gateway->fetchIssuers();
$request->send();

/** @var \Psr\Http\Message\RequestInterface $httpRequest */
$httpRequest = $this->getMockedRequests()[0];

$versionString = 'Omnipay-Mollie/'.Gateway::GATEWAY_VERSION.' PHP/' . phpversion() . ' Acme/6.84';
$this->assertEquals($versionString, $httpRequest->getHeaderLine('User-Agent'));
}

public function testAddVersionString()
{
$this->gateway->addVersionString('Acme/6.84');
$request = $this->gateway->fetchIssuers();
$request->send();

/** @var \Psr\Http\Message\RequestInterface $httpRequest */
$httpRequest = $this->getMockedRequests()[0];

$versionString = 'Omnipay-Mollie/'.Gateway::GATEWAY_VERSION.' PHP/' . phpversion() . ' Acme/6.84';
$this->assertEquals($versionString, $httpRequest->getHeaderLine('User-Agent'));
}
}
4 changes: 3 additions & 1 deletion tests/Message/FetchOrderRequestTest.php
Original file line number Diff line number Diff line change
@@ -105,7 +105,9 @@ public function testSendDataWithIncludingPayments()
->with(
FetchOrderRequest::GET,
'https://api.mollie.com/v2/orders/ord_kEn1PlbGa?embed=payments',
['Authorization' => 'Bearer mykey'],
$this->callback(function ($headers) {
return $headers['Authorization'] == 'Bearer mykey';
}),
null
)->willReturn($clientResponse);

0 comments on commit c79dba6

Please sign in to comment.