Skip to content

Commit

Permalink
Merge pull request #4 from magento-mpi/MAGETWO-42105
Browse files Browse the repository at this point in the history
[MPI] MAGETWO-42105: Creation of m2-sample-payment-provider (re-engineering)
  • Loading branch information
dkvashninbay committed Feb 3, 2016
2 parents 07c4037 + b0fad72 commit 635485d
Show file tree
Hide file tree
Showing 44 changed files with 2,316 additions and 3 deletions.
5 changes: 2 additions & 3 deletions sample-bundle-all/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@
"magento/sample-module-service-contract-client": "~1.0",
"magento/sample-module-webapi-client": "~1.0",
"magento/sample-module-service-contract-replacement": "~1.0",
"magento/sample-module-shipping-provider": "~1.0",
"magento/sample-module-payment-provider": "~1.0",
"magento/sample-module-servicecontract-new": "~1.0",
"magento/sample-module-command": "~1.0",
"magento/sample-module-custom-deployment-config": "~1.0",
"magento/module-sample-scss": "~1.0",
"magento/sample-module-form-uicomponent": "~1.0"
"magento/sample-module-form-uicomponent": "~1.0",
"magento/module-sample-payment-gateway": "~1.0"
}
}
40 changes: 40 additions & 0 deletions sample-module-payment-gateway/Block/Info.php
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 sample-module-payment-gateway/Gateway/Http/Client/ClientMock.php
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 sample-module-payment-gateway/Gateway/Http/TransferFactory.php
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();
}
}
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 sample-module-payment-gateway/Gateway/Request/CaptureRequest.php
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 sample-module-payment-gateway/Gateway/Request/MockDataRequest.php
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
];
}
}
Loading

0 comments on commit 635485d

Please sign in to comment.