Skip to content

Commit c1a9cbb

Browse files
authored
Merge pull request #162 from maxsky/master
Add complete refund notify and adjust composer.json file.
2 parents 39a0173 + fa91c19 commit c1a9cbb

File tree

8 files changed

+339
-28
lines changed

8 files changed

+339
-28
lines changed

composer.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,15 @@
2929
]
3030
},
3131
"require": {
32+
"ext-json": "*",
33+
"ext-openssl": "*",
3234
"omnipay/common": "^3.0",
33-
"php-http/guzzle6-adapter": "*"
35+
"php-http/guzzle6-adapter": "^2.0"
3436
},
3537
"require-dev": {
3638
"omnipay/tests": "^3.0",
37-
"squizlabs/php_codesniffer": "3.*"
38-
}
39+
"squizlabs/php_codesniffer": "^3.4"
40+
},
41+
"minimum-stability": "dev",
42+
"prefer-stable": true
3943
}

src/AbstractAopGateway.php

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Omnipay\Alipay;
44

55
use Omnipay\Alipay\Requests\AopCompletePurchaseRequest;
6+
use Omnipay\Alipay\Requests\AopCompleteRefundRequest;
67
use Omnipay\Alipay\Requests\AopTradeCancelRequest;
78
use Omnipay\Alipay\Requests\AopTradeCloseRequest;
89
use Omnipay\Alipay\Requests\AopTradeOrderSettleRequest;
@@ -14,6 +15,7 @@
1415
use Omnipay\Alipay\Requests\DataServiceBillDownloadUrlQueryRequest;
1516
use Omnipay\Common\AbstractGateway;
1617
use Omnipay\Common\Exception\InvalidRequestException;
18+
use Omnipay\Common\Message\AbstractRequest;
1719

1820
abstract class AbstractAopGateway extends AbstractGateway
1921
{
@@ -305,6 +307,10 @@ public function setAlipaySdk($value)
305307
}
306308

307309

310+
/**
311+
* @return AbstractAopGateway
312+
* @throws InvalidRequestException
313+
*/
308314
public function production()
309315
{
310316
return $this->setEnvironment('production');
@@ -342,16 +348,22 @@ public function setEndpoint($value)
342348
}
343349

344350

351+
/**
352+
* @return AbstractAopGateway
353+
* @throws InvalidRequestException
354+
*/
345355
public function sandbox()
346356
{
347357
return $this->setEnvironment('sandbox');
348358
}
349359

350360

351361
/**
362+
* @noinspection PhpDocRedundantThrowsInspection
363+
*
352364
* @param array $parameters
353365
*
354-
* @return \Omnipay\Alipay\Requests\AopCompletePurchaseRequest
366+
* @return AopCompletePurchaseRequest|AbstractRequest
355367
* @throws InvalidRequestException
356368
*/
357369
public function completePurchase(array $parameters = [])
@@ -360,12 +372,26 @@ public function completePurchase(array $parameters = [])
360372
}
361373

362374

