Skip to content

Commit bd05f2e

Browse files
authored
Merge pull request #2 from JumboInteractiveLimited/fix_token_auth
Fix token auth
2 parents 49e6819 + e3e6238 commit bd05f2e

6 files changed

Lines changed: 75 additions & 12 deletions

File tree

FatZebra.class.php

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ class Gateway {
6868
*/
6969
private $ca = "";
7070

71+
/**
72+
* Customer real IP to send.
73+
*/
74+
private $customer_ip;
75+
7176
/**
7277
* Creates a new instance of the Fat Zebra gateway object
7378
* @param string $username the username for the gateway
@@ -226,12 +231,12 @@ public function authorization($amount, $reference, $card_holder, $card_number, $
226231
* Performs an authorization against the FatZebra gateway with a tokenized credit card
227232
* @param float $amount the purchase amount
228233
* @param string $reference the purchase reference
229-
* @param string $card_token the card token or alias for the authorization
234+
* @param string $token the card token or alias for the authorization
230235
* @param string $currency the currency code for the transaction. Defaults to AUD
231236
* @param array<string,string> $extra an assoc. array of extra params to merge into the request (e.g. metadata, fraud etc)
232237
* @return \StdObject
233238
*/
234-
public function token_authorization($amount, $reference, $card_token, $currency = "AUD", $extra = null) {
239+
public function token_authorization($amount, $reference, $token, $currency = "AUD", $extra = null) {
235240
if(is_null($amount)) throw new \InvalidArgumentException("Amount is a required field.");
236241
if(is_null($reference)) throw new \InvalidArgumentException("Reference is a required field.");
237242
if(strlen($reference) === 0) throw new \InvalidArgumentException("Reference is a required field.");
@@ -243,10 +248,10 @@ public function token_authorization($amount, $reference, $card_token, $currency
243248

244249
$payload = array(
245250
'customer_ip' => $customer_ip,
246-
'card_token' => $card_token,
247-
'reference' => $this->reference,
251+
'card_token' => $token,
252+
'reference' => $reference,
248253
'amount' => $int_amount,
249-
'currency' => $this->currency,
254+
'currency' => $currency,
250255
'capture' => false
251256
);
252257

@@ -479,18 +484,29 @@ private function do_request($method, $uri, $payload = null) {
479484
}
480485

481486
/**
482-
* Fetches the customers 'real' IP address (i.e. pulls out the address from X-Forwarded-For if present)
487+
* Get the currently set customer ip or fetches the customers 'real' IP
488+
* address (i.e. pulls out the address from X-Forwarded-For if present)
483489
*
484490
* @return String the customers IP address
485491
*/
486492
private function get_customer_ip() {
487-
$customer_ip = $_SERVER['REMOTE_ADDR'];
488-
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
489-
$forwarded_ips = explode(', ', $_SERVER['HTTP_X_FORWARDED_FOR']);
490-
$customer_ip = $forwarded_ips[0];
493+
if (!$this->customer_ip) {
494+
$this->customer_ip = $_SERVER['REMOTE_ADDR'];
495+
if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
496+
$forwarded_ips = explode(', ', $_SERVER['HTTP_X_FORWARDED_FOR']);
497+
$this->customer_ip = $forwarded_ips[0];
498+
}
491499
}
500+
return $this->customer_ip;
501+
}
492502

493-
return $customer_ip;
503+
/**
504+
* Allows explicitly setting the customer's IP address to be sent along with some requests.
505+
*
506+
* @return String the customers IP address
507+
*/
508+
public function set_customer_ip($customer_ip) {
509+
$this->customer_ip = $customer_ip;
494510
}
495511

496512
/**

README.markdown

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,13 @@ Usage
7676

7777
See the example folder for this example tied into a website.
7878

79+
Example
80+
-------
81+
82+
From within the `example` directory you can run `docker-compose up -d` and `docker-compose down` to start and stop (respectively) an apache web server running the php example.
83+
84+
Once up, you can access the example website at http://localhost:8080/example/index.php URL to give it a go.
85+
7986
Documentation
8087
-------------
8188

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"name": "fatzebra/fatzebra-php",
3-
"version": "1.2.1",
3+
"version": "1.2.2",
4+
"license": "GPL-2.0-or-later",
45
"repositories": [
56
{
67
"type": "vcs",

example/docker-compose.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
version: '2'
2+
3+
services:
4+
5+
web:
6+
image: 'php:7-apache'
7+
ports:
8+
- '8080:80'
9+
volumes:
10+
- '../:/var/www/html'

test/docker-compose.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
version: '2'
2+
3+
services:
4+
5+
tests:
6+
image: 'phpunit/phpunit:latest'
7+
entrypoint:
8+
- /bin/sh
9+
- -c
10+
command:
11+
- 'phpunit test/*.php'
12+
volumes:
13+
- '../:/app'

test/gateway_tests.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,22 @@ public function test_create_customer() {
240240
$this->assertTrue($result->successful);
241241
$this->assertNotNull($result->response->id);
242242
}
243+
244+
/**
245+
* Testing a token authorization
246+
*/
247+
public function test_token_authorization() {
248+
$gw = new FatZebra\Gateway("TEST", "TEST", true, GW_URL);
249+
$gw->timeout = 30;
250+
251+
$card = $gw->tokenize("Billy Blanks", "5123456789012346", "05/2023", "123");
252+
253+
$result = $gw->token_authorization(100.00, "UNITTEST" . rand(), $card->response->token);
254+
255+
$this->assertTrue($result->successful);
256+
$this->assertTrue($result->response->successful);
257+
$this->assertEquals($result->response->message, "Approved");
258+
}
243259
}
244260

245261
?>

0 commit comments

Comments
 (0)