Skip to content
This repository has been archived by the owner on Oct 10, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2 from lunar/hosted-chekcout
Browse files Browse the repository at this point in the history
Hosted chekcout
  • Loading branch information
lucionescu authored Feb 16, 2024
2 parents ad3c576 + 1ffe932 commit d81ca0f
Show file tree
Hide file tree
Showing 112 changed files with 2,750 additions and 5,923 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.idea
.DS_Store
vendor
/vendor
.env
.env.production
logs
Expand Down
10 changes: 10 additions & 0 deletions LunarPayments/admin/index.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

// small hack to have loaded definitions from one place
$GLOBALS['language']->loadDefinitions('lunar_text', CC_ROOT_DIR.'/modules/plugins/LunarPayments/language', 'module.definitions.xml');

global $lunarPluginPath, $lunarMethod;

$module = new Module($lunarPluginPath, $_GET['module'], CC_ROOT_DIR.'/modules/plugins/LunarPayments/skin/admin/index.tpl', true);

$page_content = $module->display();
Binary file added LunarPayments/admin/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions LunarPayments/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require": {
"lunar/payments-api-sdk": "^1.0"
}
}
150 changes: 150 additions & 0 deletions LunarPayments/helpers/lunar_transactions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
<?php

if (!class_exists('\\Lunar\\Lunar')) {
require_once(dirname(__DIR__).'/vendor/autoload.php');
}

class lunarTransactions
{
private $_lang;
private $_config;
private $order;
private $orderId;
private $apiClient;
private $lastTransaction;
private $orderSummary;
private $actionType;
private $langKey;
private $canInsertTransaction;
private $newOrderStatus;

public function __construct($pluginCode, $orderId)
{
if (!$this->orderId = $orderId) {
return;
}

$GLOBALS['language']->loadDefinitions('lunar_text', CC_ROOT_DIR.'/modules/plugins/LunarPayments/language', 'module.definitions.xml');
$this->_lang = $GLOBALS['language']->getStrings('lunar_text');

$this->_config = $GLOBALS['config']->get($pluginCode);

$this->apiClient = new \Lunar\Lunar($this->_config['app_key'], null, !!$_COOKIE['lunar_testmode']);

$this->order = Order::getInstance();
$this->orderSummary = $this->order->getSummary($this->orderId);

/** @var Database */
$db = $GLOBALS['db'];

$lastTransaction = $db->select('CubeCart_transactions', false,
[
'order_id' => $this->orderId,
'gateway' => $pluginCode
],
[
'time' => 'DESC',
]
);

if (empty($lastTransaction) || empty($lastTransaction[0]['trans_id'])) {
$this->canInsertTransaction = false;
} else {
$this->lastTransaction = $lastTransaction[0];
}
}

/**
*
*/
public function captureTransaction()
{
$this->actionType = 'capture';
$this->langKey = 'captured';
$this->canInsertTransaction = ($this->lastTransaction['status'] === 'Authorized');

$this->processTransaction();
}

/**
* It will VOID or REFUND transaction based on last order transaction from DB
*/
public function cancelTransaction()
{
if ($this->lastTransaction['status'] === 'Captured') {
$this->actionType = 'refund';
$this->langKey = 'refunded';
$this->canInsertTransaction = true;
} elseif ($this->lastTransaction['status'] === 'Authorized') {
$this->actionType = 'cancel';
$this->langKey = 'voided';
$this->canInsertTransaction = true;
}

$this->newOrderStatus = Order::ORDER_CANCELLED;

$this->processTransaction();
}

/**
*
*/
private function processTransaction()
{
if (empty($this->canInsertTransaction)) {
return;
}

$transaction = $this->lastTransaction;
$transaction['notes'] = [];
unset($transaction['id']);

$actionType = $this->actionType;

try {
$apiResponse = $this->apiClient->payments()->{$actionType}(
$transaction['trans_id'],
[
'amount' => [
'currency' => $this->orderSummary['currency'],
'decimal' => (string) $transaction['amount'],
]
]
);
} catch(Lunar\Exception\ApiException $e) {
$transaction['notes'][] = $e->getMessage();
}

if (!empty($apiResponse["{$actionType}State"]) && 'completed' == $apiResponse["{$actionType}State"]) {

if (!empty($this->newOrderStatus)) {
$this->order->orderStatus($this->newOrderStatus, $this->orderId);
}

$transaction['notes'][] = $this->trans($this->langKey);
$transaction['status'] = ucfirst($this->langKey);

$this->order->logTransaction($transaction);

$GLOBALS['main']->successMessage($this->trans($this->langKey));

} elseif ('declined' === $apiResponse["{$actionType}State"]) {
$transaction['status'] = ucfirst($actionType).' Failed';
$transaction['notes'][] = $apiResponse['declinedReason']['error'];

$this->order->logTransaction($transaction);

} else {
$GLOBALS['main']->errorMessage($this->trans('error_general_admin_txn'));
}
}

/**
*
*/
private function trans($key)
{
return isset($this->_lang[$key]) ? ($this->_lang[$key]) : '';
}

}
28 changes: 28 additions & 0 deletions LunarPayments/hooks/admin.order.index.post_process.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

global $lunarPluginCode;

$orderId = isset($record['cart_order_id']) ? $record['cart_order_id'] : null;

