diff --git a/src/Pckg/Payment/Form/PlatformSettings/Bankart.php b/src/Pckg/Payment/Form/PlatformSettings/Bankart.php index 0cb7f81..8dec279 100644 --- a/src/Pckg/Payment/Form/PlatformSettings/Bankart.php +++ b/src/Pckg/Payment/Form/PlatformSettings/Bankart.php @@ -16,6 +16,7 @@ public function initFields() $this->addText('apiPassword')->setLabel('API password')->addValidator(new RequireWhenEnabled($this)); $this->addText('publicIntegrationKey')->setLabel('Public integration key')->addValidator(new RequireWhenEnabled($this)); $this->addText('maxInstalments')->setLabel('Max instalments')->addValidator(new RequireWhenEnabled($this)); + $this->addText('transactionMethod')->setLabel('Transaction method')->addValidator(new RequireWhenEnabled($this)); $this->addText('url')->setLabel('API URL')->addValidator(new RequireWhenEnabled($this)); return $this; diff --git a/src/Pckg/Payment/Handler/AbstractHandler.php b/src/Pckg/Payment/Handler/AbstractHandler.php index cb191c0..53a3c6d 100644 --- a/src/Pckg/Payment/Handler/AbstractHandler.php +++ b/src/Pckg/Payment/Handler/AbstractHandler.php @@ -4,6 +4,7 @@ use Pckg\Payment\Adapter\Environment; use Pckg\Payment\Adapter\Log; use Pckg\Payment\Adapter\Order; +use Pckg\Payment\Entity\Payments; use Pckg\Payment\Record\Payment; abstract class AbstractHandler implements Handler @@ -160,6 +161,8 @@ public function setPaymentId($paymentId) public function waitPayment($description, $log, $transactionId, $status = 'waiting') { + try { + (new Payments())->transaction(function() use ($description, $log, $transactionId, $status){ $this->paymentRecord->addLog($status, $log); $this->order->getBills()->keyBy('order_id')->each(function(OrdersBill $ordersBill) use ($description) { @@ -178,20 +181,47 @@ public function waitPayment($description, $log, $transactionId, $status = 'waiti 'status' => $status, 'transaction_id' => $transactionId, ]); + + throw new \Exception("Reverted"); + }); + } catch (\Throwable $e) { + ddd(exception($e)); + } } + /** + * @param $description + * @param $log + * @param $transactionId + * @param string $status + * + * Used to confirm transaction and related orders, packets and instalments. + */ public function approvePayment($description, $log, $transactionId, $status = 'approved') { $this->paymentRecord->addLog($status, $log); - $this->order->getBills()->each(function(OrdersBill $ordersBill) use ($description) { + $this->order->getBills()->each(function (OrdersBill $ordersBill) use ($description) { $ordersBill->confirm($description); }); $this->paymentRecord->setAndSave([ - 'status' => $status, - 'transaction_id' => $transactionId, - ]); + 'status' => $status, + 'transaction_id' => $transactionId, + ]); + } + + /** + * Used to pre-authorize transaction. + * Should decrease stock? + */ + public function authorizePayment($description, $log, $transactionId, $status = 'authorized') + { + $this->paymentRecord->addLog($status, $log); + $this->paymentRecord->setAndSave([ + 'status' => $status, + 'transaction_id' => $transactionId, + ]); } public function approveRefund($description, $log, $transactionId) diff --git a/src/Pckg/Payment/Handler/Omnipay/AbstractOmnipay.php b/src/Pckg/Payment/Handler/Omnipay/AbstractOmnipay.php index 8bcc52a..ccb1da1 100644 --- a/src/Pckg/Payment/Handler/Omnipay/AbstractOmnipay.php +++ b/src/Pckg/Payment/Handler/Omnipay/AbstractOmnipay.php @@ -12,6 +12,10 @@ abstract class AbstractOmnipay extends AbstractHandler { + const TRANSACTION_PURCHASE = 'purchase'; + + const TRANSACTION_PREAUTHORIZATION = 'authorize'; + /** * @var string */ @@ -80,12 +84,34 @@ public function initPayment() return $this->postStart(); } + /** + * @return string + * Return "purchase" or "authorization" + */ + public function getOmnipayTransactionMethod() + { + return $this->environment->config($this->handler . '.transactionMethod') ?? static::TRANSACTION_PURCHASE; + } + + /** + * @param $request + * @return array|mixed + */ + public function sendOmnipayEnrichedData($request) + { + return $request->sendData($this->enrichOmnipayOrderDetails($request->getData())); + } + /** * @throws \Exception */ public function postStart() { - if (!$this->client->supportsPurchase()) { + /** + * Check for purchase / authorize support. + */ + $transactionMethod = $this->getOmnipayTransactionMethod(); + if (!$this->client->{['purchase' => 'supportsPurchase', 'authorize' => 'supportsAuthorize'][$transactionMethod]}()) { throw new \Exception('Gateway does not support purchase()'); } @@ -94,21 +120,26 @@ public function postStart() * Get customer and order details. * Make the purchase call. */ - $request = $this->client->purchase($this->getOmnipayOrderDetails()); + $request = $this->client->{$transactionMethod}($this->getOmnipayOrderDetails()); /** * Some parameters are not supported by the original gateway. */ - $response = $request->sendData($this->enrichOmnipayOrderDetails($request->getData())); + $response = $this->sendOmnipayEnrichedData(); /** - * Firs check for a redirect. + * First check for a redirect. * Send the redirect to the frontend. */ if ($response->isRedirect()) { $redirect = $response->getRedirectUrl(); $method = $response->getRedirectMethod(); + + /** + * GET method is redirected. + */ if ($method === 'GET') { + $this->paymentRecord->addLog('redirecting', 'Redirected'); return [ 'success' => true, 'redirect' => $redirect, @@ -118,6 +149,7 @@ public function postStart() /** * Submit the form with data on the frontend. */ + $this->paymentRecord->addLog('redirecting', 'Submitting form'); return [ 'success' => true, 'form' => [ @@ -137,6 +169,7 @@ public function postStart() $this->paymentRecord->setAndSave([ 'payment_id' => $response->getTransactionReference(), ]); + $this->paymentRecord->addLog('success', 'Success ' . $transactionMethod); return [ 'success' => true, @@ -144,12 +177,17 @@ public function postStart() ]; } + $this->paymentRecord->addLog('error', $response->getMessage() . ' ' . $response->getCode()); + return [ 'success' => false, 'message' => $response->getMessage(), 'code' => $response->getCode(), ]; } catch (\Throwable $e) { + + $this->paymentRecord->addLog('error', exception($e)); + return [ 'success' => false, 'modal' => 'error', @@ -159,6 +197,26 @@ public function postStart() } } + public function capture($notes = null, $log = []) + { + if (!$this->client->supportsCapture()) { + throw new \Exception('Client does not support capture'); + } + + $response = $this->client->capture()->send(); + + if ($response->isSuccessful()) { + $myTransactionId = $response->getTransactionId(); + $gatewayTransactionId = $response->getTransactionReference(); + + $this->approvePayment(trim(Convention::toCamel($this->handler) . ' #' . $gatewayTransactionId . $notes), ($log ? [$log, $response] : $response), $gatewayTransactionId); + + return true; + } + + return false; + } + /** * @return bool|void */ @@ -169,6 +227,7 @@ public function completePurchase() } $response = $this->client->completePurchase()->send(); + if ($response->isSuccessful()) { $myTransactionId = $response->getTransactionId(); $gatewayTransactionId = $response->getTransactionReference(); @@ -197,12 +256,18 @@ public function postNotification() if ($response->getTransactionStatus() === NotificationInterface::STATUS_COMPLETED) { $myTransactionId = $response->getTransactionId(); $gatewayTransactionId = $response->getTransactionReference(); - - $this->approvePayment(Convention::toCamel($this->handler) . ' #' . $gatewayTransactionId, $response, $gatewayTransactionId); - - return [ - 'success' => true, - ]; + + /** + * Is every transaction approved? + * Shouldn't we only pre-authorize some? + */ + if ($this->getOmnipayTransactionMethod() === static::TRANSACTION_PURCHASE) { + $this->approvePayment(Convention::toCamel($this->handler) . ' #' . $gatewayTransactionId, $response, $gatewayTransactionId); + } else { + $this->authorizePayment(Convention::toCamel($this->handler) . ' #' . $gatewayTransactionId, $response, $gatewayTransactionId); + } + } else { + $this->errorPayment($response); } echo "OK"; diff --git a/src/Pckg/Payment/Handler/Valu.php b/src/Pckg/Payment/Handler/Valu.php index 5c9e732..521c61d 100644 --- a/src/Pckg/Payment/Handler/Valu.php +++ b/src/Pckg/Payment/Handler/Valu.php @@ -161,7 +161,7 @@ public function getNotification() $sConfirmationSignature = ValuHelper::Functions_RequestString("ConfirmationSignature", 250); $nTarifficationError = ValuHelper::Functions_RequestNumber("TARIFFICATIONERROR", 0, 1, 1); $sConfirmationIDStatus = ValuHelper::Functions_RequestString("ConfirmationIDStatus", 32); - $sIP = ValuHelper::Functions_GetServerVariable('REMOTE_ADDR'); + $sIP = request()->clientIp(); $sOutput = "1"; $this->paymentRecord->addLog('check', ['confirmationSignature' => $sConfirmationSignature, 'tarifficationError' => $nTarifficationError, 'confirmationIdStatus' => $sConfirmationIDStatus]); diff --git a/src/Pckg/Payment/View/platformSettings/bankart.vue b/src/Pckg/Payment/View/platformSettings/bankart.vue index 768792f..00376a2 100644 --- a/src/Pckg/Payment/View/platformSettings/bankart.vue +++ b/src/Pckg/Payment/View/platformSettings/bankart.vue @@ -48,12 +48,17 @@ -
- -
- -
+
+ +
+
+
+ +