Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add CoinGate * fix name * small fix * add callback URL * fix callback url name * change author * fix Ikajotest; init coingate tests
- Loading branch information
Showing
4 changed files
with
152 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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())); | ||
} | ||
} |
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,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() | ||
{ | ||
} | ||
} |
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