Skip to content

Commit

Permalink
inital
Browse files Browse the repository at this point in the history
  • Loading branch information
731MY committed Sep 15, 2024
0 parents commit 7287e36
Show file tree
Hide file tree
Showing 58 changed files with 3,926 additions and 0 deletions.
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require": {
"singlewallet/singlewallet-php-sdk": "^1.3"
}
}
52 changes: 52 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8" ?>
<module>
<name>singlewallet</name>
<displayName><![CDATA[SingleWallet]]></displayName>
<version><![CDATA[1.0]]></version>
<description><![CDATA[Accept Tether in all popular blockchains with SingleWallet.]]></description>
<author><![CDATA[SingleWallet]]></author>
<tab><![CDATA[payments_gateways]]></tab>
<confirmUninstall>Are you sure you want to uninstall?</confirmUninstall>
<is_configurable>1</is_configurable>
<need_instance>0</need_instance>
<limited_countries></limited_countries>
</module>
9 changes: 9 additions & 0 deletions controllers/front/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Location: ../');
exit;
107 changes: 107 additions & 0 deletions controllers/front/payment.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php


use singlewallet\helper\DBHelper;

class SinglewalletPaymentModuleFrontController extends ModuleFrontController {

public $auth = true;

public function postProcess(){
$cart = $this->context->cart;
$apiKey = Configuration::get('singlewallet_api_key');

if ($cart->id_customer == 0 || $cart->id_address_delivery == 0 || $cart->id_address_invoice == 0 || !$this->module->active || empty($apiKey)) {
Tools::redirect('index.php?controller=order&step=1');
}

$authorized = false;

foreach (Module::getPaymentModules() as $module) {
if ($module['name'] == 'singlewallet') {
$authorized = true;
break;
}
}

if (!$authorized) {
$this->redirectToErrorPage('This payment method is not available');
}

$customer = new Customer($cart->id_customer);

if (!Validate::isLoadedObject($customer)) {
Tools::redirect('index.php?controller=order&step=1');
}

$language = $this->context->language->iso_code;
$currency = $this->context->currency->iso_code;
$products = $this->getProductsList($cart->getProducts());
$ttl = Configuration::get('singlewallet_ttl',null,null,null,60);

$total = (string)$cart->getOrderTotal(true, Cart::BOTH);

$result = $this->module->validateOrder($cart->id, 1, $cart->getOrderTotal(), 'Single Wallet');
if(!$result){
$this->redirectToErrorPage('error occurred while validating order');
}

$orderId = Order::getIdByCartId($cart->id);
$order = new Order($orderId);
$payload = json_encode($order);

$redirect = Context::getContext()->link->getPageLink('order-detail', true,null,[
'id_order'=>$orderId,
]);

$sw = new \SingleWallet\SingleWallet($apiKey, '');
$invoice = (new \SingleWallet\Models\Request\Invoice())
->setOrderName($products)
->setOrderNumber("#$orderId")
->setAmount($total)
->setCurrencyCode($currency)
->setLanguage($language)
->setTtl($ttl)
->setCallbackUrl(Context::getContext()->link->getModuleLink('singlewallet', 'webhook'))
->setRedirectUrl($redirect)
->setCancelUrl($redirect)
->setPayload($payload);

if(Configuration::get('singlewallet_send_customer_email')){
$invoice->setCustomerEmail($customer->email);
}

$newInvoice = null;
try{
$newInvoice = $sw->createInvoice($invoice);
}catch(\Exception $e){}

if(is_null($newInvoice)){
$this->redirectToErrorPage('error occurred while creating your invoice');
}

$expireAt = time()+($ttl*60);
$createOrder = DBHelper::createOrder($orderId,$newInvoice->getId(), $newInvoice->getUrl(),$expireAt);

if(!$createOrder){
$this->redirectToErrorPage('error occurred while creating your invoice');
}

Tools::redirect($newInvoice->getUrl());
}

private function redirectToErrorPage($message){
Tools::redirect($this->context->link->getModuleLink('singlewallet','error',['msg'=>$message]));
}

private function getProductsList($products){
$count = count($products);
if($count == 1){
return $products[0]['name'];
}else if($count > 1){
return $products[0]['name']." and ".($count-1)." other items";
}

return "Order";
}
}
104 changes: 104 additions & 0 deletions controllers/front/redirect.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?php

use singlewallet\helper\DBHelper;

