Skip to content

Commit

Permalink
Updated library to support Currency field
Browse files Browse the repository at this point in the history
  • Loading branch information
amasses committed Jun 14, 2016
1 parent 9e0f44b commit 8647b96
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 25 deletions.
39 changes: 17 additions & 22 deletions FatZebra.class.php
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
<?php
/**
* Fat Zebra PHP Gateway Library
* Version 1.1.5
*
* Created February 2012 - Matthew Savage ([email protected])
* Updated 20 February 2012 - Matthew Savage ([email protected])
* Updated 19 April 2012 - Matthew Savage ([email protected])
* - Added refund support
* - Added tokenization support
* Updated 10 July 2012 - Matthew Savage ([email protected])
* - Added support for Plans, Customers and Subscriptions
* Version 1.1.7
*
* The original source for this library, including its tests can be found at
* https://github.com/fatzebra/PHP-Library
Expand All @@ -31,7 +23,7 @@ class Gateway {
/**
* The version of this library
*/
public $version = "1.1.6";
public $version = "1.1.7";

/**
* The URL of the Fat Zebra gateway
Expand Down Expand Up @@ -114,9 +106,10 @@ public function purchase($request) {
* @param float $amount the purchase amount
* @param string $reference the purchase reference
* @param string $cvv the card verification value - optional but recommended
* @param string $currency the currency code for the transaction. Defaults to AUD
* @return \StdObject
*/
public function token_purchase($token, $amount, $reference, $cvv = null) {
public function token_purchase($token, $amount, $reference, $cvv = null, $currency = "AUD") {
$customer_ip = $this->get_customer_ip();

if (function_exists('bcmul')) {
Expand All @@ -130,7 +123,8 @@ public function token_purchase($token, $amount, $reference, $cvv = null) {
"card_token" => $token,
"cvv" => $cvv,
"amount" => $int_amount,
"reference" => $reference
"reference" => $reference,
"currency" => $currency
);
return $this->do_request("POST", "/purchases", $payload);
}
Expand Down Expand Up @@ -401,7 +395,7 @@ private function do_request($method, $uri, $payload = null) {

/**
* Fetches the customers 'real' IP address (i.e. pulls out the address from X-Forwarded-For if present)
*
*
* @return String the customers IP address
*/
private function get_customer_ip() {
Expand Down Expand Up @@ -455,6 +449,11 @@ class PurchaseRequest {
*/
private $fraud_data = null;

/**
* Currency code for transaction
*/
private $currency = "AUD";

/**
* Creates a new instance of the PurchaseRequest
* @param float $amount the purchase amount
Expand All @@ -465,7 +464,7 @@ class PurchaseRequest {
* @param string $cvv the card verification value
* @return PurchaseRequest
*/
public function __construct($amount, $reference, $card_holder, $card_number, $expiry, $cvv, $fraud_data = null) {
public function __construct($amount, $reference, $card_holder, $card_number, $expiry, $cvv, $fraud_data = null, $currency = "AUD") {
if(is_null($amount)) throw new \InvalidArgumentException("Amount is a required field.");
if((float)$amount < 0) throw new \InvalidArgumentException("Amount is invalid.");
$this->amount = $amount;
Expand Down Expand Up @@ -494,20 +493,16 @@ public function __construct($amount, $reference, $card_holder, $card_number, $ex
* @return \Array
*/
public function to_array() {
if (function_exists('bcmul')) {
$int_amount = intval(bcmul($this->amount, 100));
} else {
$multiplied = round($amount * 100);
$int_amount = (int)$multiplied;
}

$int_amount = Helpers::floatToInt($this->amount);

$data = array(
"card_holder" => $this->card_holder,
"card_number" => $this->card_number,
"card_expiry" => $this->expiry,
"cvv" => $this->cvv,
"reference" => $this->reference,
"amount" => $int_amount
"amount" => $int_amount,
"currency" => $this->currency
);
if (!is_null($this->fraud_data)) {
$data['fraud'] = $this->fraud_data;
Expand Down
17 changes: 17 additions & 0 deletions Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,21 @@ static public function iso3166_alpha3($alpha2) {
$map = array("AD" => "AND", "AE" => "ARE", "AF" => "AFG", "AG" => "ATG", "AI" => "AIA", "AL" => "ALB", "AM" => "ARM", "AN" => "ANT", "AO" => "AGO", "AQ" => "ATA", "AR" => "ARG", "AS" => "ASM", "AT" => "AUT", "AU" => "AUS", "AW" => "ABW", "AX" => "ALA", "AZ" => "AZE", "BA" => "BIH", "BB" => "BRB", "BD" => "BGD", "BE" => "BEL", "BF" => "BFA", "BG" => "BGR", "BH" => "BHR", "BI" => "BDI", "BJ" => "BEN", "BL" => "BLM", "BM" => "BMU", "BN" => "BRN", "BO" => "BOL", "BQ" => "BES", "BR" => "BRA", "BS" => "BHS", "BT" => "BTN", "BV" => "BVT", "BW" => "BWA", "BY" => "BLR", "BZ" => "BLZ", "CA" => "CAN", "CC" => "CCK", "CD" => "COD", "CF" => "CAF", "CG" => "COG", "CH" => "CHE", "CI" => "CIV", "CK" => "COK", "CL" => "CHL", "CM" => "CMR", "CN" => "CHN", "CO" => "COL", "CR" => "CRI", "CU" => "CUB", "CV" => "CPV", "CW" => "CUW", "CX" => "CXR", "CY" => "CYP", "CZ" => "CZE", "DE" => "DEU", "DJ" => "DJI", "DK" => "DNK", "DM" => "DMA", "DO" => "DOM", "DZ" => "DZA", "EC" => "ECU", "EE" => "EST", "EG" => "EGY", "EH" => "ESH", "ER" => "ERI", "ES" => "ESP", "ET" => "ETH", "FI" => "FIN", "FJ" => "FJI", "FK" => "FLK", "FM" => "FSM", "FO" => "FRO", "FR" => "FRA", "GA" => "GAB", "GB" => "GBR", "GD" => "GRD", "GE" => "GEO", "GF" => "GUF", "GG" => "GGY", "GH" => "GHA", "GI" => "GIB", "GL" => "GRL", "GM" => "GMB", "GN" => "GIN", "GP" => "GLP", "GQ" => "GNQ", "GR" => "GRC", "GS" => "SGS", "GT" => "GTM", "GU" => "GUM", "GW" => "GNB", "GY" => "GUY", "HK" => "HKG", "HM" => "HMD", "HN" => "HND", "HR" => "HRV", "HT" => "HTI", "HU" => "HUN", "ID" => "IDN", "IE" => "IRL", "IL" => "ISR", "IM" => "IMN", "IN" => "IND", "IO" => "IOT", "IQ" => "IRQ", "IR" => "IRN", "IS" => "ISL", "IT" => "ITA", "JE" => "JEY", "JM" => "JAM", "JO" => "JOR", "JP" => "JPN", "KE" => "KEN", "KG" => "KGZ", "KH" => "KHM", "KI" => "KIR", "KM" => "COM", "KN" => "KNA", "KP" => "PRK", "KR" => "KOR", "KW" => "KWT", "KY" => "CYM", "KZ" => "KAZ", "LA" => "LAO", "LB" => "LBN", "LC" => "LCA", "LI" => "LIE", "LK" => "LKA", "LR" => "LBR", "LS" => "LSO", "LT" => "LTU", "LU" => "LUX", "LV" => "LVA", "LY" => "LBY", "MA" => "MAR", "MC" => "MCO", "MD" => "MDA", "ME" => "MNE", "MF" => "MAF", "MG" => "MDG", "MH" => "MHL", "MK" => "MKD", "ML" => "MLI", "MM" => "MMR", "MN" => "MNG", "MO" => "MAC", "MP" => "MNP", "MQ" => "MTQ", "MR" => "MRT", "MS" => "MSR", "MT" => "MLT", "MU" => "MUS", "MV" => "MDV", "MW" => "MWI", "MX" => "MEX", "MY" => "MYS", "MZ" => "MOZ", "NA" => "NAM", "NC" => "NCL", "NE" => "NER", "NF" => "NFK", "NG" => "NGA", "NI" => "NIC", "NL" => "NLD", "NO" => "NOR", "NP" => "NPL", "NR" => "NRU", "NU" => "NIU", "NZ" => "NZL", "OM" => "OMN", "PA" => "PAN", "PE" => "PER", "PF" => "PYF", "PG" => "PNG", "PH" => "PHL", "PK" => "PAK", "PL" => "POL", "PM" => "SPM", "PN" => "PCN", "PR" => "PRI", "PS" => "PSE", "PT" => "PRT", "PW" => "PLW", "PY" => "PRY", "QA" => "QAT", "RE" => "REU", "RO" => "ROU", "RS" => "SRB", "RU" => "RUS", "RW" => "RWA", "SA" => "SAU", "SB" => "SLB", "SC" => "SYC", "SD" => "SDN", "SE" => "SWE", "SG" => "SGP", "SH" => "SHN", "SI" => "SVN", "SJ" => "SJM", "SK" => "SVK", "SL" => "SLE", "SM" => "SMR", "SN" => "SEN", "SO" => "SOM", "SR" => "SUR", "SS" => "SSD", "ST" => "STP", "SV" => "SLV", "SX" => "SXM", "SY" => "SYR", "SZ" => "SWZ", "TC" => "TCA", "TD" => "TCD", "TF" => "ATF", "TG" => "TGO", "TH" => "THA", "TJ" => "TJK", "TK" => "TKL", "TL" => "TLS", "TM" => "TKM", "TN" => "TUN", "TO" => "TON", "TR" => "TUR", "TT" => "TTO", "TV" => "TUV", "TW" => "TWN", "TZ" => "TZA", "UA" => "UKR", "UG" => "UGA", "UM" => "UMI", "US" => "USA", "UY" => "URY", "UZ" => "UZB", "VA" => "VAT", "VC" => "VCT", "VE" => "VEN", "VG" => "VGB", "VI" => "VIR", "VN" => "VNM", "VU" => "VUT", "WF" => "WLF", "WS" => "WSM", "YE" => "YEM", "YT" => "MYT", "ZA" => "ZAF", "ZM" => "ZMB", "ZW" => "ZWE");
return isset($map[$alpha2]) ? $map[$alpha2] : $alpha2 ;
}

/**
* Convert a float to the integer value, using BCMul if available.
* If BCMul is not available use the two-line cast method to avoid floating point precision issues
*
* @param float $input the input value
* @return int the integer value of the conversion
*/
static public function floatToInt($input) {
if (function_exists('bcmul')) {
return intval(bcmul($input, 100));
} else {
$multiplied = round($input * 100);
return (int)$multiplied;
}

}
}
6 changes: 3 additions & 3 deletions README.markdown
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PHP API Library for Fat Zebra
==============================

Release 1.1.0 for API version 1.0
Release 1.1.7 for API version 1.0

A PHP library for the [Fat Zebra](https://www.fatzebra.com.au) Online Payment Gateway (for Australian Merchants)
Now supports recurring billing (subscriptions, plans, customers)
Expand Down Expand Up @@ -36,7 +36,7 @@ Usage

try {
$gateway = new FatZebra\Gateway(USERNAME, TOKEN, TEST_MODE);
$purchase_request = new FatZebra\PurchaseRequest($_POST['amount'], $_POST['reference'], $_POST['name'], $_POST['card_number'], $_POST['card_expiry_month'] ."/". $_POST['card_expiry_year'], $_POST['card_cvv']);
$purchase_request = new FatZebra\PurchaseRequest($_POST['amount'], $_POST['reference'], $_POST['name'], $_POST['card_number'], $_POST['card_expiry_month'] ."/". $_POST['card_expiry_year'], $_POST['card_cvv'], null, 'AUD');

$response = $gateway->purchase($purchase_request);

Expand All @@ -62,7 +62,7 @@ Usage

try {
$gateway = new FatZebra\Gateway(USERNAME, TOKEN, TEST_MODE);
$response = $gateway->token_purchase($_POST['token'], $amount, $reference);
$response = $gateway->token_purchase($_POST['token'], $amount, $reference, null, 'AUD');

$_SESSION['response'] = $response;
header("Location: index.php");
Expand Down

0 comments on commit 8647b96

Please sign in to comment.