Skip to content

Commit

Permalink
Merge pull request #162 from maxsky/master
Browse files Browse the repository at this point in the history
Add complete refund notify and adjust composer.json file.
  • Loading branch information
lokielse authored May 16, 2019
2 parents 39a0173 + fa91c19 commit c1a9cbb
Show file tree
Hide file tree
Showing 8 changed files with 339 additions and 28 deletions.
10 changes: 7 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,15 @@
]
},
"require": {
"ext-json": "*",
"ext-openssl": "*",
"omnipay/common": "^3.0",
"php-http/guzzle6-adapter": "*"
"php-http/guzzle6-adapter": "^2.0"
},
"require-dev": {
"omnipay/tests": "^3.0",
"squizlabs/php_codesniffer": "3.*"
}
"squizlabs/php_codesniffer": "^3.4"
},
"minimum-stability": "dev",
"prefer-stable": true
}
46 changes: 36 additions & 10 deletions src/AbstractAopGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Omnipay\Alipay;

use Omnipay\Alipay\Requests\AopCompletePurchaseRequest;
use Omnipay\Alipay\Requests\AopCompleteRefundRequest;
use Omnipay\Alipay\Requests\AopTradeCancelRequest;
use Omnipay\Alipay\Requests\AopTradeCloseRequest;
use Omnipay\Alipay\Requests\AopTradeOrderSettleRequest;
Expand All @@ -14,6 +15,7 @@
use Omnipay\Alipay\Requests\DataServiceBillDownloadUrlQueryRequest;
use Omnipay\Common\AbstractGateway;
use Omnipay\Common\Exception\InvalidRequestException;
use Omnipay\Common\Message\AbstractRequest;

abstract class AbstractAopGateway extends AbstractGateway
{
Expand Down Expand Up @@ -305,6 +307,10 @@ public function setAlipaySdk($value)
}


/**
* @return AbstractAopGateway
* @throws InvalidRequestException
*/
public function production()
{
return $this->setEnvironment('production');
Expand Down Expand Up @@ -342,16 +348,22 @@ public function setEndpoint($value)
}


/**
* @return AbstractAopGateway
* @throws InvalidRequestException
*/
public function sandbox()
{
return $this->setEnvironment('sandbox');
}


/**
* @noinspection PhpDocRedundantThrowsInspection
*
* @param array $parameters
*
* @return \Omnipay\Alipay\Requests\AopCompletePurchaseRequest
* @return AopCompletePurchaseRequest|AbstractRequest
* @throws InvalidRequestException
*/
public function completePurchase(array $parameters = [])
Expand All @@ -360,12 +372,26 @@ public function completePurchase(array $parameters = [])
}


/**
* @noinspection PhpDocRedundantThrowsInspection
*
* @param array $parameters
*
* @return AopCompleteRefundRequest|AbstractRequest
* @throws InvalidRequestException
*/
public function completeRefund(array $parameters = [])
{
return $this->createRequest(AopCompleteRefundRequest::class, $parameters);
}