class SinglewalletRedirectModuleFrontController extends ModuleFrontController {

public function initContent(){
$orderId = Tools::getValue('order_id');

$invoiceOrder = DBHelper::getOrder($orderId);

$orderLink = Context::getContext()->link->getPageLink('order-detail', true,null,[
'id_order'=>$orderId,
]);

if(is_null($invoiceOrder)){
Tools::redirect($orderLink);
}

$isUnderpaid = false;

if($invoiceOrder['status'] == 'new' || $invoiceOrder['status'] == 'expired'){
$remaining = $invoiceOrder['expire_at'] - time();

if($remaining > 600){ // 10 minutes
Tools::redirect($invoiceOrder['url']);
}
}else if($invoiceOrder['status'] == 'pending'){
Tools::redirect($orderLink);
}else if($invoiceOrder['status'] == 'paid'){
$isUnderpaid = DBHelper::isUnderpaid($orderId);
}

$order = new Order($orderId);
$products = $this->getProductsList($order->getCartProducts());

if($isUnderpaid){
$totalAmount = $order->total_paid - DBHelper::totalPaidForOrder($orderId);
}else{
$totalAmount = $order->total_paid;
}

$payload = json_encode($order);

$apiKey = Configuration::get('singlewallet_api_key');

$language = $this->context->language->iso_code;
$currency = $this->context->currency->iso_code;

$ttl = Configuration::get('singlewallet_ttl',null,null,null,60);

$sw = new \SingleWallet\SingleWallet($apiKey, '');
$invoice = (new \SingleWallet\Models\Request\Invoice())
->setOrderName($products)
->setOrderNumber("#$orderId")
->setAmount($totalAmount)
->setCurrencyCode($currency)
->setLanguage($language)
->setTtl($ttl)
->setCallbackUrl(Context::getContext()->link->getModuleLink('singlewallet', 'webhook'))
->setRedirectUrl($orderLink)
->setCancelUrl($orderLink)
->setPayload($payload);

if(Configuration::get('singlewallet_send_customer_email')){
$customer = new Customer($order->id_customer);
$invoice->setCustomerEmail($customer->email);
}

$newInvoice = null;
try{
$newInvoice = $sw->createInvoice($invoice);
}catch(\Exception $e){}

if(is_null($newInvoice)){
$this->redirectToErrorPage('error occurred while creating your invoice');
}

$expireAt = time()+($ttl*60);
$createOrder = DBHelper::createOrder($orderId,$newInvoice->getId(), $newInvoice->getUrl(),$expireAt);

if(!$createOrder){
$this->redirectToErrorPage('error occurred while creating your invoice');
}

Tools::redirect($newInvoice->getUrl());
}

private function redirectToErrorPage($message){
Tools::redirect($this->context->link->getModuleLink('singlewallet','error',['msg'=>$message]));
}

private function getProductsList($products){
$count = count($products);
if($count == 1){
return $products[0]['product_name'];
}else if($count > 1){
return $products[0]['product_name']." and ".($count-1)." other items";
}

return "Order";
}

}
87 changes: 87 additions & 0 deletions controllers/front/webhook.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

use singlewallet\helper\DBHelper;

class SinglewalletWebhookModuleFrontController extends ModuleFrontController {

public $ajax;

public function initContent(){
$this->ajax = 1;

$data = trim(file_get_contents("php://input"));

$apiKey = Configuration::get('singlewallet_api_key');
$secret = Configuration::get('singlewallet_secret');
$signature = $_SERVER['HTTP_SW_SIGNATURE'];

if (empty($apiKey) || empty($data) || empty($secret) || empty($signature)) {
Tools::redirect('/');
}

$sw = new \SingleWallet\SingleWallet($apiKey, $secret);

if(!$sw->isValidPayload($data, $signature)){
die;
}

$data = json_decode($data, true);
$invoiceId = $data['id'];
$checkInvoice = DBHelper::getOrderByInvoiceId($invoiceId);

if(is_null($checkInvoice) || in_array($checkInvoice['status'],['paid','cancelled','expired'])){
return;
}

$invoice = $sw->getInvoice($invoiceId);

$payload = json_decode($invoice->getPayload(),true);

$invoiceStatus = $invoice->getStatus();

$orderId = $payload['id'];

DBHelper::updateOrderStatus($invoice->getId(), $invoiceStatus);

if($invoiceStatus == 'pending' || $invoiceStatus == 'paid'){
$invoiceId = $invoice->getId();
$invoiceAmount = $invoice->getInvoiceAmount();
$fiatInvoiceAmount = $invoice->getFiatInvoiceAmount();
$paidAmount = $invoice->getPaidAmount();
$fiatPaidAmount = $invoice->getFiatPaidAmount();
$exchangeRate = $invoice->getExchangeRate();
$txid = $invoice->getTxid();
$txidUrl = $invoice->getBlockchainUrl();
$exception = $invoice->getException();
$currency = $invoice->getCurrencyCode();

$checkPayment = DBHelper::getPayment($txid);
if(is_null($checkPayment)){
DBHelper::createOrderPayment($orderId, $invoiceId, $invoiceAmount, $fiatInvoiceAmount, $paidAmount, $fiatPaidAmount, $exchangeRate, $currency, $txid, $txidUrl, $invoiceStatus, $exception);
}

if($invoiceStatus == 'paid'){
if($exception == 'none' || $exception == 'overpaid'){
DBHelper::updatePaymentStatus($txid, $invoiceStatus);
$this->confirmOrder($orderId);
}
}
}

}

public function confirmOrder($orderId){
$order = new Order($orderId);
$orderCurrentState = $order->current_state;
$paidState = Configuration::get('singlewallet_order_state');
if($orderCurrentState != $paidState){
$history = new OrderHistory();
$history->id_order = $order->id;
$history->id_employee = 1;
$history->id_order_state = $paidState;
$history->save();
$history->changeIdOrderState($paidState, $order , true);
$history->sendEmail($order);
}
}
}
10 changes: 10 additions & 0 deletions controllers/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Location: ../');
exit;

Loading

0 comments on commit 7287e36

Please sign in to comment.