1
|
|
|
<?php
|
|
|
|
|
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()
|
|
|
|
|
47
|
|
|
{
|
48
|
|
|
global $pxpay;
|
|
|
|
|
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))
|
|
|
|
|
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)
|
|
|
|
|
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()
|
|
|
|
|
206
|
|
|
{
|
207
|
|
|
global $pxpay;
|
|
|
|
|
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
|
|
|
|
|
215
|
|
|
#$script_url = "$server_url$request_uri"; //Using this code after PHP version 4.3.4
|
|
|
|
|
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);
|
|
|
|
|
247
|
|
|
# $request->setBillingId($BillingId);
|
|
|
|
|
248
|
|
|
# $request->setOpt($Opt);
|
|
|
|
|
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");
|
|
|
|
|
261
|
|
|
|
262
|
|
|
#Redirect to payment page
|
263
|
|
|
header("Location: ".$url);
|
264
|
|
|
}
|
265
|
|
|
?>
|
|
|
|
|
266
|
|
|
|
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.