Payment_Moneybookers_Driver::set_fields()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 9
Ratio 100 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 4
eloc 5
c 2
b 1
f 0
nc 3
nop 1
dl 9
loc 9
rs 9.2
1
<?php defined('SYSPATH') or die('No direct script access.');
2
/**
3
 * MoneyBookers Payment Driver
4
 *
5
 * $Id: MoneyBookers.php
6
 *
7
 * @package    Payment
8
 * @author     Josh Domagala
9
 * @copyright  (c) 2008 Kohana Team
10
 * @license    http://kohanaphp.com/license.html
11
 */
12
class Payment_Moneybookers_Driver implements Payment_Driver
13
{
14
    // Fields required to do a transaction
15
    private $required_fields = array(
16
        'payment_url'    => false,
17
        'script'    => false,
18
        'currency'    => false,
19
        'pay_to_email'            => false,
20
        'test_pay_to_email'            => false,
21
        'password'        => false,
22
        'test_password'        => false,
23
        'postdata'              => false,
24
        'action'              => false
25
    );
26
27
    private $fields = array(
28
        'payment_url'    => '',
29
        'script'    => '',
30
        'currency'    => '',
31
        'pay_to_email'    => '',
32
        'test_pay_to_email'    => '',
33
        'password'        => '',
34
        'test_password'        => '',
35
        'postdata'        => '',
36
        'action'        => '',
37
        'trn_id'        => '',
38
        'mb_trn_id'        => '',
39
        'rec_payment_id'        => '',
40
        'sid'        => ''
41
    );
42
43
    private $test_mode = true;
44
45
    /**
46
     * Sets the config for the class.
47
     *
48
     * @param  array  config passed from the library
49
     */
50
    public function __construct($config)
51
    {
52
        $this->test_mode = $config['test_mode'];
53
        
54
        $this->fields['payment_url'] = $config['payment_url'];
55
        $this->fields['currency'] = $config['currency'];
56
        
57
        if ($this->test_mode) {
58
            $this->fields['test_pay_to_email'] = $config['test_pay_to_email'];
59
            $this->fields['test_password'] = $config['test_password'];
60
        } else {
61
            $this->fields['pay_to_email'] = $config['pay_to_email'];
62
            $this->fields['password'] = $config['password'];
63
        }
64
        
65
        $this->curl_config = $config['curl_config'];
0 ignored issues
show
Bug introduced by
The property curl_config does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
66
67
        Kohana::Log('debug', 'MoneyBookers Payment Driver Initialized');
68
    }
69
70 View Code Duplication
    public function set_fields($fields)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
    {
72
        foreach ((array) $fields as $key => $value) {
73
            $this->fields[$key] = $value;
74
            if (array_key_exists($key, $this->required_fields) and !empty($value)) {
75
                $this->required_fields[$key] = true;
76
            }
77
        }
78
    }
79
80
    public function process()
81
    {
82
        $post_url = ($this->test_mode)
83
                  ? $this->fields['payment_url'].$this->fields['script'] // Test mode URL
84
                  : $this->fields['payment_url'].$this->fields['script']; // Live URL
85
86
        $pay_to_email = ($this->test_mode)
87
                  ? $this->fields['test_pay_to_email'] // Test mode email
88
                  : $this->fields['pay_to_email']; // Live email
89
90
        $password = ($this->test_mode)
91
                  ? $this->fields['test_password'] // Test mode password
92
                  : $this->fields['password']; // Live password
93
94
        $this->fields['postdata']['action'] = $this->fields['action'];
95
        
96
        $config = Kohana::config('payment.MoneyBookers');
97
98
        if ($this->fields['script'] == $config['refund_script'] && $this->fields['action'] == "prepare") {
99
            $this->fields['postdata']['email'] = $pay_to_email;
100
            $this->fields['postdata']['password'] = md5($password);
101
            $this->fields['postdata']['trn_id'] = $this->fields['trn_id'];
102
        } elseif ($this->fields['action'] == "prepare") {
103
            $this->fields['postdata']['email'] = $pay_to_email;
104
            $this->fields['postdata']['password'] = md5($password);
105
            $this->fields['postdata']['amount'] = $this->fields['amount'];
106
            $this->fields['postdata']['currency'] = $this->fields['currency'];
107
            $this->fields['postdata']['frn_trn_id'] = $this->fields['trn_id'];
108
            $this->fields['postdata']['rec_payment_id'] = $this->fields['rec_payment_id'];
109
        } elseif ($this->fields['action'] == "refund") {
110
            $this->fields['postdata']['sid'] = $this->fields['sid'];
111
        } elseif ($this->fields['action'] == "request") {
112
            $this->fields['postdata']['sid'] = $this->fields['sid'];
113
        } elseif ($this->fields['action'] == "status_od") {
114
            $this->fields['postdata']['email'] = $pay_to_email;
115
            $this->fields['postdata']['password'] = md5($password);
116
            $this->fields['postdata']['trn_id'] = $this->fields['trn_id'];
117
        } elseif ($this->fields['action'] == "status_trn") {
118
            $this->fields['postdata']['email'] = $pay_to_email;
119
            $this->fields['postdata']['password'] = md5($password);
120
            $this->fields['postdata']['mb_trn_id'] = $this->fields['trn_id'];
121
        }
122
123
        $ch = curl_init($post_url);
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $ch. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
124
125
        curl_setopt($ch, CURLOPT_FAILONERROR, true);
126
        // the following disallows redirects
127
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
128
        // next we return into a variable
129
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
130
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
131
        curl_setopt($ch, CURLOPT_POST, 1);
132
133
        // Set the curl POST fields
134
        curl_setopt($ch, CURLOPT_POSTFIELDS, $this->fields['postdata']);
135
136
        // Execute post and get results
137
        $response = curl_exec($ch);
138
        // Get error messages.
139
        $info = curl_getinfo($ch);
0 ignored issues
show
Unused Code introduced by
$info 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...
140
141
        curl_close($ch);
142
143
        if (!$response) {
144
            return false;
145
        }
146
            
147
        if ($this->fields['action'] != "status_od" && $this->fields['action'] != "status_trn") {
148
            $xml = simplexml_load_string($response);
149
        }
150
            
151
        if ($this->fields['action'] == "prepare") {
152
            return ($xml->{'sid'} != "") ? $xml : false;
0 ignored issues
show
Bug introduced by
The variable $xml does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
Bug Compatibility introduced by
The expression $xml->{'sid'} != '' ? $xml : false; of type SimpleXMLElement|false adds the type SimpleXMLElement to the return on line 152 which is incompatible with the return type declared by the interface Payment_Driver::process of type boolean.
Loading history...
153
        }
154
            
155
        if ($this->fields['action'] == "request") {
156
            return ($xml->{'transaction'}->{'status_msg'} == "processed") ? $xml : false;
0 ignored issues
show
Bug Compatibility introduced by
The expression $xml->{'transaction'}->{...cessed' ? $xml : false; of type SimpleXMLElement|false adds the type SimpleXMLElement to the return on line 156 which is incompatible with the return type declared by the interface Payment_Driver::process of type boolean.
Loading history...
157
        }
158
            
159
        if ($this->fields['action'] == "status_od") {
160
            return (strstr($response, "Status: 0")) ? true : false;
161
        }
162
        
163
        if ($this->fields['action'] == "status_trn") {
164
            return (strstr($response, "OK")) ? $response : false;
165
        }
166
        
167
        if ($this->fields['action'] == "refund") {
168
            return ($xml->{'status'} == 2) ? $xml : false;
0 ignored issues
show
Bug Compatibility introduced by
The expression $xml->{'status'} == 2 ? $xml : false; of type SimpleXMLElement|false adds the type SimpleXMLElement to the return on line 168 which is incompatible with the return type declared by the interface Payment_Driver::process of type boolean.
Loading history...
169
        }
170
    }
171
} // End Payment_MoneyBookers_Driver Class
172