Skip to content

Commit

Permalink
add CoinGate (#3)
Browse files Browse the repository at this point in the history
* add CoinGate

* fix name

* small fix

* add callback URL

* fix callback url name

* change author

* fix Ikajotest; init coingate tests
  • Loading branch information
bladeroot authored and hiqsol committed Apr 2, 2019
1 parent 5b03503 commit 3befdde
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 1 deletion.
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -61,6 +61,7 @@
"hiqdev/omnipay-yandexmoney": "dev-master",
"hiqdev/omnipay-epayments": "dev-master",
"hiqdev/omnipay-ikajo": "2.0.x-dev",
"hiqdev/omnipay-coingate": "dev-master",
"dercoder/omnipay-webmoney": "dev-master",
"collizo4sky/omnipay-2checkout": "^1.6",
"hiqdev/hidev-php": "dev-master",
Expand Down
76 changes: 76 additions & 0 deletions src/merchants/coingate/CoinGateMerchant.php
@@ -0,0 +1,76 @@
<?php

namespace hiqdev\php\merchant\merchants\coingate;

use hiqdev\php\merchant\InvoiceInterface;
use hiqdev\php\merchant\merchants\AbstractMerchant;
use hiqdev\php\merchant\response\CompletePurchaseResponse;
use hiqdev\php\merchant\response\RedirectPurchaseResponse;
use Omnipay\CoinGate\Gateway;

/**
* Class CoinGateMerchant.
*
* @author Yurii Myronchuk <bladeroot@gmail.com>
*/
class CoinGateMerchant extends AbstractMerchant
{
/**
* @var Gateway
*/
protected $gateway;

protected function createGateway()
{
return $this->gatewayFactory->build('CoinGate', [
'apiKey' => $this->credentials->getKey1(),
]);
}

/**
* @param InvoiceInterface $invoice
* @return RedirectPurchaseResponse
*/
public function requestPurchase(InvoiceInterface $invoice)
{
/**
* @var \Omnipay\CoinGate\Message\PurchaseResponse $response
*/
$response = $this->gateway->purchase([
'transactionId' => $invoice->getId(),
'currency' => $invoice->getCurrency()->getCode(),
'description' => $invoice->getDescription(),
'amount' => $this->moneyFormatter->format($invoice->getAmount()),
'returnUrl' => $invoice->getReturnUrl(),
'cancelUrl' => $invoice->getCancelUrl(),
'notifyUrl' => $invoice->getNotifyUrl(),
])->send();

if ($response->getRedirectUrl() === null) {
throw new MerchantException('Failed to request purchase');
}

$response = new RedirectPurchaseResponse($response->getRedirectUrl(), $response->getRedirectData());
$response->setMethod('GET');

return $response;
}

/**
* @param array $data
* @return CompletePurchaseResponse
*/
public function completePurchase($data)
{
/** @var \Omnipay\CoinGate\Message\CompletePurchaseResponse $response */
$response = $this->gateway->completePurchase($data)->send();

return (new CompletePurchaseResponse())
->setIsSuccessful($response->isSuccessful())
->setAmount($this->moneyParser->parse($response->getAmount(), $response->getCurrency()))
->setTransactionReference($response->getTransactionReference())
->setTransactionId($response->getTransactionId())
->setPayer($response->getPayer())
->setTime(new \DateTime($response->getTime()));
}
}
74 changes: 74 additions & 0 deletions tests/unit/merchants/coingate/CoinGateMerchantTest.php
@@ -0,0 +1,74 @@
<?php

namespace hiqdev\php\merchant\tests\unit\merchants\coingate;

use Guzzle\Http\Client;
use Guzzle\Http\Message\Response;
use hiqdev\php\merchant\merchants\coingate\CoinGateMerchant;
use hiqdev\php\merchant\response\RedirectPurchaseResponse;
use hiqdev\php\merchant\tests\unit\merchants\AbstractMerchantTest;
use Money\Currency;
use Money\Money;

class IkajoMerchantTest extends AbstractMerchantTest
{
/** @var CoinGateMerchant */
protected $merchant;

protected function buildMerchant()
{
return new CoinGateMerchant(
$this->getCredentials(),
$this->getGatewayFactory(),
$this->getMoneyFormatter(),
$this->getMoneyParser()
);
}

public function testCredentialsWereMappedCorrectly()
{
$gatewayPropertyReflection = (new \ReflectionObject($this->merchant))->getProperty('gateway');
$gatewayPropertyReflection->setAccessible(true);
$gateway = $gatewayPropertyReflection->getValue($this->merchant);

$this->assertSame($this->getCredentials()->getKey1(), $gateway->getSecret());
}

public function testRequestPurchase()
{
$invoice = $this->buildInvoice();

$purchaseResponse = $this->merchant->requestPurchase($invoice);
$this->assertInstanceOf(RedirectPurchaseResponse::class, $purchaseResponse);
$this->assertSame('https://secure.payinspect.com/post', $purchaseResponse->getRedirectUrl());
$this->assertArraySubset([
'payment' => 'CC',
'url' => 'https://example.com/return',
'error_url' => 'https://example.com/cancel',
'sign' => '12dd7800b6517c1df5b6eee5efcef67b',
], $purchaseResponse->getRedirectData());
}

/**
* Used only for testCompletePurchase.
*/
protected function buildHttpClient()
{
return new class() extends Client {
public function send($requests)
{
return new Response(200, [], 'VERIFIED');
}
};
}

protected function getCredentials()
{
return parent::getCredentials()
->setKey1('q-oRs-HPzZyeJu8WzgoMTqkSuaDq-6RftTxNJHx8');
}

public function testCompletePurchase()
{
}
}
2 changes: 1 addition & 1 deletion tests/unit/merchants/ikajo/IkajoMerchantTest.php
@@ -1,6 +1,6 @@
<?php

namespace hiqdev\php\merchant\tests\unit\merchants\paxum;
namespace hiqdev\php\merchant\tests\unit\merchants\ikajo;

use Guzzle\Http\Client;
use Guzzle\Http\Message\Response;
Expand Down

0 comments on commit 3befdde

Please sign in to comment.