DpsPxPayComs   B
last analyzed

Complexity

Total Complexity 38

Size/Duplication

Total Lines 270
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Importance

Changes 0
Metric Value
dl 0
loc 270
rs 8.3999
c 0
b 0
f 0
wmc 38
lcom 1
cbo 7

19 Methods

Rating   Name   Duplication   Size   Complexity  
A get_txn_type() 0 5 2
A setTxnData1() 0 4 1
A setTxnData2() 0 4 1
A setTxnData3() 0 4 1
A setEmailAddress() 0 4 1
A setAmountInput() 0 4 1
A setMerchantReference() 0 4 1
A setCurrencyInput() 0 4 1
A setTxnType() 0 4 1
A setTxnId() 0 4 1
A setUrlFail() 0 4 1
A setUrlSuccess() 0 4 1
A setEnableAddBillCard() 0 4 1
A setBillingId() 0 4 1
B __construct() 0 22 5
F startPaymentProcess() 0 78 15
A processRequestAndReturnResultsAsObject() 0 6 1
A getDebugMessage() 0 8 1
A debug() 0 5 1
1
<?php
2
/**
3
 *@author nicolaas [at] sunnysideup.co.nz
4
 **/
5
6
class DpsPxPayComs extends Object
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
7
{
8
    private static $pxpay_url = 'https://sec.paymentexpress.com/pxaccess/pxpay.aspx';
0 ignored issues
show
Unused Code introduced by
The property $pxpay_url is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
9
10
    private static $pxpay_userid = "";
0 ignored issues
show
Unused Code introduced by
The property $pxpay_userid is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
11
12
    private static $pxpay_encryption_key =  "";
0 ignored issues
show
Unused Code introduced by
The property $pxpay_encryption_key is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
13
14
    private static $alternative_thirdparty_folder =  "";
15
16
    private static $overriding_txn_type =  ""; //e.g. AUTH
0 ignored issues
show
Unused Code introduced by
The property $overriding_txn_type is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
17
18
    public static function get_txn_type()
19
    {
20
        $overridingTxnType = Config::inst()->get("DpsPxPayComs", "overriding_txn_type");
21
        return $overridingTxnType  ? $overridingTxnType : "Purchase";
22
    }
23
24
25
    /**
26
    * customer details
27
    **/
28
29
    protected $TxnData1 = "";
30
    public function setTxnData1($v)
31
    {
32
        $this->TxnData1 = $v;
33
    }
34
    protected $TxnData2 = "";
35
    public function setTxnData2($v)
36
    {
37
        $this->TxnData2 = $v;
38
    }
39
    protected $TxnData3 = "";
40
    public function setTxnData3($v)
41
    {
42
        $this->TxnData3 = $v;
43
    }
44
    protected $EmailAddress = "";
45
    public function setEmailAddress($v)
46
    {
47
        $this->EmailAddress = $v;
48
    }
49
50
    /**
51
    * order details
52
    **/
53
    protected $AmountInput = 0;
54
    public function setAmountInput($v)
55
    {
56
        $this->AmountInput = $v;
57
    }
58
59
    protected $MerchantReference = "";
60
    public function setMerchantReference($v)
61
    {
62
        $this->MerchantReference = $v;
63
    }
64
65
    protected $CurrencyInput = "NZD";
66
    public function setCurrencyInput($v)
67
    {
68
        $this->CurrencyInput = $v;
69
    }
70
71
    protected $TxnType = "Purchase";
72
    public function setTxnType($v)
73
    {
74
        $this->TxnType = $v;
75
    }
76
77
    protected $TxnId = "";
78
    public function setTxnId($v)
79
    {
80
        $this->TxnId = $v;
81
    }
82
83
    /**
84
    * details of the redirection
85
    **/
86
    protected $UrlFail = "";
87
    public function setUrlFail($v)
88
    {
89
        $this->UrlFail = $v;
90
    }
91
    protected $UrlSuccess = "";
92
    public function setUrlSuccess($v)
93
    {
94
        $this->UrlSuccess = $v;
95
    }
96
97
    /**
98
     * Details to use stored cards
99
     */
100
    protected $EnableAddBillCard = 0;
101
    public function setEnableAddBillCard($v)
102
    {
103
        $this->EnableAddBillCard = $v;
104
    }
105
    protected $BillingId = 0;
106
    public function setBillingId($v)
107
    {
108
        $this->BillingId = $v;
109
    }
110
111
    /**
112
    * external object
113
    **/
114
    protected $PxPayObject = null;
115
    protected $response = null;
116
117
    public function __construct()
118
    {
119
        if (!self::$alternative_thirdparty_folder) {
120
            self::$alternative_thirdparty_folder = Director::baseFolder().'/payment_dps/code/thirdparty';
121
        }
122
        require_once(self::$alternative_thirdparty_folder."/PxPay_Curl.inc.php");
123
        if (!Config::inst()->get("DpsPxPayComs", "pxpay_url")) {
124
            user_error("error in DpsPxPayComs::__construct, self::$pxpay_url not set. ", E_USER_WARNING);
0 ignored issues
show
Bug introduced by
The variable $pxpay_url does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
125
        }
126
        if (!Config::inst()->get("DpsPxPayComs", "pxpay_userid")) {
127
            user_error("error in DpsPxPayComs::__construct, self::$pxpay_userid not set. ", E_USER_WARNING);
0 ignored issues
show
Bug introduced by
The variable $pxpay_userid does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
128
        }
129
        if (!Config::inst()->get("DpsPxPayComs", "pxpay_encryption_key")) {
130
            user_error("error in DpsPxPayComs::__construct, self::$pxpay_encryption_key not set. ", E_USER_WARNING);
0 ignored issues
show
Bug introduced by
The variable $pxpay_encryption_key does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
131
        }
132
133
        $this->PxPayObject = new PxPay_Curl(
134
            Config::inst()->get("DpsPxPayComs", "pxpay_url"),
135
            Config::inst()->get("DpsPxPayComs", "pxpay_userid"),
136
            Config::inst()->get("DpsPxPayComs", "pxpay_encryption_key")
137
        );
138
    }
139
140
    /*
141
     * This function formats data into a request and returns redirection URL
142
     * NOTE: you will need to set all the variables prior to running this.
143
     * e.g. $myDPSPxPayComsObject->setMerchantReference("myreferenceHere");
144
     **/
145
    public function startPaymentProcess()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
146
    {
147
        if (!$this->TxnId) {
148
            $this->TxnId = uniqid("ID");
149
        }
150
        $request = new PxPayRequest();
151
152
        #Set PxPay properties
153
        if ($this->MerchantReference) {
154
            $request->setMerchantReference($this->MerchantReference);
155
        } else {
156
            user_error("error in DpsPxPayComs::startPaymentProcess, MerchantReference not set. ", E_USER_WARNING);
157
        }
158
        if ($this->AmountInput) {
159
            $request->setAmountInput($this->AmountInput);
160
        } else {
161
            user_error("error in DpsPxPayComs::startPaymentProcess, AmountInput not set. ", E_USER_WARNING);
162
        }
163
        if ($this->TxnData1) {
164
            $request->setTxnData1($this->TxnData1);
165
        }
166
        if ($this->TxnData2) {
167
            $request->setTxnData2($this->TxnData2);
168
        }
169
        if ($this->TxnData3) {
170
            $request->setTxnData3($this->TxnData3);
171
        }
172
        if ($this->TxnType) {
173
            $request->setTxnType($this->TxnType);
174
        } else {
175
            user_error("error in DpsPxPayComs::startPaymentProcess, TxnType not set. ", E_USER_WARNING);
176
        }
177
        if ($this->CurrencyInput) {
178
            $request->setCurrencyInput($this->CurrencyInput);
179
        } else {
180
            user_error("error in DpsPxPayComs::startPaymentProcess, CurrencyInput not set. ", E_USER_WARNING);
181
        }
182
        if ($this->EmailAddress) {
183
            $request->setEmailAddress($this->EmailAddress);
184
        }
185
        if ($this->UrlFail) {
186
            $request->setUrlFail($this->UrlFail);
187
        } else {
188
            user_error("error in DpsPxPayComs::startPaymentProcess, UrlFail not set. ", E_USER_WARNING);
189
        }
190
        if ($this->UrlSuccess) {
191
            $request->setUrlSuccess($this->UrlSuccess);
192
        } else {
193
            user_error("error in DpsPxPayComs::startPaymentProcess, UrlSuccess not set. ", E_USER_WARNING);
194
        }
195
        if ($this->TxnId) {
196
            $request->setTxnId($this->TxnId);
197
        }
198
        if ($this->EnableAddBillCard) {
199
            $request->setEnableAddBillCard($this->EnableAddBillCard);
200
        }
201
        if ($this->BillingId) {
202
            $request->setBillingId($this->BillingId);
203
        }
204
205
        /* TODO:
0 ignored issues
show
Unused Code Comprehensibility introduced by
70% 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
        $request->setEnableAddBillCard($EnableAddBillCard);
207
        $request->setBillingId($BillingId);
208
        $request->setOpt($Opt);
209
        */
210
211
        #Call makeRequest function to obtain input XML
212
        $request_string = $this->PxPayObject->makeRequest($request);
213
214
        #Obtain output XML
215
        $this->response = new MifMessage($request_string);
216
        #Parse output XML
217
        $url = $this->response->get_element_text("URI");
218
        //$valid = $this->response->get_attribute("valid");
0 ignored issues
show
Unused Code Comprehensibility introduced by
62% 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...
219
220
        #Redirect to payment page
221
        return $url;
222
    }
223
224
225
    /*
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...
226
     * This function receives information back from the payments page as a response object
227
     * --------------------- RESPONSE DATA ---------------------
228
     * $Success           = $responseObject->getSuccess();   # =1 when request succeeds
229
     * $AmountSettlement  = $responseObject->getAmountSettlement();
230
     * $AuthCode          = $responseObject->getAuthCode();  # from bank
231
     * $CardName          = $responseObject->getCardName();  # e.g. "Visa"
232
     * $CardNumber        = $responseObject->getCardNumber(); # Truncated card number
233
     * $DateExpiry        = $responseObject->getDateExpiry(); # in mmyy format
234
     * $DpsBillingId      = $responseObject->getDpsBillingId();
235
     * $BillingId         = $responseObject->getBillingId();
236
     * $CardHolderName    = $responseObject->getCardHolderName();
237
     * $DpsTxnRef	        = $responseObject->getDpsTxnRef();
238
     * $TxnType           = $responseObject->getTxnType();
239
     * $TxnData1          = $responseObject->getTxnData1();
240
     * $TxnData2          = $responseObject->getTxnData2();
241
     * $TxnData3          = $responseObject->getTxnData3();
242
     * $CurrencySettlement= $responseObject->getCurrencySettlement();
243
     * $ClientInfo        = $responseObject->getClientInfo(); # The IP address of the user who submitted the transaction
244
     * $TxnId             = $responseObject->getTxnId();
245
     * $CurrencyInput     = $responseObject->getCurrencyInput();
246
     * $EmailAddress      = $responseObject->getEmailAddress();
247
     * $MerchantReference = $responseObject->getMerchantReference();
248
     * $ResponseText      = $responseObject->getResponseText();
249
     * $TxnMac            = $responseObject->getTxnMac(); # An indication as to the uniqueness of a card used in relation to others
250
     *
251
     * also see: https://www.paymentexpress.com/technical_resources/ecommerce_hosted/error_codes.html
252
     **/
253
254
    public function processRequestAndReturnResultsAsObject()
0 ignored issues
show
Coding Style introduced by
processRequestAndReturnResultsAsObject 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...
255
    {
256
        #getResponse method in PxPay object returns PxPayResponse object
257
        #which encapsulates all the response data
258
        return $this->PxPayObject->getResponse($_REQUEST["result"]);
259
    }
260
261
    public function getDebugMessage()
262
    {
263
        $string = "<pre>";
264
        $string .= print_r($this->PxPayObject, true);
265
        $string .= print_r($this->response, true);
266
        $string .= "</pre>";
267
        return $string;
268
    }
269
270
    public function debug()
271
    {
272
        debug::show("debugging DpsPxPayComs");
273
        echo $this->getDebugMessage();
274
    }
275
}
276