Skip to content

Commit

Permalink
Merge pull request #39 from compropago/jose
Browse files Browse the repository at this point in the history
Fix composer require and descriptions
  • Loading branch information
JoseBSX authored Mar 13, 2019
2 parents 1596aa2 + 4c95334 commit 8a2c714
Show file tree
Hide file tree
Showing 17 changed files with 176 additions and 247 deletions.
8 changes: 3 additions & 5 deletions Block/Checkout/Onepage/Succes/Receipt.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,16 @@ class Receipt extends \Magento\Checkout\Block\Onepage\Success
*/
public function getVars()
{
$_txnId = "";

$info = $this->getOrder()
->getPayment()
->getMethodInstance()
->getInfoInstance();

$info = $info->getAdditionalInformation("offline_info")
?
? null
: $info->getAdditionalInformation();

if (isset($info["ID"])) $_txnId = $info["ID"];
$_txnId = isset($info["ID"]) ? $info["ID"] : '';

return $_txnId;
}
Expand Down
14 changes: 5 additions & 9 deletions Block/Payment/Info.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* @author José Castañeda <[email protected]>
* @category Compropago
* @package Compropago\Magento2
* @copyright qbo (http://www.qbo.tech)
* @copyright qbo (http://www.qbo.tech)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*
* © 2017 QBO DIGITAL SOLUTIONS.
Expand Down Expand Up @@ -66,16 +66,12 @@ protected function _prepareSpecificInformation($transport = null)

if (
$this->_appState->getAreaCode() === FrontNameResolver::AREA_CODE
&& $info->getAdditionalInformation() )
{
foreach ($info->getAdditionalInformation() as $field => $value)
{
&& $info->getAdditionalInformation() ) {
foreach ($info->getAdditionalInformation() as $field => $value) {
$beautifiedFieldName = str_replace("_", " ", ucwords(trim(preg_replace('/(?<=\\w)(?=[A-Z])/', " $1", $field))));
if($field == "ID")
{
if($field == "ID") {
$data["ID"] = $value;
}
else if (!in_array($field, $this->_disallowedFiledNames))
} else if (!in_array($field, $this->_disallowedFiledNames))
{
$data[__($beautifiedFieldName)->getText()] = $value;
}
Expand Down
64 changes: 30 additions & 34 deletions Controller/Index/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
namespace Compropago\Magento2\Controller\Index;

use Compropago\Magento2\Model\Webhook;
//use CompropagoSdk\Factory\Factory;

use Magento\Framework\App\Action\Action;
use Magento\Framework\App\Action\Context;
Expand All @@ -43,17 +42,17 @@

class Index extends Action
{
const SUCESS_MESSAGE = "OK";
const INVALID_REQUEST_MESSAGE = "Invalid Request. Please verify request order info";
const SERVER_ERROR_MESSAGE = "Ups. An error occurred during server request processing.";
const SUCCESSFUL_TEST_MESSAGE = "OK";
const BAD_REQUEST_MESSAGE = '[Compropago Webhook] Please specify an Order ID or Payment Type.';
const SUCESS_STATUS = "success";
const ERROR_STATUS = "error";
const ERROR_CODE_KEY = "error_code";
const MESSAGE_KEY = "message";
const TEST_SHORT_ID = "000000";
const STREAM_BUFFER_NAME = "php://input";
const SUCESS_MESSAGE = "OK";
const INVALID_REQUEST_MESSAGE = "Invalid Request. Please verify request order info";
const SERVER_ERROR_MESSAGE = "Ups. An error occurred during server request processing.";
const SUCCESSFUL_TEST_MESSAGE = "OK";
const BAD_REQUEST_MESSAGE = '[Compropago Webhook] Please specify an Order ID or Payment Type.';
const SUCESS_STATUS = "success";
const ERROR_STATUS = "error";
const ERROR_CODE_KEY = "error_code";
const MESSAGE_KEY = "message";
const TEST_SHORT_ID = "000000";
const STREAM_BUFFER_NAME = "php://input";

/**
* @var DecoderInterface
Expand Down Expand Up @@ -87,6 +86,7 @@ class Index extends Action

/**
* Index constructor.
*
* @param Context $context
* @param JsonFactory $jsonResultFactory
* @param File $fileData
Expand Down Expand Up @@ -141,10 +141,10 @@ public function execute()
);

$this->jsonResponse->setData([
'status' => 'error',
'message' => $e->getMessage(),
'short_id' => null,
'reference' => $e->getCode()
'status' => 'error',
'message' => $e->getMessage(),
'short_id' => null,
'reference' => $e->getCode()
]);
$this->_logger->critical($e->getMessage());
}
Expand All @@ -167,23 +167,23 @@ protected function _processResult($result, $event)
);

$this->jsonResponse->setData([
'status' => self::SUCESS_STATUS,
'message' => self::SUCESS_MESSAGE,
'short_id' => $event["short_id"],
'reference' => $event["id"]
'status' => self::SUCESS_STATUS,
'message' => self::SUCESS_MESSAGE,
'short_id' => $event["short_id"],
'reference' => $event["id"]
]);

return $this->jsonResponse;
}

$this->jsonResponse->setHttpResponseCode(
$result[self::ERROR_CODE_KEY]
?
? null
: \Magento\Framework\Webapi\Exception::HTTP_BAD_REQUEST
);

$this->jsonResponse->setData([
'status' => self::ERROR_STATUS,
'status' => self::ERROR_STATUS,
'message' => $result[self::MESSAGE_KEY]
]);

Expand All @@ -205,8 +205,8 @@ protected function _validateRequest($event)
);

$this->jsonResponse->setData([
'status' => self::ERROR_STATUS,
'message' => __(self::INVALID_REQUEST_MESSAGE)
'status' => self::ERROR_STATUS,
'message' => __(self::INVALID_REQUEST_MESSAGE)
]);

return false;
Expand All @@ -217,14 +217,11 @@ protected function _validateRequest($event)
\Magento\Framework\Webapi\Exception::HTTP_BAD_REQUEST
);
$this->jsonResponse->setData([
'status' => self::ERROR_STATUS,
'message' => __(self::BAD_REQUEST_MESSAGE)
'status' => self::ERROR_STATUS,
'message' => __(self::BAD_REQUEST_MESSAGE)
]);
return false;
}

//$this->webhook = Factory::getInstanceOf('CpOrderInfo', $event);
//$this->webhook = false;

return true;
}
Expand All @@ -237,15 +234,14 @@ protected function _validateRequest($event)
protected function _getIsTest($event)
{
// Test Case
if ($event["short_id"] == self::TEST_SHORT_ID)
{
if ($event["short_id"] == self::TEST_SHORT_ID) {
$this->jsonResponse->setHttpResponseCode(
\Magento\Framework\App\Response\Http::STATUS_CODE_200
);
$this->jsonResponse->setData([
'status' => self::SUCESS_STATUS,
'message' => self::SUCCESSFUL_TEST_MESSAGE,
'short_id' => $event["short_id"],
'status' => self::SUCESS_STATUS,
'message' => self::SUCCESSFUL_TEST_MESSAGE,
'short_id' => $event["short_id"],
'reference' => $event["id"]
]);

Expand Down
55 changes: 19 additions & 36 deletions Model/Cash.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class Cash extends AbstractMethod
/**
* @var array
*/
public $_supportedCurrencyCodes = ['USD','MXN','GBP','EUR'];
public $_supportedCurrencyCodes = ['EUR', 'GBP', 'MXN', 'USD'];

/**
* Payment constructor.
Expand Down Expand Up @@ -180,20 +180,15 @@ public function assignData(DataObject $data)
{
parent::assignData($data);

if ($data->getData(self::PROVIDER_KEY_NAME))
{
if ($data->getData(self::PROVIDER_KEY_NAME)) {
$this->getInfoInstance()->setAdditionalInformation(
self::PROVIDER_KEY_NAME,
$data->getData(self::PROVIDER_KEY_NAME)
);
}
else
{
} else {
$additionalData = $data->getData(PaymentInterface::KEY_ADDITIONAL_DATA);
foreach ($additionalData as $key => $value)
{
if(!is_object($value))
{
foreach ($additionalData as $key => $value) {
if(!is_object($value)) {
$this->getInfoInstance()->setAdditionalInformation($key, $value);
}
}
Expand Down Expand Up @@ -226,23 +221,19 @@ public function authorize(InfoInterface $payment, $amount)
{
$this->_initialize();

try
{
try {
$result = $this->_executePayment(
$this->_getRequestInfo( $payment->getOrder() )
);

if (isset($result['success']))
{
if (isset($result['success'])) {
$this->_addTransactionInfo(
$payment,
$result
);
}

}
catch(\Exception $e)
{
} catch(\Exception $e) {
$this->_processErrors($e);
}

Expand All @@ -260,24 +251,17 @@ protected function _executePayment($_orderInfo)
{
$result = [];

try
{
try {
$response = $this->_apiClient->createOrder($_orderInfo);
if (isset($response['id']))
{
if (isset($response['id'])) {
$result = [
'success' => true,
'response' => $response
'success' => true,
'response' => $response
];
} else {
throw new \Magento\Framework\Validator\Exception(__('Old API version, Send mail to [email protected]'));
}
else
{
# Error old API version

}
}
catch(\Exception $e)
{
} catch(\Exception $e) {
$this->_processErrors($e);
}

Expand Down Expand Up @@ -385,9 +369,9 @@ protected function _processErrors($e)
if($e->getCode() === self::ERROR_CODE_STORE_NOT_FOUND)
{
throw new \Magento\Framework\Exception\LocalizedException(__($message));
}

throw new \Magento\Framework\Validator\Exception(__('Payment capturing error.'));
}
throw new \Magento\Framework\Validator\Exception(__($message));
}

/**
Expand All @@ -406,8 +390,7 @@ public function getCode()
*/
public function canUseForCurrency($currencyCode)
{
if (!in_array($currencyCode, $this->_supportedCurrencyCodes))
{
if (!in_array($currencyCode, $this->_supportedCurrencyCodes)) {
return false;
}

Expand Down
4 changes: 2 additions & 2 deletions Model/Config/Source/Order/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ public function toOptionArray()
'label' => __(self::UNDEFINED_OPTION_LABEL)
]
];
foreach ($statuses as $code => $label)
{

foreach ($statuses as $code => $label) {
$options[] = [
'value' => $code,
'label' => $label
Expand Down
60 changes: 28 additions & 32 deletions Model/Observers/WebhookRegister.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,36 +13,35 @@

class WebhookRegister implements ObserverInterface
{
private $messageManager;
private $storeManager;
private $config;
private $messageManager;
private $storeManager;
private $config;

/**
* WebhookRegister constructor.
* @param \Magento\Framework\Message\ManagerInterface $messageManager
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Compropago\Magento2\Model\Config $config
*/
public function __construct(
\Magento\Framework\Message\ManagerInterface $messageManager,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Compropago\Magento2\Model\Config $config)
{
$this->messageManager = $messageManager;
$this->storeManager = $storeManager;
$this->config = $config;
}
/**
* WebhookRegister constructor.
* @param \Magento\Framework\Message\ManagerInterface $messageManager
* @param \Magento\Store\Model\StoreManagerInterface $storeManager
* @param \Compropago\Magento2\Model\Config $config
*/
public function __construct(
\Magento\Framework\Message\ManagerInterface $messageManager,
\Magento\Store\Model\StoreManagerInterface $storeManager,
\Compropago\Magento2\Model\Config $config)
{
$this->messageManager = $messageManager;
$this->storeManager = $storeManager;
$this->config = $config;
}

/**
* Event for the observer
* @param Observer $observer
*/
public function execute(Observer $observer)
{
$webhook = $this->storeManager->getStore()->getBaseUrl() . "cpwebhook/";
/**
* Event for the observer
* @param Observer $observer
*/
public function execute(Observer $observer)
{
$webhook = $this->storeManager->getStore()->getBaseUrl() . "cpwebhook/";

try
{
try {
$objWebhook = (new sdkWebhook)->withKeys(
$this->config->getPublicKey(),
$this->config->getPrivateKey()
Expand All @@ -51,17 +50,14 @@ public function execute(Observer $observer)
# Create webhook on ComproPago Panel
$objWebhook->create( $webhook );
$this->messageManager->addSuccessMessage('Webhook ComproPago was updated.');
}
catch(\Exception $e)
{
} catch(\Exception $e) {
$errors = [
'ComproPago: Request Error [409]: ',
];
$message = json_encode(str_replace($errors, '', $e->getMessage()));

# Ignore Webhook registered
if ( isset($message['code']) && $message['code']==409 )
{
if ( isset($message['code']) && $message['code']==409 ) {
$this->messageManager->addError("ComproPago: {$message}");
}
}
Expand Down
Loading

0 comments on commit 8a2c714

Please sign in to comment.