Skip to content

Commit 89861d7

Browse files
committed
send http post
1 parent cf089b8 commit 89861d7

File tree

6 files changed

+90
-28
lines changed

6 files changed

+90
-28
lines changed

src/Hasher.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,23 @@ public function make(array $data)
1717
unset($data['sign']);
1818
}
1919

20+
if (array_key_exists('key', $data)) {
21+
unset($data['key']);
22+
}
23+
24+
$data = array_filter($data, static function ($value) {
25+
return ! empty($value);
26+
});
27+
2028
ksort($data);
21-
$result = [];
29+
$data['key'] = $this->secret;
30+
31+
$body = '';
2232
foreach ($data as $key => $value) {
23-
$result[] = "$key=$value";
33+
$body .= $key.'='.$value.'&';
2434
}
25-
$result[] = "key=$this->secret";
35+
$body = substr($body, 0, -1);
2636

27-
return strtoupper(md5(implode('&', $result)));
37+
return strtoupper(md5($body));
2838
}
2939
}

src/Message/PurchaseRequest.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace Omnipay\WanPay\Message;
44

5+
use JsonException;
56
use Omnipay\Common\Exception\InvalidRequestException;
7+
use Omnipay\Common\Exception\InvalidResponseException;
68
use Omnipay\Common\ItemBag;
79
use Omnipay\Common\ItemInterface;
810
use Omnipay\WanPay\Item;
@@ -157,8 +159,31 @@ public function getData()
157159
return $this->mergeSign($data);
158160
}
159161

162+
/**
163+
* @throws InvalidResponseException
164+
* @throws JsonException
165+
*/
160166
public function sendData($data)
161167
{
162-
return $this->response = new PurchaseResponse($this, $data);
168+
$body = '';
169+
foreach ($data as $key => $value) {
170+
$body .= $key.'='.$value.'&';
171+
}
172+
$body = substr($body, 0, -1);
173+
174+
$response = $this->httpClient->request(
175+
'POST',
176+
$this->getEndpoint().'wxzfservice/waporder',
177+
['Content-Type' => 'application/x-www-form-urlencoded'],
178+
$body
179+
);
180+
181+
$responseData = json_decode((string) $response->getBody(), true, 512, JSON_THROW_ON_ERROR);
182+
183+
if ($responseData['status'] !== '900') {
184+
throw new InvalidResponseException($responseData['info'], $responseData['status']);
185+
}
186+
187+
return $this->response = new PurchaseResponse($this, $responseData['data']);
163188
}
164189
}

src/Message/PurchaseResponse.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@ public function isRedirect()
1919

2020
public function getRedirectUrl()
2121
{
22-
return $this->request->getEndpoint().'wxzfservice/waporder';
22+
return $this->data['html'];
2323
}
2424

2525
public function getRedirectMethod()
2626
{
27-
return 'POST';
27+
return 'GET';
2828
}
2929

30-
public function getRedirectData()
30+
public function getTransactionId()
3131
{
32-
return $this->getData();
32+
return $this->data['out_trade_no'];
3333
}
3434
}

tests/GatewayTest.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,33 +24,43 @@ public function setUp(): void
2424

2525
public function testPurchase()
2626
{
27+
$this->setMockHttpResponse('PurchaseSuccess.txt');
28+
2729
$response = $this->gateway->purchase([
2830
'secondtimestamp' => 1674157848,
2931
'total_fee' => 100,
30-
'out_trade_no' => '1000201701201708041015eNuBrl6P',
32+
'out_trade_no' => '100020170117111010101',
3133
'returnurl' => 'https://foo.bar/returnurl',
34+
'description' => '商品',
3235
])->send();
3336

3437
self::assertFalse($response->isSuccessful());
3538
self::assertTrue($response->isRedirect());
36-
self::assertEquals('https://api.wan-pay.com/wxzfservice/waporder', $response->getRedirectUrl());
37-
self::assertEquals('POST', $response->getRedirectMethod());
39+
self::assertEquals('https://ixmfree.net/index.php?g=wxuser&m=payment&a=fast', $response->getRedirectUrl());
40+
self::assertEquals('GET', $response->getRedirectMethod());
41+
self::assertEquals('100020170117111010101', $response->getTransactionId());
42+
43+
$request = $this->getMockClient()->getLastRequest();
44+
$requestBody = [];
45+
parse_str((string) $request->getBody(), $requestBody);
46+
3847
self::assertEquals([
3948
'orgno' => '21008024',
40-
'secondtimestamp' => 1674157848,
41-
'nonce_str' => '8c90afc4',
42-
'sign' => 'AC2B51CAA7996D8CC95A11080AC6F011',
49+
'secondtimestamp' => '1674157848',
50+
'nonce_str' => '78cc54e7',
51+
'sign' => 'DE642DE89F22E1B293E7FB816A2F9252',
4352
'total_fee' => '10000',
44-
'out_trade_no' => '1000201701201708041015eNuBrl6P',
53+
'out_trade_no' => '100020170117111010101',
4554
'type' => 'AUTH_3DTRXTOKEN',
4655
'returnurl' => 'https://foo.bar/returnurl',
4756
'ipoolid' => '10703',
48-
], $response->getRedirectData());
57+
'body' => '商品',
58+
], $requestBody);
4959
}
5060

