Completed
Pull Request — master (#200)
by Carlos
04:52 queued 02:20
created

Refund::__construct()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 9
rs 9.6667
cc 3
eloc 5
nc 4
nop 1
1
<?php
2
/**
3
 * Refund.php
4
 *
5
 * Part of Overtrue\Wechat.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @author    jaring <[email protected]>
11
 *
12
 *
13
 *Usage:
14
 *   $business = new Business($appId, $appSecret, $mchId, $mchKey);
15
 *   $business->setClientCert(dirname(__FILE__).'/cert/apiclient_cert.pem');
16
 *   $business->setClientKey(dirname(__FILE__).'/cert/apiclient_key.pem');
17
 *   $refund =new Refund($business);
18
 *   $refund->out_refund_no= md5(uniqid(microtime()));//退单单号
19
 *   $refund->total_fee=1; //订单金额
20
 *   $refund->refund_fee=1;//退款金额
21
 *   $refund->out_trade_no=$order_id;//原商户订单号
22
 *   var_dump($trans->getResponse());
23
*/
24
namespace Overtrue\Wechat\Payment;
25
26
use Overtrue\Wechat\Utils\XML;
27
use Overtrue\Wechat\Utils\SignGenerator;
28
use Overtrue\Wechat\Http;
29
30
class Refund
31
{
32
    /**
33
     * 退款接口链接:https://api.mch.weixin.qq.com/secapi/pay/refund
34
     */
35
    const REFUNDORDER_URL = 'https://api.mch.weixin.qq.com/secapi/pay/refund';
36
37
    /**
38
     * 商户信息
39
     * 
40
     * @var Business
41
     */
42
    protected $business;
43
44
    /**
45
     * 退款订单必填项
46
     *
47
     * @var array
48
     */
49
    protected static $required = array('out_refund_no', 'total_fee', 'refund_fee');
50
51
    /**
52
     * 退款订单选填项
53
     *
54
     * @var array
55
     */
56
    protected static $optional = array('out_trade_no', 'transaction_id', 'device_info', 'fee_type', 'op_user_id');
57
58
    /**
59
     * @var array
60
     */
61
    protected static $params      = array();
62
    protected static $allowParams = array();
63
64
    /**
65
     * 退款返回信息
66
     *
67
     * @var array
68
     */
69
    protected $refundInfo = null;
70
71
    public function __construct(Business $business = null)
72
    {
73
        if (!is_null($business)) {
74
            $this->setBusiness($business);
75
        }
76
        if (count(static::$allowParams) === 0) {
77
            static::$allowParams = array_merge(static::$required, static::$optional);
78
        }
79
    }
80
81
    /**
82
     * 设置商户
83
     * 
84
     * @param Business $business
85
     *
86
     * @return $this
87
     *
88
     * @throws Exception
89
     */
90 View Code Duplication
    public function setBusiness(Business $business)
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...
91
    {
92
        if (!is_null($business)) {
93
            try {
94
                $business->checkParams();
95
            } catch (Exception $e) {
96
                throw new Exception($e->getMessage());
97
            }
98
            $this->business   = $business;
99
            $this->refundInfo = null;
0 ignored issues
show
Documentation Bug introduced by
It seems like null of type null is incompatible with the declared type array of property $refundInfo.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
100
        }
101
102
        return $this;
103
    }
104
105
    /**
106
     * 获取商户
107
     * 
108
     * @return Business
109
     */
110
    public function getBusiness()
111
    {
112
        return $this->business;
113
    }
114
115
    /**
116
     * 获取退款结果
117
     *
118
     * @return array
119
     *
120
     * @throws Exception
121
     */
122
    public function getResponse()
123
    {
124
        if (is_null($this->business)) {
125
            throw new Exception('Business is required');
126
        }
127
128
        static::$params['appid']    = $this->business->appid;
0 ignored issues
show
Documentation introduced by
The property appid does not exist on object<Overtrue\Wechat\Payment\Business>. 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...
129
        static::$params['mch_id']   = $this->business->mch_id;
0 ignored issues
show
Documentation introduced by
The property mch_id does not exist on object<Overtrue\Wechat\Payment\Business>. 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...
130
        $this->checkParams();
131
        $signGenerator = new SignGenerator(static::$params);
132
        $signGenerator->onSortAfter(function (SignGenerator $that) {
133
            $that->key = $this->business->mch_key;
0 ignored issues
show
Documentation introduced by
The property key does not exist on object<Overtrue\Wechat\Utils\SignGenerator>. 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...
Documentation introduced by
The property mch_key does not exist on object<Overtrue\Wechat\Payment\Business>. 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...
134
        });
135
        static::$params['sign'] = $signGenerator->getResult();
136
137
        $request = XML::build(static::$params);
138
        //设置Http使用的证书
139
        $options['sslcert_path'] = $this->business->getClientCert();
0 ignored issues
show
Coding Style Comprehensibility introduced by
$options was never initialized. Although not strictly required by PHP, it is generally a good practice to add $options = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
140
        $options['sslkey_path']  = $this->business->getClientKey();
141
142
        $http     = new Http();
143
        $response = $http->request(static::REFUNDORDER_URL, Http::POST, $request, $options);
0 ignored issues
show
Documentation introduced by
$request is of type string, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
144
        if (empty($response)) {
145
            throw new Exception('Get Refund Failure:');
146
        }
147
        $refundOrder = XML::parse($response);
0 ignored issues
show
Documentation introduced by
$response is of type array, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
148
149 View Code Duplication
        if (isset($refundOrder['return_code']) &&
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
150
            $refundOrder['return_code'] === 'FAIL') {
151
            throw new Exception($refundOrder['return_code'].': '.$refundOrder['return_msg']);
152
        }
153
154
        //返回签名数据校验
155
        if (empty($refundOrder) || empty($refundOrder['sign'])) {
156
            throw new Exception('param sign is missing or empty');
157
        }
158
        $sign = $refundOrder['sign'];
159
        unset($refundOrder['sign']);
160
        $signGenerator = new SignGenerator($refundOrder);
0 ignored issues
show
Bug introduced by
It seems like $refundOrder defined by \Overtrue\Wechat\Utils\XML::parse($response) on line 147 can also be of type string; however, Overtrue\Wechat\Utils\SignGenerator::__construct() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
161
        $signGenerator->onSortAfter(function (SignGenerator $that) {
162
            $that->key = $this->business->mch_key;
0 ignored issues
show
Documentation introduced by
The property key does not exist on object<Overtrue\Wechat\Utils\SignGenerator>. 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...
Documentation introduced by
The property mch_key does not exist on object<Overtrue\Wechat\Payment\Business>. 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...
163
        });
164
        if ($sign !== $signGenerator->getResult()) {
165
            throw new Exception('check sign error');
166
        }
167
168
        //返回结果判断
169 View Code Duplication
        if (isset($refundOrder['result_code']) &&
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
170
            ($refundOrder['result_code'] === 'FAIL')) {
171
            throw new Exception($refundOrder['err_code'].': '.$refundOrder['err_code_des']);
172
        }
173
174 View Code Duplication
        if (isset($refundOrder['return_code']) &&
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
175
            $refundOrder['return_code'] === 'FAIL') {
176
            throw new Exception($refundOrder['return_code'].': '.$refundOrder['return_msg']);
177
        }
178
179
        return $this->refundInfo = $refundOrder;
0 ignored issues
show
Documentation Bug introduced by
It seems like $refundOrder can also be of type string. However, the property $refundInfo is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
180
    }
181
182
    /**
183
     * 检测参数值是否有效
184
     *
185
     * @throws Exception
186
     */
187
    public function checkParams()
188
    {
189
        foreach (static::$required as $paramName) {
190
            if (!array_key_exists($paramName, static::$params)) {
191
                throw new Exception(sprintf('"%s" is required', $paramName));
192
            }
193
        }
194
195
        if (!array_key_exists('transaction_id', static::$params) && !array_key_exists('out_trade_no', static::$params)) {
196
            throw new Exception('transaction_id or out_trade_no is required');
197
        }
198
199
        if (!array_key_exists('nonce_str', static::$params)) {
200
            static::$params['nonce_str'] = md5(uniqid(microtime()));
201
        }
202
203
        if (!array_key_exists('op_user_id', static::$params)) {
204
            static::$params['op_user_id'] = $this->business->mch_id;
0 ignored issues
show
Documentation introduced by
The property mch_id does not exist on object<Overtrue\Wechat\Payment\Business>. 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...
205
        }
206
    }
207
208 View Code Duplication
    public function __set($property, $value)
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...
209
    {
210
        if (!in_array($property, static::$allowParams, true)) {
211
            throw new Exception(sprintf('"%s" is not required', $property));
212
        }
213
214
        return static::$params[$property] = $value;
215
    }
216
}
217