if (!empty($orderId)) {

// when order status set to Order Complete
if (isset($_POST['order']['status']) && $_POST['order']['status'] == '3') {
if (!class_exists('lunarTransactions')) {
require(CC_ROOT_DIR.'/modules/plugins/LunarPayments/helpers/lunar_transactions.php');
}

$lunarTransactions = new lunarTransactions($lunarPluginCode, $orderId);
$lunarTransactions->captureTransaction();
}

// void OR refund request when status set to Cancelled
if (isset($_POST['order']['status']) && $_POST['order']['status'] == '6') {
if (!class_exists('lunarTransactions')) {
require(CC_ROOT_DIR.'/modules/plugins/LunarPayments/helpers/lunar_transactions.php');
}

$lunarTransactions = new lunarTransactions($lunarPluginCode, $orderId);
$lunarTransactions->cancelTransaction();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

if (isset($_GET['module']) && $_GET['module'] == $lunarPluginCode) {
$plugin = true;
}
33 changes: 33 additions & 0 deletions LunarPayments/hooks/class.cubecart.display_gateways.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

$lunarConfig = $GLOBALS['config']->get($lunarPluginCode);

if ($lunarConfig['status']) {

if ('lunar_mobilepay' == $lunarPluginCode) {
$description = '<div style="display:flex;height:2rem"><span>'.$lunarConfig['checkout_name']
.'</span>'.'<div style="margin-left:1rem;"><img style="height:2rem;" src="/modules/plugins/LunarPayments/skin/images/mobilepay-logo.png" alt="mobilepay logo"></div></div>';
} else {
$description = '<div style="display:flex;align-items: center;">'.$lunarConfig['checkout_name']
.'<img style="margin-left:0.3rem;" src="/modules/plugins/LunarPayments/skin/images/mastercard.svg" alt="mastercard logo">'
.'<img style="margin-left:0.3rem;" src="/modules/plugins/LunarPayments/skin/images/visa.svg" alt="visa logo">'
.'<img style="margin-left:0.3rem;" src="/modules/plugins/LunarPayments/skin/images/maestro.svg" alt="maestro logo"></div>';
}

$gatewayData = [
'plugin' => true,
'base_folder' => $lunarPluginCode,
'folder' => $lunarPluginCode,
'desc' => $description,
];

if (isset($_POST['gateway']) || !empty($name)) {
$base_folder = isset($_POST['gateway']) ? $_POST['gateway'] : $name;
if ($base_folder == $lunarPluginCode) {
$gateways[0] = $gatewayData;
}
} else {
$gatewayData['default'] = isset($lunarConfig['default']) ? (bool) $lunarConfig['default'] : true;
$gateways[] = $gatewayData;
}
}
42 changes: 42 additions & 0 deletions LunarPayments/language/module.definitions.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="UTF-8"?>
<definitions version="1.0">
<group name="lunar_text">
<string name="setup_lunar"><![CDATA[Configure Lunar]]></string>
<string name="checkout_name"><![CDATA[Payment name (displayed to customers)]]></string>
<string name="app_key"><![CDATA[App Key]]></string>
<string name="public_key"><![CDATA[Public Key]]></string>
<string name="logo_url"><![CDATA[Logo URL]]></string>
<string name="configuration_id"><![CDATA[Configuration ID]]></string>
<string name="shop_name"><![CDATA[Shop name]]></string>
<string name="capture_mode"><![CDATA[Capture Mode]]></string>
<string name="instant"><![CDATA[Instant]]></string>
<string name="delayed"><![CDATA[Delayed]]></string>

<!-- messages -->
<string name="error_create_intent"><![CDATA[An error occurred creating payment intent. Please try again or contact system administrator.]]></string>
<string name="invalid_txn"><![CDATA[Invalid Transaction ID]]></string>
<string name="txn_exists"><![CDATA[This Transaction has been processed before.]]></string>
<string name="idsmissing"><![CDATA[Required order id is missing. Please contact administrator.]]></string>
<string name="confirmerror"><![CDATA[Confirmation process contains errors.]]></string>
<string name="notsuccessful"><![CDATA[Payment confirmation was not successful]]></string>
<string name="amountmismatch"><![CDATA[Amounts mismatch for the payment]]></string>
<string name="user_payment_error"><![CDATA[We were unable to confirm your payment. Please contact administrator for more information.]]></string>
<string name="error_unknown"><![CDATA[There were errors during checkout. Please contact administrator.]]></string>
<string name="paysuccess"><![CDATA[Payment successful and confirmed.]]></string>
<string name="captured"><![CDATA[Successfully CAPTURED authorized amount on Lunar.]]></string>
<string name="refund"><![CDATA[Refund]]></string>
<string name="refunded"><![CDATA[Payment was REFUNDED on Lunar.]]></string>
<string name="refund_title"><![CDATA[Refund Payment]]></string>
<string name="refund_confirm"><![CDATA[Refund action is not reversible. All paid amount will return to customer! Confirm if you want to proceed.]]></string>
<string name="void"><![CDATA[Void]]></string>
<string name="void_title"><![CDATA[Void Payment]]></string>
<string name="void_confirm"><![CDATA[When you void this payment, authorization will be cancelled and you will not be able to claim it back! Void action is not reversible. Confirm if you want to proceed.]]></string>
<string name="voided"><![CDATA[Payment authorization was VOIDED on Lunar.]]></string>

<!-- admin messages -->
<string name="cache_reminder"><![CDATA[Please note that changes won't show on the front end until the "Clear Cache" button is clicked.]]></string>
<string name="missing_order_id"><![CDATA[Order ID was not provided]]></string>
<string name="missing_order_transaction"><![CDATA[Could not find any transaction for provided order ID]]></string>
<string name="error_general_admin_txn"><![CDATA[There were errors during order transaction process. Please try again.]]></string>
</group>
</definitions>
Loading

0 comments on commit d81ca0f

Please sign in to comment.