/**
* Query Order Status
*
* @param array $parameters
*
* @return \Omnipay\Alipay\Requests\AopTradeQueryRequest
* @return AopTradeQueryRequest|AbstractRequest
*/
public function query(array $parameters = [])
{
Expand All @@ -378,7 +404,7 @@ public function query(array $parameters = [])
*
* @param array $parameters
*
* @return \Omnipay\Alipay\Requests\AopTradeRefundRequest
* @return AopTradeRefundRequest|AbstractRequest
*/
public function refund(array $parameters = [])
{
Expand All @@ -391,7 +417,7 @@ public function refund(array $parameters = [])
*
* @param array $parameters
*
* @return \Omnipay\Alipay\Requests\AopTradeRefundQueryRequest
* @return AopTradeRefundQueryRequest|AbstractRequest
*/
public function refundQuery(array $parameters = [])
{
Expand All @@ -404,7 +430,7 @@ public function refundQuery(array $parameters = [])
*
* @param array $parameters
*
* @return \Omnipay\Alipay\Requests\AopTradeCloseRequest
* @return AopTradeCloseRequest|AbstractRequest
*/
public function close(array $parameters = [])
{
Expand All @@ -417,7 +443,7 @@ public function close(array $parameters = [])
*
* @param array $parameters
*
* @return \Omnipay\Alipay\Requests\AopTradeCancelRequest
* @return AopTradeCancelRequest|AbstractRequest
*/
public function cancel(array $parameters = [])
{
Expand All @@ -430,7 +456,7 @@ public function cancel(array $parameters = [])
*
* @param array $parameters
*
* @return \Omnipay\Alipay\Requests\AopTransferToAccountRequest
* @return AopTransferToAccountRequest|AbstractRequest
*/
public function transfer(array $parameters = [])
{
Expand All @@ -443,7 +469,7 @@ public function transfer(array $parameters = [])
*
* @param array $parameters
*
* @return \Omnipay\Alipay\Requests\AopTransferToAccountQueryRequest
* @return AopTransferToAccountQueryRequest|AbstractRequest
*/
public function transferQuery(array $parameters = [])
{
Expand All @@ -456,7 +482,7 @@ public function transferQuery(array $parameters = [])
*
* @param array $parameters
*
* @return \Omnipay\Alipay\Requests\AopTradeCancelRequest
* @return AopTradeCancelRequest|AbstractRequest
*/
public function settle(array $parameters = [])
{
Expand All @@ -467,7 +493,7 @@ public function settle(array $parameters = [])
/**
* @param array $parameters
*
* @return \Omnipay\Common\Message\AbstractRequest
* @return AbstractRequest
*/
public function queryBillDownloadUrl(array $parameters = [])
{
Expand Down
28 changes: 25 additions & 3 deletions src/Requests/AbstractAopRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

use Omnipay\Alipay\Common\Signer;
use Omnipay\Common\Exception\InvalidRequestException;
use Omnipay\Common\Http\Exception\NetworkException;
use Omnipay\Common\Message\AbstractRequest;
use Omnipay\Common\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;

abstract class AbstractAopRequest extends AbstractRequest
{
Expand All @@ -28,6 +31,7 @@ abstract class AbstractAopRequest extends AbstractRequest
* gateway, but will usually be either an associative array, or a SimpleXMLElement.
*
* @return mixed
* @throws InvalidRequestException
*/
public function getData()
{
Expand All @@ -49,6 +53,9 @@ public function getData()
}


/**
* @throws InvalidRequestException
*/
public function validateParams()
{
$this->validate(
Expand Down Expand Up @@ -101,6 +108,13 @@ protected function convertToString()
}


/**
* @param array $params
* @param string $signType
*
* @return string|null
* @throws InvalidRequestException
*/
protected function sign($params, $signType)
{
$signer = new Signer($params);
Expand Down Expand Up @@ -176,9 +190,8 @@ public function setAlipayPublicKey($value)
/**
* @param mixed $data
*
* @return mixed|\Omnipay\Common\Message\ResponseInterface|\Psr\Http\Message\StreamInterface
* @throws \Psr\Http\Client\Exception\NetworkException
* @throws \Psr\Http\Client\Exception\RequestException
* @return mixed|ResponseInterface|StreamInterface
* @throws NetworkException
*/
public function sendData($data)
{
Expand Down Expand Up @@ -503,6 +516,9 @@ public function setAppAuthToken($value)
}


/**
* @throws InvalidRequestException
*/
public function validateBizContent()
{
$data = $this->getBizContent();
Expand All @@ -519,6 +535,9 @@ public function validateBizContent()
}


/**
* @throws InvalidRequestException
*/
public function validateBizContentOne()
{
$data = $this->getBizContent();
Expand Down Expand Up @@ -558,6 +577,9 @@ protected function filter($data)
}


/**
* @throws InvalidRequestException
*/
protected function validateOne()
{
$keys = func_get_args();
Expand Down
86 changes: 86 additions & 0 deletions src/Requests/AopCompleteRefundRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

namespace Omnipay\Alipay\Requests;

use Omnipay\Alipay\Responses\AopCompleteRefundResponse;
use Omnipay\Alipay\Responses\AopTradeQueryResponse;

class AopCompleteRefundRequest extends AbstractAopRequest
{

/**
* Get the raw data array for this message. The format of this varies from gateway to
* gateway, but will usually be either an associative array, or a SimpleXMLElement.
*
* @return mixed
*/
public function getData()
{
$this->validateParams();

return $this->getParams();
}


public function validateParams()
{
$this->validate('params');
}


/**
* @return mixed
*/
public function getParams()
{
return $this->getParameter('params');
}


/**
* Send the request with specified data
*
* @param mixed $data The data to send
*
* @return AopCompleteRefundResponse
*/
public function sendData($data)
{
$request = new AopNotifyRequest($this->httpClient, $this->httpRequest);
$request->initialize(['params' => $data]);
$request->setEndpoint($this->getEndpoint());
$request->setAlipayPublicKey($this->getAlipayPublicKey());
$data = $request->send()->getData();

if (!array_get($data, 'trade_status')) {
$tn = array_get($data, 'trade_no');

$request = new AopTradeQueryRequest($this->httpClient, $this->httpRequest);
$request->initialize($this->getParameters());
$request->setEndpoint($this->getEndpoint());
$request->setBizContent(['trade_no' => $tn]);
$request->setPrivateKey($this->getPrivateKey());

/**
* @var AopTradeQueryResponse $response
*/
$response = $request->send();

$tradeStatus = $response->getAlipayResponse('trade_status');

$data['trade_status'] = $tradeStatus;
}
return $this->response = new AopCompleteRefundResponse($this, $data);
}


/**
* @param $value
*
* @return $this
*/
public function setParams($value)
{
return $this->setParameter('params', $value);
}
}
49 changes: 49 additions & 0 deletions src/Responses/AopCompleteRefundResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace Omnipay\Alipay\Responses;

use Omnipay\Alipay\Requests\AopCompletePurchaseRequest;

class AopCompleteRefundResponse extends AbstractResponse
{

/**
* @var AopCompletePurchaseRequest
*/
protected $request;

/**
* Is the response successful?
*
* @return boolean
*/
public function isSuccessful()
{
return true;
}

public function getResponseText()
{
if ($this->isSuccessful()) {
return 'success';
} else {
return 'fail';
}
}

public function isRefunded()
{
$trade_status = array_get($this->data, 'trade_status');
if ($trade_status) {
// 全额退款为 TRADE_CLOSED;非全额退款为 TRADE_SUCCESS
if ($trade_status == 'TRADE_CLOSED' || $trade_status == 'TRADE_SUCCESS') {
return true;
} else {
return false;
}
} elseif (array_get($this->data, 'code') == '10000') {
return true;
}
return false;
}
}
Loading

0 comments on commit c1a9cbb

Please sign in to comment.