Skip to content

Commit

Permalink
Merge pull request #38 from mundipagg/develop
Browse files Browse the repository at this point in the history
Merge develop into master
  • Loading branch information
LilianaLessa authored Jan 29, 2019
2 parents 8b5f6eb + 122eb74 commit 7752e1c
Show file tree
Hide file tree
Showing 5 changed files with 148 additions and 53 deletions.
137 changes: 97 additions & 40 deletions Concrete/Magento2DataService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Magento\Framework\App\ObjectManager;
use Mundipagg\Core\Kernel\Abstractions\AbstractDataService;
use Mundipagg\Core\Kernel\Aggregates\Order;
use Mundipagg\Core\Kernel\ValueObjects\Id\ChargeId;

class Magento2DataService extends AbstractDataService
{
Expand All @@ -17,59 +18,115 @@ public function updateAcquirerData(Order $order)
$lastTransId = $platformOrder->getPayment()->getLastTransId();
$paymentId = $platformOrder->getPayment()->getEntityId();
$orderId = $platformOrder->getPayment()->getParentId();
$transaction = $transactionRepository->getByTransactionId(
$lastTransId,

$transactionAuth = $transactionRepository->getByTransactionId(
str_replace('-capture', '', $lastTransId),
$paymentId,
$orderId
);
if ($transaction !== false) {

$orderCreationResponse =
$transaction->getAdditionalInformation('mundipagg_payment_module_api_response');

if ($orderCreationResponse === null) {
return;
}
$transactionCapture = $transactionRepository->getByTransactionId(
$lastTransId,
$paymentId,
$orderId
);

$orderCreationResponse = json_decode($orderCreationResponse);
//to prevent overwriting auth transaction
if ($transactionAuth->getTxnId() === $transactionCapture->getTxnId())
{
return;
}

$responseCharges = $orderCreationResponse->charges;
if ($transactionAuth !== false) {
$currentCharges = $order->getCharges();

foreach($currentCharges as $charge) {
$outdatedCharge = null;
foreach ($responseCharges as $responseCharge)
{
if ($responseCharge->id === $charge->getMundipaggId()->getValue()) {
$outdatedCharge = $responseCharge;
}
}
if ($outdatedCharge === null) {
$baseKey = $this->getChargeBaseKey($transactionAuth, $charge);
if ($baseKey === null) {
continue;
}

$additionalInformation = $transaction->getAdditionalInformation();
$lastTransaction = $charge->getLastTransaction();
foreach ($additionalInformation as $key => $value) {
if (
strpos($key, 'acquirer_nsu') !== false &&
$value === $outdatedCharge->last_transaction->acquirer_nsu
) {
$baseKey = str_replace('_acquirer_nsu', '', $key);
$transaction->setAdditionalInformation(
$baseKey . '_acquirer_nsu',
$lastTransaction->getAcquirerNsu()
);
//@fixme is it necessary update other info besides nsu?
/*$transaction->setAdditionalInformation(
$baseKey . '_acquirer_tid',
$lastTransaction->getAcquirerTid()
);*/
$transaction->save();
break;
}
}
$lastMundipaggTransaction = $charge->getLastTransaction();

$transactionCapture->setAdditionalInformation(
$baseKey . '_acquirer_nsu',
$lastMundipaggTransaction->getAcquirerNsu()
);

$transactionCapture->setAdditionalInformation(
$baseKey . '_acquirer_tid',
$lastMundipaggTransaction->getAcquirerTid()
);

$transactionCapture->setAdditionalInformation(
$baseKey . '_acquirer_auth_code',
$lastMundipaggTransaction->getAcquirerAuthCode()
);

$transactionCapture->setAdditionalInformation(
$baseKey . '_acquirer_name',
$lastMundipaggTransaction->getAcquirerName()
);

$transactionCapture->setAdditionalInformation(
$baseKey . '_acquirer_message',
$lastMundipaggTransaction->getAcquirerMessage()
);

$transactionCapture->setAdditionalInformation(
$baseKey . '_brand',
$lastMundipaggTransaction->getBrand()
);

$transactionCapture->setAdditionalInformation(
$baseKey . '_installments',
$lastMundipaggTransaction->getInstallments()
);
}

$transactionCapture->save();
}
}

private function getChargeBaseKey($transactionAuth, $charge)
{
$orderCreationResponse =
$transactionAuth->getAdditionalInformation('mundipagg_payment_module_api_response');

if ($orderCreationResponse === null) {
return null;
}

$orderCreationResponse = json_decode($orderCreationResponse);

$authCharges = $orderCreationResponse->charges;

$outdatedCharge = null;
foreach ($authCharges as $authCharge) {
if ($charge->getMundipaggId()->equals(new ChargeId($authCharge->id)))
{
$outdatedCharge = $authCharge;
}
}

if ($outdatedCharge === null) {
return null;
}

try {
//if it have no nsu, then it isn't a credit_card transaction;
$lastNsu = $outdatedCharge->last_transaction->acquirer_nsu;
}catch (\Throwable $e) {
return null;
}

$additionalInformation = $transactionAuth->getAdditionalInformation();
foreach ($additionalInformation as $key => $value) {
if ($value == $lastNsu) {
return str_replace('_acquirer_nsu', '', $key);
}
}

return null;
}
}
57 changes: 47 additions & 10 deletions Concrete/Magento2PlatformInvoiceDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
use Mundipagg\Core\Kernel\Abstractions\AbstractInvoiceDecorator;
use Mundipagg\Core\Kernel\Interfaces\PlatformOrderInterface;
use Mundipagg\Core\Kernel\ValueObjects\InvoiceState;
use MundiPagg\MundiPagg\Observer\SalesOrderPlaceAfter;
use Magento\Sales\Model\Service\InvoiceService;
use Magento\Sales\Model\Order\Email\Sender\InvoiceSender;
use Magento\Framework\DB\Transaction;


class Magento2PlatformInvoiceDecorator extends AbstractInvoiceDecorator
Expand Down Expand Up @@ -36,15 +38,7 @@ public function prepareFor(PlatformOrderInterface $order)

public function createFor(PlatformOrderInterface $order)
{
$objectManager = ObjectManager::getInstance();
/**
* @var SalesOrderPlaceAfter $observer;
*/
$observer = $objectManager->get(SalesOrderPlaceAfter::class);
$observer->createInvoice($order->getPlatformOrder());

$this->platformInvoice =
$order->getPlatformOrder()->getPayment()->getCreatedInvoice();
$this->platformInvoice = $this->createInvoice($order->getPlatformOrder());

return;

Expand Down Expand Up @@ -82,4 +76,47 @@ public function isCanceled()
{
return $this->platformInvoice->isCanceled();
}

private function createInvoice($order)
{
$objectManager = ObjectManager::getInstance();
$invoiceService = $objectManager->get(InvoiceService::class);
$transaction = $objectManager->get(Transaction::class);
$invoiceSender = $objectManager->get(InvoiceSender::class);

$invoice = $invoiceService->prepareInvoice($order);
$invoice->setRequestedCaptureCase(\Magento\Sales\Model\Order\Invoice::CAPTURE_OFFLINE);
$invoice->register();
$invoice->save();
$transactionSave = $transaction->addObject(
$invoice
)->addObject(
$invoice->getOrder()
);
$transactionSave->save();

//Remove comments if you need to send e-mail here until we don't create
//a class for e-mail sending.
//$invoiceSender->send($invoice);

$order->addStatusHistoryComment(
'MP - ' .
__('Notified customer about invoice #%1.', $invoice->getIncrementId())
)
->setIsCustomerNotified(true)
->save();

$payment = $order->getPayment();
$payment
->setIsTransactionClosed(true)
->registerCaptureNotification(
$order->getGrandTotal(),
true
);

$order->setState('processing')->setStatus('processing');
$order->save();

return $invoice;
}
}
1 change: 1 addition & 0 deletions Observer/SalesOrderPlaceAfter.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ public function createInvoice($order)
$invoice = $payment->getCreatedInvoice();
if ($invoice && !$order->getEmailSent()) {
$order->addStatusHistoryComment(
'MP - ' .
__(
'Notified customer about invoice #%1.',
$invoice->getIncrementId()
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "mundipagg/mundipagg-magento2-module",
"license": "MIT",
"version": "1.4.4",
"version": "1.4.5",
"type": "magento2-module",
"description": "Magento 2 Module Mundipagg",
"require": {
"php": ">=5.6.0",
"mundipagg/mundiapi": "^3.0",
"mundipagg/ecommerce-module-core": "1.2.1"
"mundipagg/ecommerce-module-core": "1.2.2"
},
"require-dev": {
"phpunit/phpunit": "4.1.0",
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="MundiPagg_MundiPagg" setup_version="1.4.4">
<module name="MundiPagg_MundiPagg" setup_version="1.4.5">
<sequence>
<module name="Magento_Sales" />
<module name="Magento_Payment" />
Expand Down

0 comments on commit 7752e1c

Please sign in to comment.