Completed
Push — master ( bb025d...541291 )
by
unknown
04:05
created

PxPay_Sample_Curl.php ➔ isProcessed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 46 and the first side effect is on line 14.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
#******************************************************************************
3
#* Name          	: PxPay_Sample_Curl.php
4
#* Description   	: Payment Express PxPay PHP cURL Sample
5
#* Copyright	 	: Payment Express 2017(c)
6
#* Date          	: 2017-04-10
7
#* References    	: https://www.paymentexpress.com/developer-e-commerce-paymentexpress-hosted-pxpay
8
#*@version 		    : 2.0
9
#* Author 		    : Payment Express DevSupport
10
#******************************************************************************
11
12
# This file is a sample demonstrating integration with the PxPay interface using PHP with the cURL extension installed.  
13
#Inlcude PxPay objects
14
include "PxPay_Curl.inc.php";
15
16
   $PxPay_Url    = "https://sec.paymentexpress.com/pxaccess/pxpay.aspx";
17
  $PxPay_Userid = "PratikG_Dev";//"PratikPxPay_Dev"; #Important! Update with your UserId
18
  $PxPay_Key    =  "e99e0f8b19e27ea73094875cf931c4539146924c7bc054caeda486b67feb6078";//"e99e0f8b19e27ea73094875cf931c4539146924c7bc054caeda486b67feb6078"; #Important! Update with your Key
19
  #
20
  # MAIN
21
  #
22
23
  $pxpay = new PxPay_Curl( $PxPay_Url, $PxPay_Userid, $PxPay_Key );
24
25
  if (isset($_REQUEST["result"]))
26
  {
27
    # this is a redirection from the payments page.
28
    print_result();
29
  }
30
  elseif (isset($_REQUEST["Submit"]))
31
  {
32
    # this is a post back -- redirect to payments page.
33
    redirect_form();
34
  }
35
  else
36
  {
37
    # this is a fresh request -- display the purchase form.
38
    print_form();
39
  }
