DpsPxPayPayment::processPayment()   D
last analyzed

Complexity

Conditions 9
Paths 24

Size

Total Lines 36
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 19
nc 24
nop 2
dl 0
loc 36
rs 4.909
c 0
b 0
f 0
1
<?php
2
3
/**
4
 *@author nicolaas[at]sunnysideup.co.nz
5
 *@description: OrderNumber and PaymentID
6
 *
7
 *
8
 **/
9
10
class DpsPxPayPayment extends EcommercePayment
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...
11
{
12
    private static $db = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $db 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
        'TxnRef' => 'Text',
14
        'DebugMessage' => 'HTMLText'
15
    );
16
17
    protected $Currency = "";
18
    public function setCurrency($s)
19
    {
20
        $this->Currency = $s;
21
    }
22
23
    // DPS Information
24
25
    private static $privacy_link = 'http://www.paymentexpress.com/privacypolicy.htm';
0 ignored issues
show
Unused Code introduced by
The property $privacy_link 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...
26
27
    private static $logo = 'payment_dps/images/dps_paymentexpress_small.png';
0 ignored issues
show
Unused Code introduced by
The property $logo 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...
28
29
30
    // URLs
31
32
    // Please set from YAML. See _config/payment_dps.yml.example
33
    private static $credit_cards = array(
34
        /*'Visa' => 'ecommerce/images/paymentmethods/visa.jpg',
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% 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...
35
        'MasterCard' => 'ecommerce/images/paymentmethods/mastercard.jpg',
36
        'American Express' => 'ecommerce/images/paymentmethods/american-express.gif',
37
        'Dinners Club' => 'ecommerce/images/paymentmethods/dinners-club.jpg',
38
        'JCB' => 'ecommerce/images/paymentmethods/jcb.jpg'*/
39
    );
40
41
    public static function remove_credit_card($creditCard)
42
    {
43
        unset(self::$credit_cards[$creditCard]);
44
    }
45
46
    private static $email_debug = false;
0 ignored issues
show
Unused Code introduced by
The property $email_debug 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...
47
48
    public function getCMSFields()
49
    {
50
        $fields = parent::getCMSFields();
51
        $fields->replaceField("DebugMessage", new ReadonlyField("DebugMessage", "Debug info"));
52
        return $fields;
53
    }
54
55
    public function getPaymentFormFields()
56
    {
57
        $logo = '<img src="' . $this->config()->get("logo"). '" alt="Credit card payments powered by DPS"/>';
58
        $privacyLink = '<a href="' . $this->config()->get("privacy_link"). '" target="_blank" title="Read DPS\'s privacy policy">' . $logo . '</a><br/>';
59
        $paymentsList = '';
60
        if ($cards = $this->config()->get("credit_cards")) {
61
            foreach ($cards as $name => $image) {
62
                $paymentsList .= '<img src="' . $image . '" alt="' . $name . '"/>';
63
            }
64
        }
65
        $fields = new FieldList(
66
            new LiteralField('DPSInfo', $privacyLink),
67
            new LiteralField('DPSPaymentsList', $paymentsList)
68
        );
69
        return $fields;
70
    }
71
72
    public function getPaymentFormRequirements()
73
    {
74
        return array();
75
    }
76
77
    /**
78
     * @param array $data The form request data - see OrderForm
79
     * @param OrderForm $form The form object submitted on
80
     *
81
     * @return EcommercePayment_Result
82
     */
83
    public function processPayment($data, $form)
84
    {
85
        $order = $this->Order();
0 ignored issues
show
Documentation Bug introduced by
The method Order does not exist on object<DpsPxPayPayment>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
86
        //if currency has been pre-set use this
87
        $currency = $this->Amount->Currency;
0 ignored issues
show
Documentation introduced by
The property Amount does not exist on object<DpsPxPayPayment>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
88
        //if amout has been pre-set, use this
89
        $amount = $this->Amount->Amount;
0 ignored issues
show
Documentation introduced by
The property Amount does not exist on object<DpsPxPayPayment>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
90
        if ($order && $order->exists()) {
91
            //amount may need to be adjusted to total outstanding
92
            //or amount may not have been set yet
93
            $amount = $order->TotalOutstanding();
94
            //get currency from Order
95
            //this is better than the pre-set currency one
96
            //which may have been set to the default
97
            $currencyObject = $order->CurrencyUsed();
98
            if ($currencyObject) {
99
                $currency = $currencyObject->Code;
100
            }
101
        }
102
        if (!$amount && !empty($data["Amount"])) {
103
            $amount = floatval($data["Amount"]);
104
        }
105
        if (!$currency && !empty($data["Currency"])) {
106
            $currency = floatval($data["Currency"]);
107
        }
108
        //final backup for currency
109
        if (!$currency) {
110
            $currency = EcommercePayment::site_currency();
111
        }
112
        $this->Amount->Currency = $currency;
0 ignored issues
show
Documentation introduced by
The property Amount does not exist on object<DpsPxPayPayment>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
113
        $this->Amount->Amount = $amount;
0 ignored issues
show
Documentation introduced by
The property Amount does not exist on object<DpsPxPayPayment>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
114
        //no need to write here, as it will be done by BuildURL
115
        //$this->write();
0 ignored issues
show
Unused Code Comprehensibility introduced by
84% 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...
116
        $url = $this->buildURL($amount, $currency);
117
        return $this->executeURL($url);
118
    }
