PxPay_Sample_Curl.php ➔ print_result()   B
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 84
Code Lines 58

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 58
nc 3
nop 0
dl 0
loc 84
rs 8.7169
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 41 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
      # this is a redirection from the payments page.
27
      print_result();
28
  } elseif (isset($_REQUEST["Submit"])) {
29
      # this is a post back -- redirect to payments page.
30
      redirect_form();
31
  } else {
32
      # this is a fresh request -- display the purchase form.
33
      print_form();
34
  }
35
36
37
#******************************************************************************
38
# This function receives information back from the payments page,
39
# and displays it to the user.
40
#******************************************************************************
41
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...
42
{
43
    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...
44
45
    $enc_hex = $_REQUEST["result"];
46
    #getResponse method in PxPay object returns PxPayResponse object
47
    #which encapsulates all the response data
48
    $rsp = $pxpay->getResponse($enc_hex);
49
50
51
    # the following are the fields available in the PxPayResponse object
52
  $Success           = $rsp->getSuccess();   # =1 when request succeeds
53
  $AmountSettlement  = $rsp->getAmountSettlement();
54
    $AuthCode          = $rsp->getAuthCode();  # from bank
55
  $CardName          = $rsp->getCardName();  # e.g. "Visa"
56
  $CardNumber        = $rsp->getCardNumber(); # Truncated card number
57
  $DateExpiry        = $rsp->getDateExpiry(); # in mmyy format
58
  $DpsBillingId      = $rsp->getDpsBillingId();
59
    $BillingId    	 = $rsp->getBillingId();
60
    $CardHolderName    = $rsp->getCardHolderName();
61
    $DpsTxnRef	     = $rsp->getDpsTxnRef();
62
    $TxnType           = $rsp->getTxnType();
63
    $TxnData1          = $rsp->getTxnData1();
64
    $TxnData2          = $rsp->getTxnData2();
65
    $TxnData3          = $rsp->getTxnData3();
66
    $CurrencySettlement= $rsp->getCurrencySettlement();
67
    $ClientInfo        = $rsp->getClientInfo(); # The IP address of the user who submitted the transaction
68
    $TxnId             = $rsp->getTxnId();
69
    $CurrencyInput     = $rsp->getCurrencyInput();
70
    $EmailAddress      = $rsp->getEmailAddress();
71
    $MerchantReference = $rsp->getMerchantReference();
72
    $ResponseText		 = $rsp->getResponseText();
73
    $TxnMac            = $rsp->getTxnMac(); # An indication as to the uniqueness of a card used in relation to others
74
75
    if ($rsp->getSuccess() == "1") {
76
        $result = "The transaction was approved.";
77
    
78
        # Sending invoices/updating order status within database etc.
79
    
80
        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...
81
            # Send emails, generate invoices, update order status etc.
82
        }
83
    } else {
84
        $result = "The transaction was declined.";
85
    }
86
87
    print <<<HTMLEOF
88
<html>
89
<head>
90
<title>Payment Express PxPay transaction result</title>
91
</head>
92
<body>
93
<h1>Payment Express PxPay transaction result</h1>
94
<p>$result</p>
95
  <table border=1>
96
	<tr><th>Name</th>				<th>Value</th> </tr>
97
	<tr><td>Success</td>			<td>$Success</td></tr>
98
	<tr><td>TxnType</td>			<td>$TxnType</td></tr>
99
	<tr><td>CurrencyInput</td>		<td>$CurrencyInput</td></tr>
100
	<tr><td>MerchantReference</td>	<td>$MerchantReference</td></tr>
101
	<tr><td>TxnData1</td>			<td>$TxnData1</td></tr>
102
	<tr><td>TxnData2</td>			<td>$TxnData2</td></tr>
103
	<tr><td>TxnData3</td>			<td>$TxnData3</td></tr>
104
	<tr><td>AuthCode</td>			<td>$AuthCode</td></tr>
105
	<tr><td>CardName</td>			<td>$CardName</td></tr>
106
	<tr><td>CardHolderName</td>		<td>$CardHolderName</td></tr>
107
	<tr><td>CardNumber</td>			<td>$CardNumber</td></tr>
108
	<tr><td>DateExpiry</td>			<td>$DateExpiry</td></tr>
109
	<tr><td>CardHolderName</td>		<td>$CardHolderName</td></tr>
110
	<tr><td>ClientInfo</td>			<td>$ClientInfo</td></tr>
111
	<tr><td>TxnId</td>				<td>$TxnId</td></tr>
112
	<tr><td>EmailAddress</td>		<td>$EmailAddress</td></tr>
113
	<tr><td>DpsTxnRef</td>			<td>$DpsTxnRef</td></tr>
