Skip to content

Commit 8484a1e

Browse files
Merge pull request #88 from hugofintecture/3.5.0
3.5.0
2 parents e9d7995 + 7f590f6 commit 8484a1e

File tree

23 files changed

+669
-386
lines changed

23 files changed

+669
-386
lines changed

Block/Checkout/Error.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Error extends Template
1111
protected $request;
1212

1313
public function __construct(
14-
\Magento\Framework\View\Element\Template\Context $context,
14+
Template\Context $context,
1515
Http $request,
1616
array $data = []
1717
) {

Block/Checkout/Success.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Success extends Template
1111
protected $request;
1212

1313
public function __construct(
14-
\Magento\Framework\View\Element\Template\Context $context,
14+
Template\Context $context,
1515
Http $request,
1616
array $data = []
1717
) {

Controller/Checkout/Bnpl.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Fintecture\Payment\Controller\Checkout;
66

77
use Fintecture\Payment\Controller\FintectureAbstract;
8+
use Fintecture\Payment\Helper\Fintecture;
89

910
class Bnpl extends FintectureAbstract
1011
{
@@ -21,7 +22,7 @@ public function execute()
2122
}
2223

2324
// Connect
24-
$data = $this->fintectureHelper->generatePayload($order, self::BNPL_TYPE);
25+
$data = $this->fintectureHelper->generatePayload($order, Fintecture::BNPL_TYPE);
2526
$apiResponse = $this->connect->get($order, $data);
2627
$url = $apiResponse->meta->url ?? '';
2728

Controller/Checkout/Index.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Fintecture\Payment\Controller\Checkout;
66

77
use Fintecture\Payment\Controller\FintectureAbstract;
8+
use Fintecture\Payment\Helper\Fintecture;
89
use Magento\Framework\App\ObjectManager;
910
use Magento\LoginAsCustomerApi\Api\GetLoggedAsCustomerAdminIdInterface;
1011
use Magento\Sales\Model\Order;
@@ -26,7 +27,7 @@ public function execute()
2627
$alternativeMethod = $this->getAlternativeMethod();
2728
if (!$alternativeMethod) {
2829
// Connect
29-
$data = $this->fintectureHelper->generatePayload($order, self::PIS_TYPE);
30+
$data = $this->fintectureHelper->generatePayload($order, Fintecture::PIS_TYPE);
3031
$apiResponse = $this->connect->get($order, $data);
3132
$url = $apiResponse->meta->url ?? '';
3233
} else {
@@ -58,7 +59,7 @@ public function execute()
5859

5960
private function getQRCodeRedirect(Order $order): string
6061
{
61-
$data = $this->fintectureHelper->generatePayload($order, self::RTP_TYPE);
62+
$data = $this->fintectureHelper->generatePayload($order, Fintecture::RTP_TYPE);
6263
$apiResponse = $this->requestToPay->get($order, $data);
6364
$url = $apiResponse->meta->url ?? '';
6465

Controller/FintectureAbstract.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,6 @@ abstract class FintectureAbstract implements CsrfAwareActionInterface
8080
/** @var OrderRepositoryInterface */
8181
protected $orderRepository;
8282

83-
public const PIS_TYPE = 'PayByBank';
84-
public const RTP_TYPE = 'RequestToPay';
85-
public const BNPL_TYPE = 'BuyNowPayLater';
86-
8783
public function __construct(
8884
CheckoutSession $checkoutSession,
8985
Logger $fintectureLogger,

Controller/Standard/PaymentCreated.php

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public function execute()
5555
'state' => $params['state'],
5656
'status' => $params['status'],
5757
'sessionId' => $sessionId,
58+
'isRefund' => $isRefund ? 'true' : 'false',
5859
]);
5960
$result->setContents('invalid_order');
6061

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

6465
try {
6566
if ($isRefund) {
66-
$decodedState = json_decode(base64_decode($params['state']));
67-
if (property_exists($decodedState, 'creditmemo_transaction_id')) {
68-
return $this->refund($order, $params['status'], $decodedState->creditmemo_transaction_id);
69-
} else {
70-
$this->fintectureLogger->error('Webhook', [
71-
'message' => 'No credit memo id found',
72-
'state' => $params['state'],
73-
'status' => $params['status'],
74-
'sessionId' => $params['sessionId'],
75-
]);
76-
$result->setContents('invalid_refund');
67+
if ((float) $order->getData('fintecture_payment_refund_amount') === (float) $order->getBaseGrandTotal()) {
68+
$result->setContents('order_already_refunded');
69+
70+
return $result;
71+
}
72+
73+
$decodedState = base64_decode($params['state']);
74+
if ($decodedState) {
75+
$decodedStateObj = json_decode($decodedState);
76+
if ($decodedStateObj) {
77+
if (property_exists($decodedState, 'creditmemo_transaction_id')) {
78+
// Use existing credit memo
79+
return $this->refund($order, $params['status'], (float) $params['amount'], $decodedStateObj->creditmemo_transaction_id);
80+
}
81+
}
7782
}
83+
84+
return $this->refund($order, $params['status'], (float) $params['amount']);
7885
} else {
7986
return $this->payment($order, $params);
8087
}
@@ -95,7 +102,6 @@ private function payment(Order $order, array $params): Raw
95102
{
96103
$result = $this->resultRawFactory->create();
97104
$result->setHeader('Content-Type', 'text/plain');
98-
$result->setHttpResponseCode(200);
99105

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

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

181187
if ($status === 'payment_created') {
182-
$appliedRefund = $this->handleRefund->apply($order, $creditmemoTransactionId);
183-
if ($appliedRefund) {
184-
$result->setHttpResponseCode(200);
188+
if (!is_null($creditmemoTransactionId)) {
189+
$creditmemo = $this->fintectureHelper->getCreditmemoByTransactionId($order, $creditmemoTransactionId);
190+
191+
$appliedRefund = $creditmemo ? $this->handleRefund->apply($order, $creditmemo, $amount) : false;
185192
} else {
193+
$appliedRefund = $this->handleRefund->applyWithoutCreditmemo($order, $amount);
194+
}
195+
196+
if (!$appliedRefund) {
186197
$result->setHttpResponseCode(400);
187198
$result->setContents('refund_not_applied');
188199
}

Controller/Standard/Send.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use chillerlan\QRCode\QRCode as QRCodeGenerator;
88
use Fintecture\Payment\Controller\FintectureAbstract;
9+
use Fintecture\Payment\Helper\Fintecture;
910
use Magento\Framework\View\Element\Template;
1011

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

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

3940
$reference = $data['data']['attributes']['communication'];

Gateway/Config/Config.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
class Config extends BaseConfig
88
{
99
const CODE = 'fintecture';
10-
const VERSION = '3.4.2';
10+
const VERSION = '3.5.0';
1111

1212
const KEY_SHOP_NAME = 'general/store_information/name';
1313
const KEY_ACTIVE = 'active';
@@ -21,6 +21,7 @@ class Config extends BaseConfig
2121
const KEY_PRIVATE_KEY_SANDBOX = 'custom_file_upload_sandbox';
2222
const KEY_PRIVATE_KEY_PRODUCTION = 'custom_file_upload_production';
2323
const KEY_BANK_TYPE = 'general/bank_type';
24+
const KEY_REFUND_STATUSES_ACTIVE = 'refund_statuses_active';
2425
const KEY_EXPIRATION_ACTIVE = 'expiration_active';
2526
const KEY_EXPIRATION_AFTER = 'expiration_after';
2627
const KEY_INVOICING_ACTIVE = 'invoicing_active';
@@ -110,6 +111,11 @@ public function getBankType(): ?string
110111
return $this->getValue(self::KEY_BANK_TYPE);
111112
}
112113

114+
public function isRefundStatusesActive(): bool
115+
{
116+
return (bool) $this->getValue(self::KEY_REFUND_STATUSES_ACTIVE);
117+
}
118+
113119
public function isExpirationActive(): bool
114120
{
115121
return (bool) $this->getValue(self::KEY_EXPIRATION_ACTIVE);
@@ -234,4 +240,14 @@ public function getPaymentFailedStatus(): string
234240

235241
return $status;
236242
}
243+
244+
public function getPartialRefundStatus(): string
245+
{
246+
$status = $this->getValue('payment/fintecture/partial_refund_status');
247+
if (!$status) {
248+
$status = 'fintecture_partial_refund';
249+
}
250+
251+
return $status;
252+
}
237253
}

Gateway/HandlePayment.php

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Fintecture\Payment\Gateway;
44

55
use Fintecture\Payment\Gateway\Config\Config;
6+
use Fintecture\Payment\Gateway\Http\Sdk;
67
use Fintecture\Payment\Helper\Fintecture as FintectureHelper;
78
use Fintecture\Payment\Logger\Logger;
89
use Magento\Framework\DB\Transaction;
@@ -60,6 +61,9 @@ class HandlePayment
6061
/** @var InvoiceRepositoryInterface */
6162
protected $invoiceRepository;
6263

64+
/** @var Sdk */
65+
protected $sdk;
66+
6367
public function __construct(
6468
Logger $fintectureLogger,
6569
Config $config,
@@ -73,7 +77,8 @@ public function __construct(
7377
OrderSender $orderSender,
7478
InvoiceSender $invoiceSender,
7579
InvoiceService $invoiceService,
76-
InvoiceRepositoryInterface $invoiceRepository
80+
InvoiceRepositoryInterface $invoiceRepository,
81+
Sdk $sdk
7782
) {
7883
$this->fintectureLogger = $fintectureLogger;
7984
$this->config = $config;
@@ -88,6 +93,7 @@ public function __construct(
8893
$this->invoiceService = $invoiceService;
8994
$this->invoiceSender = $invoiceSender;
9095
$this->invoiceRepository = $invoiceRepository;
96+
$this->sdk = $sdk;
9197
}
9298

9399
public function create(
@@ -140,7 +146,7 @@ public function create(
140146
->setOrder($order)
141147
->setTransactionId($order->getIncrementId() . '-' . time())
142148
->setAdditionalInformation([
143-
\Magento\Sales\Model\Order\Payment\Transaction::RAW_DETAILS => [
149+
Payment\Transaction::RAW_DETAILS => [
144150
'amount' => (string) $lastTransactionAmount . '',
145151
'status' => $params['status'],
146152
'sessionId' => $params['sessionId'],
@@ -204,6 +210,7 @@ public function sendInvoice(Order $order, array $params): void
204210
$invoice = $this->invoiceService->prepareInvoice($order);
205211
$invoice->setTransactionId($params['sessionId']);
206212
$invoice->register();
213+
$invoice->pay();
207214
$this->invoiceRepository->save($invoice);
208215
$transactionSave = $this->transaction
209216
->addObject($invoice)
@@ -214,6 +221,35 @@ public function sendInvoice(Order $order, array $params): void
214221

215222
$order->setIsCustomerNotified(true);
216223
$this->orderRepository->save($order);
224+
225+
if ($params['status'] === 'order_created') {
226+
// Update invoice_reference field for BNPL orders
227+
$data = [
228+
'data' => [
229+
'attributes' => [
230+
'invoice_reference' => '#' . $invoice->getIncrementId(),
231+
],
232+
],
233+
];
234+
235+
$pisToken = $this->sdk->pisClient->token->generate();
236+
if (!$pisToken->error) {
237+
$this->sdk->pisClient->setAccessToken($pisToken); // set token of PIS client
238+
} else {
239+
$this->fintectureLogger->error("Can't update invoice_reference field", [
240+
'message' => $pisToken->errorMsg,
241+
'incrementOrderId' => $order->getIncrementId(),
242+
]);
243+
}
244+
245+
$apiResponse = $this->sdk->pisClient->payment->update($params['sessionId'], $data);
246+
if ($apiResponse->error) {
247+
$this->fintectureLogger->error("Can't update invoice_reference field", [
248+
'message' => $apiResponse->errorMsg,
249+
'incrementOrderId' => $order->getIncrementId(),
250+
]);
251+
}
252+
}
217253
}
218254
}
219255

0 commit comments

Comments
 (0)