Skip to content
This repository has been archived by the owner on Feb 21, 2023. It is now read-only.

Commit

Permalink
Updated WHMCS version to 6.3.1
Browse files Browse the repository at this point in the history
  • Loading branch information
ktamasauskas committed Jul 1, 2016
1 parent 889e714 commit b8dec0f
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 81 deletions.
21 changes: 16 additions & 5 deletions README.md
@@ -1,12 +1,12 @@
SpectroCoin Payment Method
SpectroCoin Bitcoin Payment Extension
---------------

This module integrates [SpectroCoin](https://spectrocoin.com/) Payments with [WHMCS](http://www.whmcs.com/) to accept [Bitcoin](https://bitcoin.org) payments.
This merchant module integrates [SpectroCoin](https://spectrocoin.com/) Payments with [WHMCS](http://www.whmcs.com/) to accept [Bitcoin](https://bitcoin.org) payments.

**INSTALLATION**

1. Upload module content to your WHMCS folder.
2. Generate private and public keys
2. Generate private and public keys [Manually]
1. Private key:
```shell
# generate a 2048-bit RSA private key
Expand All @@ -17,10 +17,21 @@ This module integrates [SpectroCoin](https://spectrocoin.com/) Payments with [WH
# output public key portion in PEM format
openssl rsa -in "C:\private" -pubout -outform PEM -out "C:\public"
```
3. Save private key to modules/gateways/spectrocoin/keys as "private"
3. Generate private and public keys [Automatically]
1. Private key/Public key:
Go to [SpectroCoin](https://spectrocoin.com/) -> [Project list](https://spectrocoin.com/en/merchant/api/list.html)
Click on your project -> Edit Project -> Click on Public key (You will get Automatically generated private key, you can download it. After that and Public key will be generated Automatically.)

4. Save private key to modules/gateways/spectrocoin/keys as "private"

**CONFIGURATION**

3. Go to Setup -> Payments -> Payment Gateways
3. Go to Setup -> Payments -> Payment Gateways -> All Payment Gateways
4. Select "Bitcoin provided by SpectroCoin" and press Activate
5. Enter your Merchant Id, Application Id.

**INFORMATION**

1. You can contact us e-mail: info@spectrocoin.com
2. You can contact us by phone: +442037697306
3. You can contact us on Skype: spectrocoin_merchant
14 changes: 5 additions & 9 deletions modules/gateways/callback/spectrocoin.php
@@ -1,6 +1,6 @@
<?php
# Required File Includes
include '../../../dbconnect.php';
include '../../../init.php';
include '../../../includes/functions.php';
include '../../../includes/gatewayfunctions.php';
include '../../../includes/invoicefunctions.php';
Expand All @@ -27,15 +27,11 @@
$receiveCurrency = $GATEWAY['receive_currency'];

$request = $_REQUEST;
$client = new SCMerchantClient($privateKey, '', $merchantId, $appId);
$client = new SCMerchantClient($merchantApiUrl, $merchantId, $appId);
$callback = $client->parseCreateOrderCallback($request);

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
if ($client->validateCreateOrderCallback($callback)) {
if ($callback->getReceiveCurrency() != $receiveCurrency) {
error_log('SpectroCoin error. Currencies does not match in callback');
exit('SpectroCoin error. Currencies does not match in callback');
}
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
if ($callback != null && $client->validateCreateOrderCallback($callback)){
if (!isset($_GET['invoice_id'])) {
error_log('SpectroCoin error. invoice_id is not provided');
exit('SpectroCoin error. invoice_id is not provided');
Expand Down
5 changes: 0 additions & 5 deletions modules/gateways/spectrocoin.php
Expand Up @@ -15,11 +15,6 @@ function spectrocoin_config()
'FriendlyName' => 'Application id',
'Type' => 'text',
),
'receive_currency' => array(
'FriendlyName' => 'Receive currency',
'Type' => 'dropdown',
'Options' => 'EUR,BTC',
)
);
return $configarray;
}
Expand Down
Expand Up @@ -2,9 +2,9 @@

/**
* Created by UAB Spectro Fincance.
* This is a sample SpectroCoin Merchant v1.0 API PHP client
* This is a sample SpectroCoin Merchant v1.1 API PHP client
*/

include_once('httpful.phar');
include_once('components/FormattingUtil.php');
include_once('data/ApiError.php');
Expand All @@ -16,48 +16,51 @@

class SCMerchantClient
{

const merchantApiUrlStart = 'https://spectrocoin.com/api/merchant/1';



private $merchantApiUrl;
private $privateMerchantCertLocation;
private $publicMerchantCertLocation;
private $publicSpectroCoinCertLocation;

private $merchantId;

private $apiId;
private $debug;

private $privateMerchantKey;

/**
* @param $privateCertLocation
* @param $publicCerLocation
* @param $merchantApiUrl
* @param $merchantId
* @param $apiId
* @param bool $debug
*/
function __construct($privateCertLocation, $publicCerLocation, $merchantId, $apiId, $debug = false)
function __construct($merchantApiUrl, $merchantId, $apiId, $debug = false)
{
$this->privateMerchantCertLocation = $privateCertLocation;
$this->publicMerchantCertLocation = $publicCerLocation;
$this->privateMerchantCertLocation = dirname(__FILE__) . '/../cert/mprivate.pem';
$this->publicSpectroCoinCertLocation = 'https://spectrocoin.com/files/merchant.public.pem';
$this->merchantApiUrl = self::merchantApiUrlStart;
$this->merchantApiUrl = $merchantApiUrl;
$this->merchantId = $merchantId;
$this->apiId = $apiId;
$this->debug = $debug;
}

/**
* @param $privateKey
*/
public function setPrivateMerchantKey($privateKey) {
$this->privateMerchantKey = $privateKey;
}
/**
* @param CreateOrderRequest $request
* @return ApiError|CreateOrderResponse
*/
public function createOrder(CreateOrderRequest $request)
public function createOrder(CreateOrderRequest $request)
{
$payload = array(
'merchantId' => $this->merchantId,
'apiId' => $this->apiId,
'orderId' => $request->getOrderId(),
'orderId' => $request->getOrderId(),
'payCurrency' => $request->getPayCurrency(),
'payAmount' => $request->getPayAmount(),
'receiveCurrency' => $request->getReceiveCurrency(),
'receiveAmount' => $request->getReceiveAmount(),
'description' => $request->getDescription(),
'culture' => $request->getCulture(),
Expand Down Expand Up @@ -91,7 +94,7 @@ public function createOrder(CreateOrderRequest $request)
private function generateSignature($data)
{
// fetch private key from file and ready it
$privateKey = file_get_contents($this->privateMerchantCertLocation);
$privateKey = $this->privateMerchantKey != null ? $this->privateMerchantKey : file_get_contents($this->privateMerchantCertLocation);
$pkeyid = openssl_pkey_get_private($privateKey);

// compute signature
Expand Down
Expand Up @@ -5,6 +5,7 @@ class CreateOrderRequest
private $orderId;
private $payCurrency;
private $payAmount;
private $receiveCurrency;
private $receiveAmount;
private $description;
private $culture;
Expand All @@ -14,26 +15,28 @@ class CreateOrderRequest

/**
* @param $orderId
* @param $payAmount
* @param $receiveAmount
* @param $payCurrency - Customer pay amount calculation currency
* @param $payAmount - Customer pay amount in calculation currency
* @param $receiveCurrency - Merchant receive amount calculation currency
* @param $receiveAmount - Merchant receive amount in calculation currency
* @param $description
* @param $culture
* @param $callbackUrl
* @param $successUrl
* @param $failureUrl
* @param string $payCurrency
*/
function __construct($orderId, $payAmount, $receiveAmount, $description, $culture, $callbackUrl, $successUrl, $failureUrl, $payCurrency = 'BTC')
function __construct($orderId, $payCurrency, $payAmount, $receiveCurrency, $receiveAmount, $description, $culture, $callbackUrl, $successUrl, $failureUrl)
{
$this->orderId = $orderId;
$this->payCurrency = $payCurrency;
$this->payAmount = $payAmount;
$this->receiveCurrency = $receiveCurrency;
$this->receiveAmount = $receiveAmount;
$this->description = $description;
$this->culture = $culture;
$this->callbackUrl = $callbackUrl;
$this->successUrl = $successUrl;
$this->failureUrl = $failureUrl;
$this->payCurrency = $payCurrency;
}

/**
Expand Down Expand Up @@ -68,6 +71,13 @@ public function getReceiveAmount()
return FormattingUtil::formatCurrency($this->receiveAmount == null ? 0.0 : $this->receiveAmount);
}

/**
* @return string
*/
public function getReceiveCurrency()
{
return $this->receiveCurrency;
}
/**
* @return string
*/
Expand Down
52 changes: 13 additions & 39 deletions modules/gateways/spectrocoin/order.php
@@ -1,22 +1,9 @@
<?php

include '../../../dbconnect.php';
include '../../../init.php';
include '../../../includes/functions.php';
include '../../../includes/gatewayfunctions.php';
include '../../../includes/invoicefunctions.php';
require_once 'lib/SCMerchantClient/SCMerchantClient.php';

function unitConversion($amount, $currencyFrom, $currencyTo)
{
$amount = urlencode($amount);
$currencyFrom = urlencode($currencyFrom);
$currencyTo = urlencode($currencyTo);
$get = file_get_contents("https://www.google.com/finance/converter?a=$amount&from=$currencyFrom&to=$currencyTo");
$get = explode("<span class=bld>",$get);
$get = explode("</span>",$get[1]);
return round(preg_replace("/[^0-9\.]/", null, $get[0]), 2);
}

$gatewaymodule = "spectrocoin";
$GATEWAY = getGatewayVariables($gatewaymodule);
// get invoice
Expand Down Expand Up @@ -46,21 +33,13 @@ function unitConversion($amount, $currencyFrom, $currencyTo)
$privateKeyFilePath = __DIR__ . '/keys/private';

if (!file_exists($privateKeyFilePath) || !is_file($privateKeyFilePath)
|| !$merchantId || !$appId
) {
|| !$merchantId || !$appId)
{
echo 'Spectrocoin is not fully configured. Please select different payment';
exit;
}

$receiveCurrency = strtoupper(trim($GATEWAY['receive_currency']));

if ($currency != $receiveCurrency) {
$receiveAmount = unitConversion($amount, $currency, $receiveCurrency);
} else {
$receiveAmount = $amount;
}
if ($receiveAmount < 0) {
error_log('Spectrocoin error. Could not convert amount to other currency');
if ($amount < 0) {
error_log('Spectrocoin error. Amount is negativ');
echo 'Spectrocoin is not fully configured. Please select different payment';
exit;
}
Expand All @@ -69,22 +48,17 @@ function unitConversion($amount, $currencyFrom, $currencyTo)
$callbackUrl = $options['systemURL'] . '/modules/gateways/callback/spectrocoin.php?invoice_id=' . $invoiceId;
$successUrl = $options['systemURL'] . '';
$cancelUrl = $options['systemURL'] . '/modules/gateways/callback/spectrocoin.php?cancel&invoice_id=' . $invoiceId;

$client = new SCMerchantClient($privateKeyFilePath, '', $merchantId, $appId);
$orderRequest = new CreateOrderRequest(null, 0, $receiveAmount, $orderDescription, "en", $callbackUrl, $successUrl, $cancelUrl);
$response = $client->createOrder($orderRequest);

$merchantApiUrl = 'https://spectrocoin.com/api/merchant/1';
$client = new SCMerchantClient($merchantApiUrl, $merchantId, $appId);
$privateKey = file_get_contents($privateKeyFilePath);
$client->setPrivateMerchantKey($privateKey);
$orderRequest = new CreateOrderRequest(null, "BTC", null, $currency, $amount, $orderDescription, "en", $callbackUrl, $successUrl, $cancelUrl);
$response =$client->createOrder($orderRequest);
if ($response instanceof ApiError) {
error_log('Error getting response from Spectrocoin. Error code: ' . $response->getCode() . ' Error message: ' . $response->getMessage());
echo 'Error getting response from Spectrocoin. Error code: ' . $response->getCode() . ' Error message: ' . $response->getMessage();
exit;
} else {
if ($response->getReceiveCurrency() != $receiveCurrency) {
echo 'Currencies does not match';
exit;
} else {
$redirectUrl = $response->getRedirectUrl();
header('Location: ' . $redirectUrl);
}

$redirectUrl = $response->getRedirectUrl();
header('Location: ' . $redirectUrl);
}

0 comments on commit b8dec0f

Please sign in to comment.