Skip to content

Commit 5082f17

Browse files
committed
Use guzzlehttp client
1 parent 39abe0a commit 5082f17

15 files changed

+161
-170
lines changed

src/BaseAbstractGateway.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,6 @@
77
abstract class BaseAbstractGateway extends AbstractGateway
88
{
99

10-
public function getTradeType()
11-
{
12-
return $this->getParameter('trade_type');
13-
}
14-
15-
1610
public function setTradeType($tradeType)
1711
{
1812
$this->setParameter('trade_type', $tradeType);
@@ -116,6 +110,12 @@ public function purchase($parameters = array ())
116110
}
117111

118112

113+
public function getTradeType()
114+
{
115+
return $this->getParameter('trade_type');
116+
}
117+
118+
119119
/**
120120
* @param array $parameters
121121
*

src/Helper.php

+1-20
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,6 @@
55
class Helper
66
{
77

8-
public static function post($url, $data = array(), $timeout = 30, $options = array())
9-
{
10-
$ch = curl_init($url);
11-
12-
curl_setopt($ch, CURLOPT_POST, true);
13-
curl_setopt($ch, CURLOPT_POSTFIELDS, self::array2xml($data));
14-
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
15-
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
16-
curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
17-
curl_setopt_array($ch, $options);
18-
19-
$result = curl_exec($ch);
20-
21-
$result = self::xml2array($result);
22-
23-
return $result;
24-
}
25-
26-
278
public static function array2xml($arr, $root = 'xml')
289
{
2910
$xml = "<$root>";
@@ -48,7 +29,7 @@ public static function xml2array($xml)
4829

4930
public static function sign($data, $key)
5031
{
51-
unset( $data['sign'] );
32+
unset($data['sign']);
5233

5334
ksort($data);
5435

src/Message/CloseOrderRequest.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,9 @@ public function setOutTradeNo($outTradeNo)
7171
*/
7272
public function sendData($data)
7373
{
74-
$responseData = Helper::post($this->endpoint, $data);
74+
$request = $this->httpClient->post($this->endpoint)->setBody(Helper::array2xml($data));
75+
$response = $request->send()->getBody();
76+
$responseData = Helper::xml2array($response);
7577

7678
return $this->response = new CloseOrderResponse($this, $responseData);
7779
}

src/Message/CompletePurchaseRequest.php

+24-24
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,12 @@
1515
class CompletePurchaseRequest extends BaseAbstractRequest
1616
{
1717

18-
/**
19-
* Get the raw data array for this message. The format of this varies from gateway to
20-
* gateway, but will usually be either an associative array, or a SimpleXMLElement.
21-
*
22-
* @return mixed
23-
*/
24-
public function getData()
25-
{
26-
$data = $this->getRequestParams();
27-
28-
if (is_string($data)) {
29-
$data = Helper::xml2array($data);
30-
}
31-
32-
return $data;
33-
}
34-
35-
3618
public function setRequestParams($requestParams)
3719
{
3820
$this->setParameter('request_params', $requestParams);
3921
}
4022

4123

42-
public function getRequestParams()
43-
{
44-
return $this->getParameter('request_params');
45-
}
46-
47-
4824
/**
4925
* Send the request with specified data
5026
*
@@ -73,4 +49,28 @@ public function sendData($data)
7349

7450
return $this->response = new CompletePurchaseResponse($this, $responseData);
7551
}
52+
53+
54+
/**
55+
* Get the raw data array for this message. The format of this varies from gateway to
56+
* gateway, but will usually be either an associative array, or a SimpleXMLElement.
57+
*
58+
* @return mixed
59+
*/
60+
public function getData()
61+
{
62+
$data = $this->getRequestParams();
63+
64+
if (is_string($data)) {
65+
$data = Helper::xml2array($data);
66+
}
67+
68+
return $data;
69+
}
70+
71+
72+
public function getRequestParams()
73+
{
74+
return $this->getParameter('request_params');
75+
}
7676
}

src/Message/CompletePurchaseResponse.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,22 @@ public function isSuccessful()
2424
}
2525

2626

27-
public function isSignMatch()
27+
public function isPaid()
2828
{
2929
$data = $this->getData();
3030

31-
return $data['sign_match'];
31+
return $data['paid'];
3232
}
3333

3434

35-
public function isPaid()
35+
public function isSignMatch()
3636
{
3737
$data = $this->getData();
3838

39-
return $data['paid'];
39+
return $data['sign_match'];
4040
}
4141

42+
4243
public function getRequestData()
4344
{
4445
return $this->request->getData();

src/Message/CreateMicroOrderRequest.php

+4-10
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,7 @@ class CreateMicroOrderRequest extends CreateOrderRequest
2525
*/
2626
public function getData()
2727
{
28-
$this->validate(
29-
'app_id',
30-
'mch_id',
31-
'body',
32-
'out_trade_no',
33-
'total_fee',
34-
'auth_code'
35-
);
28+
$this->validate('app_id', 'mch_id', 'body', 'out_trade_no', 'total_fee', 'auth_code');
3629

3730
$data = array (
3831
'appid' => $this->getAppId(),//*
@@ -59,7 +52,6 @@ public function getData()
5952
}
6053

6154

62-
6355
/**
6456
* @return mixed
6557
*/
@@ -84,7 +76,9 @@ public function setAuthCode($authCode)
8476
*/
8577
public function sendData($data)
8678
{
87-
$responseData = Helper::post($this->endpoint, $data);
79+
$request = $this->httpClient->post($this->endpoint)->setBody(Helper::array2xml($data));
80+
$response = $request->send()->getBody();
81+
$responseData = Helper::xml2array($response);
8882

8983
return $this->response = new CreateOrderResponse($this, $responseData);
9084
}

src/Message/CreateMicroOrderResponse.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,20 @@ public function getOrderData()
3939
}
4040

4141

42-
public function getCodeUrl()
42+
public function getPrepayId()
4343
{
44-
if ($this->isSuccessful() && $this->request->getTradeType() == 'NATIVE') {
45-
return $this->getData()['code_url'];
44+
if ($this->isSuccessful()) {
45+
return $this->getData()['prepay_id'];
4646
} else {
4747
return null;
4848
}
4949
}
5050

5151

52-
public function getPrepayId()
52+
public function getCodeUrl()
5353
{
54-
if ($this->isSuccessful()) {
55-
return $this->getData()['prepay_id'];
54+
if ($this->isSuccessful() && $this->request->getTradeType() == 'NATIVE') {
55+
return $this->getData()['code_url'];
5656
} else {
5757
return null;
5858
}

0 commit comments

Comments
 (0)