Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Block/Checkout/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Error extends Template
protected $request;

public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
Template\Context $context,
Http $request,
array $data = []
) {
Expand Down
2 changes: 1 addition & 1 deletion Block/Checkout/Success.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Success extends Template
protected $request;

public function __construct(
\Magento\Framework\View\Element\Template\Context $context,
Template\Context $context,
Http $request,
array $data = []
) {
Expand Down
3 changes: 2 additions & 1 deletion Controller/Checkout/Bnpl.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Fintecture\Payment\Controller\Checkout;

use Fintecture\Payment\Controller\FintectureAbstract;
use Fintecture\Payment\Helper\Fintecture;

class Bnpl extends FintectureAbstract
{
Expand All @@ -21,7 +22,7 @@ public function execute()
}

// Connect
$data = $this->fintectureHelper->generatePayload($order, self::BNPL_TYPE);
$data = $this->fintectureHelper->generatePayload($order, Fintecture::BNPL_TYPE);
$apiResponse = $this->connect->get($order, $data);
$url = $apiResponse->meta->url ?? '';

Expand Down
5 changes: 3 additions & 2 deletions Controller/Checkout/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Fintecture\Payment\Controller\Checkout;

use Fintecture\Payment\Controller\FintectureAbstract;
use Fintecture\Payment\Helper\Fintecture;
use Magento\Framework\App\ObjectManager;
use Magento\LoginAsCustomerApi\Api\GetLoggedAsCustomerAdminIdInterface;
use Magento\Sales\Model\Order;
Expand All @@ -26,7 +27,7 @@ public function execute()
$alternativeMethod = $this->getAlternativeMethod();
if (!$alternativeMethod) {
// Connect
$data = $this->fintectureHelper->generatePayload($order, self::PIS_TYPE);
$data = $this->fintectureHelper->generatePayload($order, Fintecture::PIS_TYPE);
$apiResponse = $this->connect->get($order, $data);
$url = $apiResponse->meta->url ?? '';
} else {
Expand Down Expand Up @@ -58,7 +59,7 @@ public function execute()

private function getQRCodeRedirect(Order $order): string
{
$data = $this->fintectureHelper->generatePayload($order, self::RTP_TYPE);
$data = $this->fintectureHelper->generatePayload($order, Fintecture::RTP_TYPE);
$apiResponse = $this->requestToPay->get($order, $data);
$url = $apiResponse->meta->url ?? '';

Expand Down
4 changes: 0 additions & 4 deletions Controller/FintectureAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,6 @@ abstract class FintectureAbstract implements CsrfAwareActionInterface
/** @var OrderRepositoryInterface */
protected $orderRepository;

public const PIS_TYPE = 'PayByBank';
public const RTP_TYPE = 'RequestToPay';
public const BNPL_TYPE = 'BuyNowPayLater';

public function __construct(
CheckoutSession $checkoutSession,
Logger $fintectureLogger,
Expand Down
43 changes: 27 additions & 16 deletions Controller/Standard/PaymentCreated.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public function execute()
'state' => $params['state'],
'status' => $params['status'],
'sessionId' => $sessionId,
'isRefund' => $isRefund ? 'true' : 'false',
]);
$result->setContents('invalid_order');

Expand All @@ -63,18 +64,24 @@ public function execute()

try {
if ($isRefund) {
$decodedState = json_decode(base64_decode($params['state']));
if (property_exists($decodedState, 'creditmemo_transaction_id')) {
return $this->refund($order, $params['status'], $decodedState->creditmemo_transaction_id);
} else {
$this->fintectureLogger->error('Webhook', [
'message' => 'No credit memo id found',
'state' => $params['state'],
'status' => $params['status'],
'sessionId' => $params['sessionId'],
]);
$result->setContents('invalid_refund');
if ((float) $order->getData('fintecture_payment_refund_amount') === (float) $order->getBaseGrandTotal()) {
$result->setContents('order_already_refunded');

return $result;
}

$decodedState = base64_decode($params['state']);
if ($decodedState) {
$decodedStateObj = json_decode($decodedState);
if ($decodedStateObj) {
if (property_exists($decodedState, 'creditmemo_transaction_id')) {
// Use existing credit memo
return $this->refund($order, $params['status'], (float) $params['amount'], $decodedStateObj->creditmemo_transaction_id);
}
}
}

return $this->refund($order, $params['status'], (float) $params['amount']);
} else {
return $this->payment($order, $params);
}
Expand All @@ -95,7 +102,6 @@ private function payment(Order $order, array $params): Raw
{
$result = $this->resultRawFactory->create();
$result->setHeader('Content-Type', 'text/plain');
$result->setHttpResponseCode(200);

$statuses = $this->fintectureHelper->getOrderStatus($params);
if (!$statuses) {
Expand Down Expand Up @@ -173,16 +179,21 @@ private function payment(Order $order, array $params): Raw
return $result;
}

private function refund(Order $order, string $status, string $creditmemoTransactionId): Raw
private function refund(Order $order, string $status, float $amount, string $creditmemoTransactionId = null): Raw
{
$result = $this->resultRawFactory->create();
$result->setHeader('Content-Type', 'text/plain');

if ($status === 'payment_created') {
$appliedRefund = $this->handleRefund->apply($order, $creditmemoTransactionId);
if ($appliedRefund) {
$result->setHttpResponseCode(200);
if (!is_null($creditmemoTransactionId)) {
$creditmemo = $this->fintectureHelper->getCreditmemoByTransactionId($order, $creditmemoTransactionId);

$appliedRefund = $creditmemo ? $this->handleRefund->apply($order, $creditmemo, $amount) : false;
} else {
$appliedRefund = $this->handleRefund->applyWithoutCreditmemo($order, $amount);
}

if (!$appliedRefund) {
$result->setHttpResponseCode(400);
$result->setContents('refund_not_applied');
}
Expand Down
3 changes: 2 additions & 1 deletion Controller/Standard/Send.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use chillerlan\QRCode\QRCode as QRCodeGenerator;
use Fintecture\Payment\Controller\FintectureAbstract;
use Fintecture\Payment\Helper\Fintecture;
use Magento\Framework\View\Element\Template;

class Send extends FintectureAbstract
Expand Down Expand Up @@ -33,7 +34,7 @@ public function execute()
throw new \Exception('Send error: no order found');
}

$data = $this->fintectureHelper->generatePayload($order, self::RTP_TYPE, $method);
$data = $this->fintectureHelper->generatePayload($order, Fintecture::RTP_TYPE, $method);
$apiResponse = $this->requestToPay->get($order, $data);

$reference = $data['data']['attributes']['communication'];
Expand Down
18 changes: 17 additions & 1 deletion Gateway/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class Config extends BaseConfig
{
const CODE = 'fintecture';
const VERSION = '3.4.2';
const VERSION = '3.5.0';

const KEY_SHOP_NAME = 'general/store_information/name';
const KEY_ACTIVE = 'active';
Expand All @@ -21,6 +21,7 @@ class Config extends BaseConfig
const KEY_PRIVATE_KEY_SANDBOX = 'custom_file_upload_sandbox';
const KEY_PRIVATE_KEY_PRODUCTION = 'custom_file_upload_production';
const KEY_BANK_TYPE = 'general/bank_type';
const KEY_REFUND_STATUSES_ACTIVE = 'refund_statuses_active';
const KEY_EXPIRATION_ACTIVE = 'expiration_active';
const KEY_EXPIRATION_AFTER = 'expiration_after';
const KEY_INVOICING_ACTIVE = 'invoicing_active';
Expand Down Expand Up @@ -110,6 +111,11 @@ public function getBankType(): ?string
return $this->getValue(self::KEY_BANK_TYPE);
}

public function isRefundStatusesActive(): bool
{
return (bool) $this->getValue(self::KEY_REFUND_STATUSES_ACTIVE);
}

public function isExpirationActive(): bool
{
return (bool) $this->getValue(self::KEY_EXPIRATION_ACTIVE);
Expand Down Expand Up @@ -234,4 +240,14 @@ public function getPaymentFailedStatus(): string

return $status;
}

public function getPartialRefundStatus(): string
{
$status = $this->getValue('payment/fintecture/partial_refund_status');
if (!$status) {
$status = 'fintecture_partial_refund';
}

return $status;
}
}
40 changes: 38 additions & 2 deletions Gateway/HandlePayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Fintecture\Payment\Gateway;

use Fintecture\Payment\Gateway\Config\Config;
use Fintecture\Payment\Gateway\Http\Sdk;
use Fintecture\Payment\Helper\Fintecture as FintectureHelper;
use Fintecture\Payment\Logger\Logger;
use Magento\Framework\DB\Transaction;
Expand Down Expand Up @@ -60,6 +61,9 @@ class HandlePayment
/** @var InvoiceRepositoryInterface */
protected $invoiceRepository;

/** @var Sdk */
protected $sdk;

public function __construct(
Logger $fintectureLogger,
Config $config,
Expand All @@ -73,7 +77,8 @@ public function __construct(
OrderSender $orderSender,
InvoiceSender $invoiceSender,
InvoiceService $invoiceService,
InvoiceRepositoryInterface $invoiceRepository
InvoiceRepositoryInterface $invoiceRepository,
Sdk $sdk
) {
$this->fintectureLogger = $fintectureLogger;
$this->config = $config;
Expand All @@ -88,6 +93,7 @@ public function __construct(
$this->invoiceService = $invoiceService;
$this->invoiceSender = $invoiceSender;
$this->invoiceRepository = $invoiceRepository;
$this->sdk = $sdk;
}

public function create(
Expand Down Expand Up @@ -140,7 +146,7 @@ public function create(
->setOrder($order)
->setTransactionId($order->getIncrementId() . '-' . time())
->setAdditionalInformation([
\Magento\Sales\Model\Order\Payment\Transaction::RAW_DETAILS => [
Payment\Transaction::RAW_DETAILS => [
'amount' => (string) $lastTransactionAmount . ' €',
'status' => $params['status'],
'sessionId' => $params['sessionId'],
Expand Down Expand Up @@ -204,6 +210,7 @@ public function sendInvoice(Order $order, array $params): void
$invoice = $this->invoiceService->prepareInvoice($order);
$invoice->setTransactionId($params['sessionId']);
$invoice->register();
$invoice->pay();
$this->invoiceRepository->save($invoice);
$transactionSave = $this->transaction
->addObject($invoice)
Expand All @@ -214,6 +221,35 @@ public function sendInvoice(Order $order, array $params): void

$order->setIsCustomerNotified(true);
$this->orderRepository->save($order);

if ($params['status'] === 'order_created') {
// Update invoice_reference field for BNPL orders
$data = [
'data' => [
'attributes' => [
'invoice_reference' => '#' . $invoice->getIncrementId(),
],
],
];

$pisToken = $this->sdk->pisClient->token->generate();
if (!$pisToken->error) {
$this->sdk->pisClient->setAccessToken($pisToken); // set token of PIS client
} else {
$this->fintectureLogger->error("Can't update invoice_reference field", [
'message' => $pisToken->errorMsg,
'incrementOrderId' => $order->getIncrementId(),
]);
}

$apiResponse = $this->sdk->pisClient->payment->update($params['sessionId'], $data);
if ($apiResponse->error) {
$this->fintectureLogger->error("Can't update invoice_reference field", [
'message' => $apiResponse->errorMsg,
'incrementOrderId' => $order->getIncrementId(),
]);
}
}
}
}

Expand Down
Loading