375+
/**
376+
* @noinspection PhpDocRedundantThrowsInspection
377+
*
378+
* @param array $parameters
379+
*
380+
* @return AopCompleteRefundRequest|AbstractRequest
381+
* @throws InvalidRequestException
382+
*/
383+
public function completeRefund(array $parameters = [])
384+
{
385+
return $this->createRequest(AopCompleteRefundRequest::class, $parameters);
386+
}
387+
388+
363389
/**
364390
* Query Order Status
365391
*
366392
* @param array $parameters
367393
*
368-
* @return \Omnipay\Alipay\Requests\AopTradeQueryRequest
394+
* @return AopTradeQueryRequest|AbstractRequest
369395
*/
370396
public function query(array $parameters = [])
371397
{
@@ -378,7 +404,7 @@ public function query(array $parameters = [])
378404
*
379405
* @param array $parameters
380406
*
381-
* @return \Omnipay\Alipay\Requests\AopTradeRefundRequest
407+
* @return AopTradeRefundRequest|AbstractRequest
382408
*/
383409
public function refund(array $parameters = [])
384410
{
@@ -391,7 +417,7 @@ public function refund(array $parameters = [])
391417
*
392418
* @param array $parameters
393419
*
394-
* @return \Omnipay\Alipay\Requests\AopTradeRefundQueryRequest
420+
* @return AopTradeRefundQueryRequest|AbstractRequest
395421
*/
396422
public function refundQuery(array $parameters = [])
397423
{
@@ -404,7 +430,7 @@ public function refundQuery(array $parameters = [])
404430
*
405431
* @param array $parameters
406432
*
407-
* @return \Omnipay\Alipay\Requests\AopTradeCloseRequest
433+
* @return AopTradeCloseRequest|AbstractRequest
408434
*/
409435
public function close(array $parameters = [])
410436
{
@@ -417,7 +443,7 @@ public function close(array $parameters = [])
417443
*
418444
* @param array $parameters
419445
*
420-
* @return \Omnipay\Alipay\Requests\AopTradeCancelRequest
446+
* @return AopTradeCancelRequest|AbstractRequest
421447
*/
422448
public function cancel(array $parameters = [])
423449
{
@@ -430,7 +456,7 @@ public function cancel(array $parameters = [])
430456
*
431457
* @param array $parameters
432458
*
433-
* @return \Omnipay\Alipay\Requests\AopTransferToAccountRequest
459+
* @return AopTransferToAccountRequest|AbstractRequest
434460
*/
435461
public function transfer(array $parameters = [])
436462
{
@@ -443,7 +469,7 @@ public function transfer(array $parameters = [])
443469
*
444470
* @param array $parameters
445471
*
446-
* @return \Omnipay\Alipay\Requests\AopTransferToAccountQueryRequest
472+
* @return AopTransferToAccountQueryRequest|AbstractRequest
447473
*/
448474
public function transferQuery(array $parameters = [])
449475
{
@@ -456,7 +482,7 @@ public function transferQuery(array $parameters = [])
456482
*
457483
* @param array $parameters
458484
*
459-
* @return \Omnipay\Alipay\Requests\AopTradeCancelRequest
485+
* @return AopTradeCancelRequest|AbstractRequest
460486
*/
461487
public function settle(array $parameters = [])
462488
{
@@ -467,7 +493,7 @@ public function settle(array $parameters = [])
467493
/**
468494
* @param array $parameters
469495
*
470-
* @return \Omnipay\Common\Message\AbstractRequest
496+
* @return AbstractRequest
471497
*/
472498
public function queryBillDownloadUrl(array $parameters = [])
473499
{

src/Requests/AbstractAopRequest.php

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
use Omnipay\Alipay\Common\Signer;
66
use Omnipay\Common\Exception\InvalidRequestException;
7+
use Omnipay\Common\Http\Exception\NetworkException;
78
use Omnipay\Common\Message\AbstractRequest;
9+
use Omnipay\Common\Message\ResponseInterface;
10+
use Psr\Http\Message\StreamInterface;
811

912
abstract class AbstractAopRequest extends AbstractRequest
1013
{
@@ -28,6 +31,7 @@ abstract class AbstractAopRequest extends AbstractRequest
2831
* gateway, but will usually be either an associative array, or a SimpleXMLElement.
2932
*
3033
* @return mixed
34+
* @throws InvalidRequestException
3135
*/
3236
public function getData()
3337
{
@@ -49,6 +53,9 @@ public function getData()
4953
}
5054

5155

56+
/**
57+
* @throws InvalidRequestException
58+
*/
5259
public function validateParams()
5360
{
5461
$this->validate(
@@ -101,6 +108,13 @@ protected function convertToString()
101108
}
102109

103110

111+
/**
112+
* @param array $params
113+
* @param string $signType
114+
*
115+
* @return string|null
116+
* @throws InvalidRequestException
117+
*/
104118
protected function sign($params, $signType)
105119
{
106120
$signer = new Signer($params);
@@ -176,9 +190,8 @@ public function setAlipayPublicKey($value)
176190
/**
177191
* @param mixed $data
178192
*
179-
* @return mixed|\Omnipay\Common\Message\ResponseInterface|\Psr\Http\Message\StreamInterface
180-
* @throws \Psr\Http\Client\Exception\NetworkException
181-
* @throws \Psr\Http\Client\Exception\RequestException
193+
* @return mixed|ResponseInterface|StreamInterface
194+
* @throws NetworkException
182195
*/
183196
public function sendData($data)
184197
{
@@ -503,6 +516,9 @@ public function setAppAuthToken($value)
503516
}
504517

505518

519+
/**
520+
* @throws InvalidRequestException
521+
*/
506522
public function validateBizContent()
507523
{
508524
$data = $this->getBizContent();
@@ -519,6 +535,9 @@ public function validateBizContent()
519535
}
520536

521537

538+
/**
539+
* @throws InvalidRequestException
540+
*/
522541
public function validateBizContentOne()
523542
{
524543
$data = $this->getBizContent();
@@ -558,6 +577,9 @@ protected function filter($data)
558577
}
559578

560579

580+
/**
581+
* @throws InvalidRequestException
582+
*/
561583
protected function validateOne()
562584
{
563585
$keys = func_get_args();
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
3+
namespace Omnipay\Alipay\Requests;
4+
5+
use Omnipay\Alipay\Responses\AopCompleteRefundResponse;
6+
use Omnipay\Alipay\Responses\AopTradeQueryResponse;
7+
8+
class AopCompleteRefundRequest extends AbstractAopRequest
9+
{
10+
11+
/**
12+
* Get the raw data array for this message. The format of this varies from gateway to
13+
* gateway, but will usually be either an associative array, or a SimpleXMLElement.
14+
*
15+
* @return mixed
16+
*/
17+
public function getData()
18+
{
19+
$this->validateParams();
20+
21+
return $this->getParams();
22+
}
23+
24+
25+
public function validateParams()
26+
{
27+
$this->validate('params');
28+
}
29+
30+
31+
/**
32+
* @return mixed
33+
*/
34+
public function getParams()
35+
{
36+
return $this->getParameter('params');
37+
}
38+
39+
40+
/**
41+
* Send the request with specified data
42+
*
43+
* @param mixed $data The data to send
44+
*
45+
* @return AopCompleteRefundResponse
46+
*/
47+
public function sendData($data)
48+
{
49+
$request = new AopNotifyRequest($this->httpClient, $this->httpRequest);
50+
$request->initialize(['params' => $data]);
51+
$request->setEndpoint($this->getEndpoint());
52+
$request->setAlipayPublicKey($this->getAlipayPublicKey());
53+
$data = $request->send()->getData();
54+
55+
if (!array_get($data, 'trade_status')) {
56+
$tn = array_get($data, 'trade_no');
57+
58+
$request = new AopTradeQueryRequest($this->httpClient, $this->httpRequest);
59+
$request->initialize($this->getParameters());
60+
$request->setEndpoint($this->getEndpoint());
61+
$request->setBizContent(['trade_no' => $tn]);
62+
$request->setPrivateKey($this->getPrivateKey());
63+
64+
/**
65+
* @var AopTradeQueryResponse $response
66+
*/
67+
$response = $request->send();
68+
69+
$tradeStatus = $response->getAlipayResponse('trade_status');
70+
71+
$data['trade_status'] = $tradeStatus;
72+
}
73+
return $this->response = new AopCompleteRefundResponse($this, $data);
74+
}
75+
76+
77+
/**
78+
* @param $value
79+
*
80+
* @return $this
81+
*/
82+
public function setParams($value)
83+
{
84+
return $this->setParameter('params', $value);
85+
}
86+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
namespace Omnipay\Alipay\Responses;
4+
5+
use Omnipay\Alipay\Requests\AopCompletePurchaseRequest;
6+
7+
class AopCompleteRefundResponse extends AbstractResponse
8+
{
9+
10+
/**
11+
* @var AopCompletePurchaseRequest
12+
*/
13+
protected $request;
14+
15+
/**
16+
* Is the response successful?
17+
*
18+
* @return boolean
19+
*/
20+
public function isSuccessful()
21+
{
22+
return true;
23+
}
24+
25+
public function getResponseText()
26+
{
27+
if ($this->isSuccessful()) {
28+
return 'success';
29+
} else {
30+
return 'fail';
31+
}
32+
}
33+
34+
public function isRefunded()
35+
{
36+
$trade_status = array_get($this->data, 'trade_status');
37+
if ($trade_status) {
38+
// 全额退款为 TRADE_CLOSED;非全额退款为 TRADE_SUCCESS
39+
if ($trade_status == 'TRADE_CLOSED' || $trade_status == 'TRADE_SUCCESS') {
40+
return true;
41+
} else {
42+
return false;
43+
}
44+
} elseif (array_get($this->data, 'code') == '10000') {
45+
return true;
46+
}
47+
return false;
48+
}
49+
}

0 commit comments

Comments
 (0)