5161
public function testCompletePurchase()
5262
{
53-
$response = 'https://member.healthchain.com.tw/MHC01SSV2TEST/Checkout/CheckOutShowResultWangPay.aspx?authcode=154566&bankcard=552199******1898&nonce_str=46444248&orgno=21001719&out_trade_no=040911560243087HRC&result=核准&secondtimestamp=1586404583&status=0000&total_fee=100&orderdate=2020-04-09 11:56:02&trxtoken=&storename=旺旺電子商務-快點付&details=固定金額 免收件地址&payername=0409&payermobile=0409&payeremail=0409&sign=C720CD30C24DB5A372A33B90D2906C46';
63+
$response = 'https://member.healthchain.com.tw/MHC01SSV2TEST/Checkout/CheckOutShowResultWangPay.aspx?authcode=154566&bankcard=552199******1898&nonce_str=46444248&orgno=21001719&out_trade_no=040911560243087HRC&result=核准&secondtimestamp=1586404583&status=0000&total_fee=100&orderdate=2020-04-09 11:56:02&trxtoken=&storename=旺旺電子商務-快點付&details=固定金額 免收件地址&payername=0409&payermobile=0409&payeremail=0409&sign=7F015EB2B4674CD76C9C62090B6DF3E2';
5464
$parsed = parse_url($response);
5565
$options = [];
5666
parse_str($parsed['query'], $options);

tests/HasherTest.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,21 @@ class HasherTest extends TestCase
99
{
1010
public function testGenerate(): void
1111
{
12-
$hasher = new Hasher('n12bQ9Ew1_2342X4rEcO');
12+
$hasher = new Hasher('sbgSpVkimGvNgRnu');
1313
$data = [
14-
't0t1' => 'T1',
15-
'secondtimestamp' => '1489215551',
16-
'orgno' => '1265',
17-
'total_fee' => '8888',
18-
'nonce_str' => '71669965',
14+
'backurl' => 'https://test.com',
1915
'body' => 'good',
16+
'currency' => 'USD',
17+
'nonce_str' => '84778745',
18+
'orgno' => '21004280',
19+
'out_trade_no' => '20230327093049',
20+
'returnurl' => 'https://test.com',
21+
'secondtimestamp' => '1679880649',
22+
't0t1' => 'T1',
23+
'total_fee' => '1',
24+
'key' => 'sbgSpVkimGvNgRnu',
2025
];
2126

22-
self::assertEquals(
23-
'CF949AE048C0C3B23D5FADFE9495B319',
24-
$hasher->make($data)
25-
);
27+
self::assertEquals('2100EA95EFC3BEAF774EF536FAB8B132', $hasher->make($data));
2628
}
2729
}

tests/Mock/PurchaseSuccess.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
HTTP/1.1 200 OK
2+
Server: Apache
3+
Date: Sun, 11 May 2014 21:17:31 GMT
4+
Content-Type: application/json; charset=utf-8
5+
Status: 200 OK
6+
7+
8+
{
9+
"status": "900",
10+
"info": "下單成功",
11+
"data": {
12+
"html": "https://ixmfree.net/index.php?g=wxuser&m=payment&a=fast",
13+
"out_trade_no": "100020170117111010101"
14+
}
15+
}

0 commit comments

Comments
 (0)