Skip to content

Commit 8647b96

Browse files
committed
Updated library to support Currency field
1 parent 9e0f44b commit 8647b96

File tree

3 files changed

+37
-25
lines changed

3 files changed

+37
-25
lines changed

FatZebra.class.php

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,7 @@
11
<?php
22
/**
33
* Fat Zebra PHP Gateway Library
4-
* Version 1.1.5
5-
*
6-
* Created February 2012 - Matthew Savage (matthew.savage@fatzebra.com.au)
7-
* Updated 20 February 2012 - Matthew Savage (matthew.savage@fatzebra.com.au)
8-
* Updated 19 April 2012 - Matthew Savage (matthew.savage@fatzebra.com.au)
9-
* - Added refund support
10-
* - Added tokenization support
11-
* Updated 10 July 2012 - Matthew Savage (matthew.savage@fatzebra.com.au)
12-
* - Added support for Plans, Customers and Subscriptions
4+
* Version 1.1.7
135
*
146
* The original source for this library, including its tests can be found at
157
* https://github.com/fatzebra/PHP-Library
@@ -31,7 +23,7 @@ class Gateway {
3123
/**
3224
* The version of this library
3325
*/
34-
public $version = "1.1.6";
26+
public $version = "1.1.7";
3527

3628
/**
3729
* The URL of the Fat Zebra gateway
@@ -114,9 +106,10 @@ public function purchase($request) {
114106
* @param float $amount the purchase amount
115107
* @param string $reference the purchase reference
116108
* @param string $cvv the card verification value - optional but recommended
109+
* @param string $currency the currency code for the transaction. Defaults to AUD
117110
* @return \StdObject
118111
*/
119-
public function token_purchase($token, $amount, $reference, $cvv = null) {
112+
public function token_purchase($token, $amount, $reference, $cvv = null, $currency = "AUD") {
120113
$customer_ip = $this->get_customer_ip();
121114

122115
if (function_exists('bcmul')) {
@@ -130,7 +123,8 @@ public function token_purchase($token, $amount, $reference, $cvv = null) {
130123
"card_token" => $token,
131124
"cvv" => $cvv,
132125
"amount" => $int_amount,
133-
"reference" => $reference
126+
"reference" => $reference,
127+
"currency" => $currency
134128
);
135129
return $this->do_request("POST", "/purchases", $payload);
136130
}
@@ -401,7 +395,7 @@ private function do_request($method, $uri, $payload = null) {
401395

402396
/**
403397
* Fetches the customers 'real' IP address (i.e. pulls out the address from X-Forwarded-For if present)
404-
*
398+
*
405399
* @return String the customers IP address
406400
*/
407401
private function get_customer_ip() {
@@ -455,6 +449,11 @@ class PurchaseRequest {
455449
*/
456450
private $fraud_data = null;
457451

452+
/**
453+
* Currency code for transaction
454+
*/
455+
private $currency = "AUD";
456+
458457
/**
459458
* Creates a new instance of the PurchaseRequest
460459
* @param float $amount the purchase amount
@@ -465,7 +464,7 @@ class PurchaseRequest {
465464
* @param string $cvv the card verification value
466465
* @return PurchaseRequest
467466
*/
468-
public function __construct($amount, $reference, $card_holder, $card_number, $expiry, $cvv, $fraud_data = null) {
467+
public function __construct($amount, $reference, $card_holder, $card_number, $expiry, $cvv, $fraud_data = null, $currency = "AUD") {
469468
if(is_null($amount)) throw new \InvalidArgumentException("Amount is a required field.");
470469
if((float)$amount < 0) throw new \InvalidArgumentException("Amount is invalid.");
471470
$this->amount = $amount;
@@ -494,20 +493,16 @@ public function __construct($amount, $reference, $card_holder, $card_number, $ex
494493
* @return \Array
495494
*/
496495
public function to_array() {
497-
if (function_exists('bcmul')) {
498-
$int_amount = intval(bcmul($this->amount, 100));
499-
} else {
500-
$multiplied = round($amount * 100);
501-
$int_amount = (int)$multiplied;
502-
}
503-
496+
$int_amount = Helpers::floatToInt($this->amount);
497+
504498
$data = array(
505499
"card_holder" => $this->card_holder,
506500
"card_number" => $this->card_number,
507501
"card_expiry" => $this->expiry,
508502
"cvv" => $this->cvv,
509503
"reference" => $this->reference,
510-
"amount" => $int_amount
504+
"amount" => $int_amount,
505+
"currency" => $this->currency
511506
);
512507
if (!is_null($this->fraud_data)) {
513508
$data['fraud'] = $this->fraud_data;

Helpers.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,21 @@ static public function iso3166_alpha3($alpha2) {
3636
$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");
3737
return isset($map[$alpha2]) ? $map[$alpha2] : $alpha2 ;
3838
}
39+
40+
/**
41+
* Convert a float to the integer value, using BCMul if available.
42+
* If BCMul is not available use the two-line cast method to avoid floating point precision issues
43+
*
44+
* @param float $input the input value
45+
* @return int the integer value of the conversion
46+
*/
47+
static public function floatToInt($input) {
48+
if (function_exists('bcmul')) {
49+
return intval(bcmul($input, 100));
50+
} else {
51+
$multiplied = round($input * 100);
52+
return (int)$multiplied;
53+
}
54+
55+
}
3956
}

README.markdown

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PHP API Library for Fat Zebra
22
==============================
33

4-
Release 1.1.0 for API version 1.0
4+
Release 1.1.7 for API version 1.0
55

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

3737
try {
3838
$gateway = new FatZebra\Gateway(USERNAME, TOKEN, TEST_MODE);
39-
$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']);
39+
$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');
4040

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

@@ -62,7 +62,7 @@ Usage
6262

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

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

0 commit comments

Comments
 (0)