114
	<tr><td>BillingId</td>			<td>$BillingId</td></tr>
115
	<tr><td>DpsBillingId</td>		<td>$DpsBillingId</td></tr>
116
	<tr><td>AmountSettlement</td>	<td>$AmountSettlement</td></tr>
117
	<tr><td>CurrencySettlement</td>	<td>$CurrencySettlement</td></tr>
118
	<tr><td>TxnMac</td>				<td>$TxnMac</td></tr>
119
	<tr><td>ResponseText</td>		<td>$ResponseText</td></tr>
120
</table>
121
</body>
122
</html>
123
HTMLEOF;
124
}
125
126
#******************************************************************************
127
# Database lookup to check the status of the order or shopping cart
128
#******************************************************************************
129
130
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...
131
{
132
    # Check database if order relating to TxnId has alread been processed
133
    return false;
134
}
135
136
#******************************************************************************
137
# This function prints a blank purchase form.
138
#******************************************************************************
139
function print_form()
140
{
141
    print <<<HTMLEOF
142
<html>
143
<head>
144
<title>Payment Express PxPay transaction sample</title>
145
</head>
146
<body>
147
<h1>Payment Express PxPay transaction result</h1>
148
<p>
149
You have indicated you would like to buy some widgets.
150
</p>
151
<p>
152
Please enter the number of widgets below, and enter your
153
shipping details.
154
</p>
155
<form method="post">
156
<table>
157
  <tr>
158
    <td>Quantity:</td>
159
    <td><input name="Quantity" type="text"/></td>
160
    <td>@ $19.95 ea</td>
161
  </tr>
162
  <tr>
163
    <td>Reference:</td>
164
    <td><input name="Reference" type="text"/></td>
165
  </tr>  
166
  <tr>
167
    <td>Ship to</td>
168
    <td></td>
169
  </tr>
170
  <tr>
171
    <td>Address line 1:</td>
172
    <td><input name="Address1" type="text"/></td>
173
  </tr>
174
  <tr>
175
    <td>Address line 2</td>
176
    <td><input name="Address2" type="text"/></td>
177
  </tr>
178
    <tr>
179
    <td>Address line 3</td>
180
    <td><input name="Address3" type="text"/></td>
181
  </tr>
182
</table>
183
<input name="Submit" type="submit" value="Submit"/>
184
Click submit to go to the secure payment page.
185
</form>
186
</body>
187
</html>
188
HTMLEOF;
189
}
190
191
#******************************************************************************
192
# This function formats data into a request and redirects to the
193
# Payments Page.
194
#******************************************************************************
195
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...
196
{
197
    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...
198
199
    $request = new PxPayRequest();
200
201
    $http_host   = getenv("HTTP_HOST");
202
    $request_uri = getenv("SCRIPT_NAME");
203
    $server_url  = "http://$http_host";
204
    #$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...
205
    #$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...
206
    $script_url = (version_compare(PHP_VERSION, "4.3.4", ">=")) ?"$server_url$request_uri" : "$server_url/$request_uri";
207
208
209
    # the following variables are read from the form
210
    $Quantity = $_REQUEST["Quantity"];
211
    $MerchantReference = $_REQUEST["Reference"];
212
    $Address1 = $_REQUEST["Address1"];
213
    $Address2 = $_REQUEST["Address2"];
214
    $Address3 = $_REQUEST["Address3"];
215
  
216
    #Calculate AmountInput
217
    $AmountInput = 19.95 * $Quantity;
218
  
219
    #Generate a unique identifier for the transaction
220
    $TxnId = uniqid("ID");
221
  
222
    #Set PxPay properties
223
    $request->setMerchantReference($MerchantReference);
224
    $request->setAmountInput($AmountInput);
225
    $request->setTxnData1($Address1);
226
    $request->setTxnData2($Address2);
227
    $request->setTxnData3($Address3);
228
    $request->setTxnType("Purchase");
229
    $request->setCurrencyInput("NZD");
230
    $request->setEmailAddress("[email protected]");
231
    $request->setUrlFail($script_url);			# can be a dedicated failure page
232
  $request->setUrlSuccess($script_url);			# can be a dedicated success page
233
  $request->setTxnId($TxnId);
234
  
235
    #The following properties are not used in this case
236
    # $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...
237
    # $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...
238
    # $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...
239
  
240
241
  
242
    #Call makeRequest function to obtain input XML
243
    $request_string = $pxpay->makeRequest($request);
244
   
245
    #Obtain output XML
246
    $response = new MifMessage($request_string);
247
  
248
    #Parse output XML
249
    $url = $response->get_element_text("URI");
250
    $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...
251
   
252
    #Redirect to payment page
253
    header("Location: ".$url);
254
}
255