diff --git a/README.md b/README.md index 1e6e35f..43d65e8 100644 --- a/README.md +++ b/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 @@ -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 \ No newline at end of file diff --git a/modules/gateways/callback/spectrocoin.php b/modules/gateways/callback/spectrocoin.php index 762657a..1a9a17a 100644 --- a/modules/gateways/callback/spectrocoin.php +++ b/modules/gateways/callback/spectrocoin.php @@ -1,6 +1,6 @@ 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'); diff --git a/modules/gateways/spectrocoin.php b/modules/gateways/spectrocoin.php index 9ea3e56..53fd1ca 100644 --- a/modules/gateways/spectrocoin.php +++ b/modules/gateways/spectrocoin.php @@ -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; } diff --git a/modules/gateways/spectrocoin/lib/SCMerchantClient/SCMerchantClient.php b/modules/gateways/spectrocoin/lib/SCMerchantClient/SCMerchantClient.php index be33595..e17d40b 100644 --- a/modules/gateways/spectrocoin/lib/SCMerchantClient/SCMerchantClient.php +++ b/modules/gateways/spectrocoin/lib/SCMerchantClient/SCMerchantClient.php @@ -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'); @@ -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(), @@ -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 diff --git a/modules/gateways/spectrocoin/lib/SCMerchantClient/messages/CreateOrderRequest.php b/modules/gateways/spectrocoin/lib/SCMerchantClient/messages/CreateOrderRequest.php index 7a4ebaf..5168a5f 100644 --- a/modules/gateways/spectrocoin/lib/SCMerchantClient/messages/CreateOrderRequest.php +++ b/modules/gateways/spectrocoin/lib/SCMerchantClient/messages/CreateOrderRequest.php @@ -5,6 +5,7 @@ class CreateOrderRequest private $orderId; private $payCurrency; private $payAmount; + private $receiveCurrency; private $receiveAmount; private $description; private $culture; @@ -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; } /** @@ -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 */ diff --git a/modules/gateways/spectrocoin/order.php b/modules/gateways/spectrocoin/order.php index 997392e..7672a3c 100644 --- a/modules/gateways/spectrocoin/order.php +++ b/modules/gateways/spectrocoin/order.php @@ -1,22 +1,9 @@ ",$get); - $get = explode("",$get[1]); - return round(preg_replace("/[^0-9\.]/", null, $get[0]), 2); -} - $gatewaymodule = "spectrocoin"; $GATEWAY = getGatewayVariables($gatewaymodule); // get invoice @@ -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; } @@ -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); } \ No newline at end of file