40
41
42
#******************************************************************************
43
# This function receives information back from the payments page,
44
# and displays it to the user.
45
#******************************************************************************
46
function print_result()
0 ignored issues
show
Coding Style introduced by
print_result uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
47
{
48
  global $pxpay;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
49
50
  $enc_hex = $_REQUEST["result"];
51
  #getResponse method in PxPay object returns PxPayResponse object
52
  #which encapsulates all the response data
53
  $rsp = $pxpay->getResponse($enc_hex);
54
55
56
  # the following are the fields available in the PxPayResponse object
57
  $Success           = $rsp->getSuccess();   # =1 when request succeeds
58
  $AmountSettlement  = $rsp->getAmountSettlement();
59
  $AuthCode          = $rsp->getAuthCode();  # from bank
60
  $CardName          = $rsp->getCardName();  # e.g. "Visa"
61
  $CardNumber        = $rsp->getCardNumber(); # Truncated card number
62
  $DateExpiry        = $rsp->getDateExpiry(); # in mmyy format
63
  $DpsBillingId      = $rsp->getDpsBillingId();
64
  $BillingId    	 = $rsp->getBillingId();
65
  $CardHolderName    = $rsp->getCardHolderName();
66
  $DpsTxnRef	     = $rsp->getDpsTxnRef();
67
  $TxnType           = $rsp->getTxnType();
68
  $TxnData1          = $rsp->getTxnData1();
69
  $TxnData2          = $rsp->getTxnData2();
70
  $TxnData3          = $rsp->getTxnData3();
71
  $CurrencySettlement= $rsp->getCurrencySettlement();
72
  $ClientInfo        = $rsp->getClientInfo(); # The IP address of the user who submitted the transaction
73
  $TxnId             = $rsp->getTxnId();
74
  $CurrencyInput     = $rsp->getCurrencyInput();
75
  $EmailAddress      = $rsp->getEmailAddress();
76
  $MerchantReference = $rsp->getMerchantReference();
77
  $ResponseText		 = $rsp->getResponseText();
78
  $TxnMac            = $rsp->getTxnMac(); # An indication as to the uniqueness of a card used in relation to others
79
80
  if ($rsp->getSuccess() == "1")
81
  {
82
    $result = "The transaction was approved.";
83
	
84
		# Sending invoices/updating order status within database etc.
85
	
86
	if (!isProcessed($TxnId))
0 ignored issues
show
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
87
	{
88
		# Send emails, generate invoices, update order status etc.
89
	}
90
	
91
  }
92
  else
93
  {
94
    $result = "The transaction was declined.";
95
  }
96
97
  print <<<HTMLEOF
98
<html>
99
<head>
100
<title>Payment Express PxPay transaction result</title>
101
</head>
102
<body>
103
<h1>Payment Express PxPay transaction result</h1>
104
<p>$result</p>
105
  <table border=1>
106
	<tr><th>Name</th>				<th>Value</th> </tr>
107
	<tr><td>Success</td>			<td>$Success</td></tr>
108
	<tr><td>TxnType</td>			<td>$TxnType</td></tr>
109
	<tr><td>CurrencyInput</td>		<td>$CurrencyInput</td></tr>
110
	<tr><td>MerchantReference</td>	<td>$MerchantReference</td></tr>
111
	<tr><td>TxnData1</td>			<td>$TxnData1</td></tr>
112
	<tr><td>TxnData2</td>			<td>$TxnData2</td></tr>
113
	<tr><td>TxnData3</td>			<td>$TxnData3</td></tr>
114
	<tr><td>AuthCode</td>			<td>$AuthCode</td></tr>
115
	<tr><td>CardName</td>			<td>$CardName</td></tr>
116
	<tr><td>CardHolderName</td>		<td>$CardHolderName</td></tr>
117
	<tr><td>CardNumber</td>			<td>$CardNumber</td></tr>
118
	<tr><td>DateExpiry</td>			<td>$DateExpiry</td></tr>
119
	<tr><td>CardHolderName</td>		<td>$CardHolderName</td></tr>
120
	<tr><td>ClientInfo</td>			<td>$ClientInfo</td></tr>
121
	<tr><td>TxnId</td>				<td>$TxnId</td></tr>
122
	<tr><td>EmailAddress</td>		<td>$EmailAddress</td></tr>
123
	<tr><td>DpsTxnRef</td>			<td>$DpsTxnRef</td></tr>
124
	<tr><td>BillingId</td>			<td>$BillingId</td></tr>
125
	<tr><td>DpsBillingId</td>		<td>$DpsBillingId</td></tr>
126
	<tr><td>AmountSettlement</td>	<td>$AmountSettlement</td></tr>
127
	<tr><td>CurrencySettlement</td>	<td>$CurrencySettlement</td></tr>
128
	<tr><td>TxnMac</td>				<td>$TxnMac</td></tr>
129
	<tr><td>ResponseText</td>		<td>$ResponseText</td></tr>
130
</table>
131
</body>
132
</html>
133
HTMLEOF;
134
}
135
136
#******************************************************************************
137
# Database lookup to check the status of the order or shopping cart
138
#******************************************************************************
139
140
function isProcessed($TxnId)
0 ignored issues
show
Unused Code introduced by
The parameter $TxnId is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
141
{
142
	# Check database if order relating to TxnId has alread been processed
143
	return false;
144
}
145
146
#******************************************************************************
147
# This function prints a blank purchase form.
148
#******************************************************************************
149
function print_form()
150
{
151
  print <<<HTMLEOF
152
<html>
153
<head>
154
<title>Payment Express PxPay transaction sample</title>
155
</head>
156
<body>
157
<h1>Payment Express PxPay transaction result</h1>
158
<p>
159
You have indicated you would like to buy some widgets.
160
</p>
161
<p>
162
Please enter the number of widgets below, and enter your
163
shipping details.
164
</p>
165
<form method="post">
166
<table>
167
  <tr>
168
    <td>Quantity:</td>
169
    <td><input name="Quantity" type="text"/></td>
170
    <td>@ $19.95 ea</td>
171
  </tr>
172
  <tr>
173
    <td>Reference:</td>
174
    <td><input name="Reference" type="text"/></td>
175
  </tr>  
176
  <tr>
177
    <td>Ship to</td>
178
    <td></td>
179
  </tr>
180
  <tr>
181
    <td>Address line 1:</td>
182
    <td><input name="Address1" type="text"/></td>
183
  </tr>
184
  <tr>
185
    <td>Address line 2</td>
186
    <td><input name="Address2" type="text"/></td>
187
  </tr>
188
    <tr>
189
    <td>Address line 3</td>
190
    <td><input name="Address3" type="text"/></td>
191
  </tr>
192
</table>
193
<input name="Submit" type="submit" value="Submit"/>
194
Click submit to go to the secure payment page.
195
</form>
196
</body>
197
</html>
198
HTMLEOF;
199
}
200
201
#******************************************************************************
202
# This function formats data into a request and redirects to the
203
# Payments Page.
204
#******************************************************************************
205
function redirect_form()
0 ignored issues
show
Coding Style introduced by
redirect_form uses the super-global variable $_REQUEST which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
206
{
207
  global $pxpay;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
208
209
  $request = new PxPayRequest();
210
211
  $http_host   = getenv("HTTP_HOST");
212
  $request_uri = getenv("SCRIPT_NAME");
213
  $server_url  = "http://$http_host";
214
  #$script_url  = "$server_url/$request_uri"; //using this code before PHP version 4.3.4
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
215
  #$script_url  = "$server_url$request_uri"; //Using this code after PHP version 4.3.4
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
216
  $script_url = (version_compare(PHP_VERSION, "4.3.4", ">=")) ?"$server_url$request_uri" : "$server_url/$request_uri";
217
218
219
  # the following variables are read from the form
220
  $Quantity = $_REQUEST["Quantity"];
221
  $MerchantReference = $_REQUEST["Reference"];  
222
  $Address1 = $_REQUEST["Address1"];
223
  $Address2 = $_REQUEST["Address2"];
224
  $Address3 = $_REQUEST["Address3"];
225
  
226
  #Calculate AmountInput
227
  $AmountInput = 19.95 * $Quantity;
228
  
229
  #Generate a unique identifier for the transaction
230
  $TxnId = uniqid("ID");
231
  
232
  #Set PxPay properties
233
  $request->setMerchantReference($MerchantReference);
234
  $request->setAmountInput($AmountInput);
235
  $request->setTxnData1($Address1);
236
  $request->setTxnData2($Address2);
237
  $request->setTxnData3($Address3);
238
  $request->setTxnType("Purchase");
239
  $request->setCurrencyInput("NZD");
240
  $request->setEmailAddress("[email protected]");
241
  $request->setUrlFail($script_url);			# can be a dedicated failure page
242
  $request->setUrlSuccess($script_url);			# can be a dedicated success page
243
  $request->setTxnId($TxnId);  
244
  
245
  #The following properties are not used in this case
246
  # $request->setEnableAddBillCard($EnableAddBillCard);    
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
247
  # $request->setBillingId($BillingId);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
248
  # $request->setOpt($Opt);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
249
  
250
251
  
252
  #Call makeRequest function to obtain input XML
253
  $request_string = $pxpay->makeRequest($request);
254
   
255
  #Obtain output XML
256
  $response = new MifMessage($request_string);
257
  
258
  #Parse output XML
259
  $url = $response->get_element_text("URI");
260
  $valid = $response->get_attribute("valid");
0 ignored issues
show
Unused Code introduced by
$valid is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
261
   
262
   #Redirect to payment page
263
   header("Location: ".$url);
264
}
265
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
266