File: /whmcs/modules/gateways/carrot.php
<?php
function carrot_config() {
$configarray = array(
"FriendlyName" => array("Type" => "System", "Value"=>"CarrotPay"),
"merchant" => array("FriendlyName" => "Merchant ID", "Type" => "text", "Size" => "30", "Description" => "Your Merchant ID", ),
"hashSeed" => array("FriendlyName" => "Hash Seed", "Type" => "text", "Size" => "30", "Description" => "Your Secret/HashSeed", ),
"btnURL" => array("FriendlyName" => "Button URL", "Type" => "text", "Size" => "30", "Value"=>"http://www.carrotpay.com/images/buttons/btn88x31.gif","Description" => "URL to Button (Do not change for default)", ),
);
return $configarray;
}// end function carrot_config()
function carrot_link($params) {
# Gateway Specific Variables
$gatewayusername = $params['merchant'];
$gatewayhashSeed = $params['hashSeed'];
$btnURL = $params['btnURL'];
# Invoice Variables
$invoiceid = $params['invoiceid'];
$description = $params["description"];
$amount = $params['amount'];
$currency = $params['currency']; # Currency Code
$word = "id".$invoiceid.$amount; // Used to create Hash
//$duedate = $params['duedate'];
# Client Variables
$firstname = $params['clientdetails']['firstname'];
$lastname = $params['clientdetails']['lastname'];
$email = $params['clientdetails']['email'];
$address1 = $params['clientdetails']['address1'];
$address2 = $params['clientdetails']['address2'];
$city = $params['clientdetails']['city'];
$state = $params['clientdetails']['state'];
$postcode = $params['clientdetails']['postcode'];
$country = $params['clientdetails']['country'];
$phone = $params['clientdetails']['phone'];
# System Variables
$companyname = $params['companyname'];
$systemurl = $params['systemurl'];
$currency = $params['currency'];
# Enter your code submit to the gateway...
$code = '<br/>
<script src="http://cdn.carrot.org/js/carrot.js" ></script>
<script type="text/javascript">
<!-- <![CDATA[
function payWithCarrots(thePrice) {
Carrot.pay({
price: thePrice,
merchant: "'.$gatewayusername.'",
description: "'.$description.'",
return_url: "'.$systemurl.'/modules/gateways/callback/carrotPayCB.php?invoiceid='.$invoiceid.'&value='.$amount.'¤cy='.$currency.'&hash=['.$word.']"
});
};
function showPurse() {
Carrot.purse();
}
$(document).ready(function() {
//Only for testing - not needed with live site
// Carrot.debug = 1;
// Carrot.purse_url = "https://secure.test.carrot.org:8443/webPurse.do";
// Carrot.payment_url = "https://secure.test.carrot.org:8443/payment";
$("#debug").html("<p>Looking for ID ...</p>");
Carrot.get_id(function(id) {
if(id==null)
$("#debug").html("<p>No id found</p>");
else
$("#debug").html("<p>"+id+"</p>");
});
});
// ]]> -->
</script>
<a href="javascript:payWithCarrots(\''.$amount.':'.$currency.'\')"><img src="'.$btnURL.'" alt="Pay with CarrotPay"/></a>
';
return $code;
}
?>
File: /whmcs/modules/gateways/carrot/hashword.php
<?php
function hash_word($word, $price, $seed)
{
// Lower case and strip dashes from seed
$seed = strtolower($seed);
$seed = str_replace("-", "", $seed);
// Construct hash text from seed, price and word
// Note space delimited
$text = "$word $price $seed";
// Get md5 (hex string)
$md5 = md5($text);
// Get last 8 hex chars (32 bits, unsigned)
$hex = substr($md5, -8);
// Replace with 'safe' alphabet
$safe = strtr($hex, "0123456789abcdef", "bcdghjklmpqrsvwz");
// Trim off leading 'b' (zero)
return ltrim($safe, "b");
}
?>
File: /whmcs/modules/gateways/callback/carrotPayCB.php
<?php
# Required File Includes
include("../../../dbconnect.php");
include("../../../includes/functions.php");
include("../../../includes/gatewayfunctions.php");
include("../../../includes/invoicefunctions.php");
$gatewaymodule = "carrot"; # Enter your gateway module name here replacing template
$GATEWAY = getGatewayVariables($gatewaymodule);
if (!$GATEWAY["type"]) die("Module Not Activated"); # Checks gateway module is active before accepting callback
# Required by CarrotPay for verification
include ("../carrot/hashword.php");
$hash = $_GET['hash'];
$invoiceid = $_GET['invoiceid'];
$amount = $_GET['value']; //Gets the actual price
$currency = $_GET['currency'];
$word = "id".$invoiceid.$amount; // Used to create Hash
$price = $amount.":".$currency; //Converts the pirce into carrots
$seed = $GATEWAY["hashSeed"];
$hashval = hash_word($word,$price,$seed);
$systemurl = $params['systemurl'];
#Checks if transaction is valid
$status="0";
if ($hashval == $hash) {
$status = "1"; // set to '1' if valid
}
$transid = $hash;
$fee = 0;
//$invoiceid = checkCbInvoiceID($invoiceid,$GATEWAY["name"]); # Checks invoice ID is a valid invoice number or ends processing
//checkCbTransID($transid); # Checks transaction number isn't already in the database and ends processing if it does
if ($status=="1") {
# Successful
echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"1; URL=/billing/viewinvoice.php?id=$invoiceid\"><h2>Redirecting</h2><p>Your payment was <b>sucessful</b> and you are being redirected now.</p><p>If you page does not redirect in 5 seconds, click <a href=\"/billing/viewinvoice.php?id=$invoiceid\">here</a>.</p>";
addInvoicePayment($invoiceid,$transid,$amount,$fee,$gatewaymodule);
logTransaction($GATEWAY["name"],$_POST,"Successful");
} else {
# Unsuccessful
echo "<META HTTP-EQUIV=\"Refresh\" CONTENT=\"1; URL=/billing/viewinvoice.php?id=$invoiceid\"><h2>Redirecting</h2><p>Your payment has <b>failed</b>. An error occured during payment and you are being redirected now.</p><p>If your page does not redirect in 5 seconds, click <a href=\"/billing/viewinvoice.php?id=$invoiceid\">here</a>.</p>";
logTransaction($GATEWAY["name"],$_POST,"Unsuccessful");
}
?>