forked from magento/magento2-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from magento-mpi/MAGETWO-42105
[MPI] MAGETWO-42105: Creation of m2-sample-payment-provider (re-engineering)
- Loading branch information
Showing
44 changed files
with
2,316 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
/** | ||
* Copyright © 2015 Magento. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
namespace Magento\SamplePaymentGateway\Block; | ||
|
||
use Magento\Framework\Phrase; | ||
use Magento\Payment\Block\ConfigurableInfo; | ||
use Magento\SamplePaymentGateway\Gateway\Response\FraudHandler; | ||
|
||
class Info extends ConfigurableInfo | ||
{ | ||
/** | ||
* Returns label | ||
* | ||
* @param string $field | ||
* @return Phrase | ||
*/ | ||
protected function getLabel($field) | ||
{ | ||
return __($field); | ||
} | ||
|
||
/** | ||
* Returns value view | ||
* | ||
* @param string $field | ||
* @param string $value | ||
* @return string | Phrase | ||
*/ | ||
protected function getValueView($field, $value) | ||
{ | ||
switch ($field) { | ||
case FraudHandler::FRAUD_MSG_LIST: | ||
return implode('; ', $value); | ||
} | ||
return parent::getValueView($field, $value); | ||
} | ||
} |
125 changes: 125 additions & 0 deletions
125
sample-module-payment-gateway/Gateway/Http/Client/ClientMock.php
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,125 @@ | ||
<?php | ||
/** | ||
* Copyright © 2015 Magento. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
namespace Magento\SamplePaymentGateway\Gateway\Http\Client; | ||
|
||
use Magento\Payment\Gateway\Http\ClientInterface; | ||
use Magento\Payment\Gateway\Http\TransferInterface; | ||
use Magento\Payment\Model\Method\Logger; | ||
|
||
class ClientMock implements ClientInterface | ||
{ | ||
const SUCCESS = 1; | ||
const FAILURE = 0; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
private $results = [ | ||
self::SUCCESS, | ||
self::FAILURE | ||
]; | ||
|
||
/** | ||
* @var Logger | ||
*/ | ||
private $logger; | ||
|
||
/** | ||
* @param Logger $logger | ||
*/ | ||
public function __construct( | ||
Logger $logger | ||
) { | ||
$this->logger = $logger; | ||
} | ||
|
||
/** | ||
* Places request to gateway. Returns result as ENV array | ||
* | ||
* @param TransferInterface $transferObject | ||
* @return array | ||
*/ | ||
public function placeRequest(TransferInterface $transferObject) | ||
{ | ||
$response = $this->generateResponseForCode( | ||
$this->getResultCode( | ||
$transferObject | ||
) | ||
); | ||
|
||
$this->logger->debug( | ||
[ | ||
'request' => $transferObject->getBody(), | ||
'response' => $response | ||
] | ||
); | ||
|
||
return $response; | ||
} | ||
|
||
/** | ||
* Generates response | ||
* | ||
* @return array | ||
*/ | ||
protected function generateResponseForCode($resultCode) | ||
{ | ||
|
||
return array_merge( | ||
[ | ||
'RESULT_CODE' => $resultCode, | ||
'TXN_ID' => $this->generateTxnId() | ||
], | ||
$this->getFieldsBasedOnResponseType($resultCode) | ||
); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
protected function generateTxnId() | ||
{ | ||
return md5(mt_rand(0, 1000)); | ||
} | ||
|
||
/** | ||
* Returns result code | ||
* | ||
* @param TransferInterface $transfer | ||
* @return int | ||
*/ | ||
private function getResultCode(TransferInterface $transfer) | ||
{ | ||
$headers = $transfer->getHeaders(); | ||
|
||
if (isset($headers['force_result'])) { | ||
return (int)$headers['force_result']; | ||
} | ||
|
||
return $this->results[mt_rand(0, 1)]; | ||
} | ||
|
||
/** | ||
* Returns response fields for result code | ||
* | ||
* @param int $resultCode | ||
* @return array | ||
*/ | ||
private function getFieldsBasedOnResponseType($resultCode) | ||
{ | ||
switch ($resultCode) { | ||
case self::FAILURE: | ||
return [ | ||
'FRAUD_MSG_LIST' => [ | ||
'Stolen card', | ||
'Customer location differs' | ||
] | ||
]; | ||
} | ||
|
||
return []; | ||
} | ||
} |
49 changes: 49 additions & 0 deletions
49
sample-module-payment-gateway/Gateway/Http/TransferFactory.php
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,49 @@ | ||
<?php | ||
/** | ||
* Copyright © 2015 Magento. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
namespace Magento\SamplePaymentGateway\Gateway\Http; | ||
|
||
use Magento\Payment\Gateway\Http\TransferBuilder; | ||
use Magento\Payment\Gateway\Http\TransferFactoryInterface; | ||
use Magento\Payment\Gateway\Http\TransferInterface; | ||
use Magento\SamplePaymentGateway\Gateway\Request\MockDataRequest; | ||
|
||
class TransferFactory implements TransferFactoryInterface | ||
{ | ||
/** | ||
* @var TransferBuilder | ||
*/ | ||
private $transferBuilder; | ||
|
||
/** | ||
* @param TransferBuilder $transferBuilder | ||
*/ | ||
public function __construct( | ||
TransferBuilder $transferBuilder | ||
) { | ||
$this->transferBuilder = $transferBuilder; | ||
} | ||
|
||
/** | ||
* Builds gateway transfer object | ||
* | ||
* @param array $request | ||
* @return TransferInterface | ||
*/ | ||
public function create(array $request) | ||
{ | ||
return $this->transferBuilder | ||
->setBody($request) | ||
->setMethod('POST') | ||
->setHeaders( | ||
[ | ||
'force_result' => isset($request[MockDataRequest::FORCE_RESULT]) | ||
? $request[MockDataRequest::FORCE_RESULT] | ||
: null | ||
] | ||
) | ||
->build(); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
sample-module-payment-gateway/Gateway/Request/AuthorizationRequest.php
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,59 @@ | ||
<?php | ||
/** | ||
* Copyright © 2015 Magento. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
namespace Magento\SamplePaymentGateway\Gateway\Request; | ||
|
||
use Magento\Payment\Gateway\ConfigInterface; | ||
use Magento\Payment\Gateway\Data\PaymentDataObjectInterface; | ||
use Magento\Payment\Gateway\Request\BuilderInterface; | ||
|
||
class AuthorizationRequest implements BuilderInterface | ||
{ | ||
/** | ||
* @var ConfigInterface | ||
*/ | ||
private $config; | ||
|
||
/** | ||
* @param ConfigInterface $config | ||
*/ | ||
public function __construct( | ||
ConfigInterface $config | ||
) { | ||
$this->config = $config; | ||
} | ||
|
||
/** | ||
* Builds ENV request | ||
* | ||
* @param array $buildSubject | ||
* @return array | ||
*/ | ||
public function build(array $buildSubject) | ||
{ | ||
if (!isset($buildSubject['payment']) | ||
|| !$buildSubject['payment'] instanceof PaymentDataObjectInterface | ||
) { | ||
throw new \InvalidArgumentException('Payment data object should be provided'); | ||
} | ||
|
||
/** @var PaymentDataObjectInterface $payment */ | ||
$payment = $buildSubject['payment']; | ||
$order = $payment->getOrder(); | ||
$address = $order->getShippingAddress(); | ||
|
||
return [ | ||
'TXN_TYPE' => 'A', | ||
'INVOICE' => $order->getOrderIncrementId(), | ||
'AMOUNT' => $order->getGrandTotalAmount(), | ||
'CURRENCY' => $order->getCurrencyCode(), | ||
'EMAIL' => $address->getEmail(), | ||
'MERCHANT_KEY' => $this->config->getValue( | ||
'merchant_gateway_key', | ||
$order->getStoreId() | ||
) | ||
]; | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
sample-module-payment-gateway/Gateway/Request/CaptureRequest.php
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,63 @@ | ||
<?php | ||
/** | ||
* Copyright © 2015 Magento. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
namespace Magento\SamplePaymentGateway\Gateway\Request; | ||
|
||
use Magento\Payment\Gateway\ConfigInterface; | ||
use Magento\Payment\Gateway\Data\PaymentDataObjectInterface; | ||
use Magento\Payment\Gateway\Request\BuilderInterface; | ||
use Magento\Sales\Api\Data\OrderPaymentInterface; | ||
|
||
class CaptureRequest implements BuilderInterface | ||
{ | ||
/** | ||
* @var ConfigInterface | ||
*/ | ||
private $config; | ||
|
||
/** | ||
* @param ConfigInterface $config | ||
*/ | ||
public function __construct( | ||
ConfigInterface $config | ||
) { | ||
$this->config = $config; | ||
} | ||
|
||
/** | ||
* Builds ENV request | ||
* | ||
* @param array $buildSubject | ||
* @return array | ||
*/ | ||
public function build(array $buildSubject) | ||
{ | ||
if (!isset($buildSubject['payment']) | ||
|| !$buildSubject['payment'] instanceof PaymentDataObjectInterface | ||
) { | ||
throw new \InvalidArgumentException('Payment data object should be provided'); | ||
} | ||
|
||
/** @var PaymentDataObjectInterface $paymentDO */ | ||
$paymentDO = $buildSubject['payment']; | ||
|
||
$order = $paymentDO->getOrder(); | ||
|
||
$payment = $paymentDO->getPayment(); | ||
|
||
if (!$payment instanceof OrderPaymentInterface) { | ||
throw new \LogicException('Order payment should be provided.'); | ||
} | ||
|
||
return [ | ||
'TXN_TYPE' => 'S', | ||
'TXN_ID' => $payment->getLastTransId(), | ||
'MERCHANT_KEY' => $this->config->getValue( | ||
'merchant_gateway_key', | ||
$order->getStoreId() | ||
) | ||
]; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
sample-module-payment-gateway/Gateway/Request/MockDataRequest.php
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,41 @@ | ||
<?php | ||
/** | ||
* Copyright © 2015 Magento. All rights reserved. | ||
* See COPYING.txt for license details. | ||
*/ | ||
namespace Magento\SamplePaymentGateway\Gateway\Request; | ||
|
||
use Magento\Payment\Gateway\Data\PaymentDataObjectInterface; | ||
use Magento\Payment\Gateway\Request\BuilderInterface; | ||
use Magento\SamplePaymentGateway\Gateway\Http\Client\ClientMock; | ||
|
||
class MockDataRequest implements BuilderInterface | ||
{ | ||
const FORCE_RESULT = 'FORCE_RESULT'; | ||
|
||
/** | ||
* Builds ENV request | ||
* | ||
* @param array $buildSubject | ||
* @return array | ||
*/ | ||
public function build(array $buildSubject) | ||
{ | ||
if (!isset($buildSubject['payment']) | ||
|| !$buildSubject['payment'] instanceof PaymentDataObjectInterface | ||
) { | ||
throw new \InvalidArgumentException('Payment data object should be provided'); | ||
} | ||
|
||
/** @var PaymentDataObjectInterface $paymentDO */ | ||
$paymentDO = $buildSubject['payment']; | ||
$payment = $paymentDO->getPayment(); | ||
|
||
$transactionResult = $payment->getAdditionalInformation('transaction_result'); | ||
return [ | ||
self::FORCE_RESULT => $transactionResult === null | ||
? ClientMock::SUCCESS | ||
: $transactionResult | ||
]; | ||
} | ||
} |
Oops, something went wrong.