119
120
    /**
121
     *
122
     * @param Float $amount
123
     * @param String $currency - e.g. NZD
124
     * @return String
125
     *
126
     */
127
    protected function buildURL($amount, $currency)
128
    {
129
        $commsObject = new DpsPxPayComs();
130
131
        /**
132
        * order details
133
        **/
134
        $commsObject->setTxnType(DpsPxPayComs::get_txn_type());
135
        $commsObject->setMerchantReference($this->ID);
136
        //replace any character that is NOT [0-9] or dot (.)
137
138
        $commsObject->setAmountInput(floatval(preg_replace("/[^0-9\.]/", "", $amount)));
139
        $commsObject->setCurrencyInput($currency);
140
141
        /**
142
        * details of the redirection
143
        **/
144
        $commsObject->setUrlFail(DpsPxPayPayment_Handler::absolute_complete_link());
145
        $commsObject->setUrlSuccess(DpsPxPayPayment_Handler::absolute_complete_link());
146
147
        /**
148
        * process payment data (check if it is OK and go forward if it is...
149
        **/
150
        $url = $commsObject->startPaymentProcess();
151
        $debugMessage = $commsObject->getDebugMessage();
152
        $this->DebugMessage = $debugMessage;
0 ignored issues
show
Documentation introduced by
The property DebugMessage does not exist on object<DpsPxPayPayment>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
153
        $this->write();
154
        if ($this->config()->get("email_debug")) {
155
            $from = Email::config()->admin_email;
156
            $to = Email::config()->admin_email;
157
            $subject = "DPS Debug Information";
158
            $body = $debugMessage;
159
            $email = new Email($from, $to, $subject, $body);
160
            $email->send();
161
        }
162
        return $url;
163
    }
164
165
    public function executeURL($url)
166
    {
167
        $url = str_replace("&", "&amp;", $url);
168
        $url = str_replace("&amp;&amp;", "&amp;", $url);
169
        //$url = str_replace("==", "", $url);
0 ignored issues
show
Unused Code Comprehensibility introduced by
60% 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...
170
        if ($url) {
171
            /**
172
            * build redirection page
173
            **/
174
            $page = new SiteTree();
175
            $page->Title = 'Redirection to DPS...';
176
            $page->Logo = '<img src="' . $this->config()->get("logo") . '" alt="Payments powered by DPS"/>';
177
            $page->Form = $this->DPSForm($url);
178
            $controller = new ContentController($page);
179
            Requirements::clear();
180
            Requirements::javascript(THIRDPARTY_DIR."/jquery/jquery.js");
181
            //Requirements::block(THIRDPARTY_DIR."/jquery/jquery.js");
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% 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...
182
            //Requirements::javascript(Director::protocol()."ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js");
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...
183
            return EcommercePayment_Processing::create($controller->renderWith('PaymentProcessingPage'));
184
        } else {
185
            $page = new SiteTree();
186
            $page->Title = 'Sorry, DPS can not be contacted at the moment ...';
187
            $page->Logo = 'Sorry, an error has occured in contacting the Payment Processing Provider, please try again in a few minutes...';
188
            $page->Form = $this->DPSForm($url);
189
            $controller = new ContentController($page);
190
            Requirements::clear();
191
            Requirements::javascript(THIRDPARTY_DIR."/jquery/jquery.js");
192
            //Requirements::block(THIRDPARTY_DIR."/jquery/jquery.js");
0 ignored issues
show
Unused Code Comprehensibility introduced by
56% 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...
193
            //Requirements::javascript(Director::protocol()."ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js");
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...
194
            return EcommercePayment_Failure::create($controller->renderWith('PaymentProcessingPage'));
195
        }
196
    }
197
198
    public function DPSForm($url)
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...
199
    {
200
        $urlWithoutAmpersand = Convert::raw2js(str_replace('&amp;', '&', $url));
0 ignored issues
show
Unused Code introduced by
$urlWithoutAmpersand 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...
201
        return <<<HTML
202
            <form id="PaymentFormDPS" method="post" action="$url">
203
                <input type="submit" value="pay now" />
204
            </form>
205
            <script type="text/javascript">
206
                jQuery(document).ready(function() {
207
                    if(!jQuery.browser.msie) {
208
                        jQuery("#PaymentFormDPS").submit();
209
                    }
210
                });
211
            </script>
212
HTML;
213
    }
214
}
215