-
-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
增加退款类 #105
Merged
Merged
增加退款类 #105
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,216 @@ | ||
<?php | ||
/** | ||
* Refund.php | ||
* | ||
* Part of Overtrue\Wechat. | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
* | ||
* @author jaring <[email protected]> | ||
* | ||
* | ||
Usage: | ||
$business = new Business($appId, $appSecret, $mchId, $mchKey); | ||
$business->setClientCert(dirname(__FILE__).'/cert/apiclient_cert.pem'); | ||
$business->setClientKey(dirname(__FILE__).'/cert/apiclient_key.pem'); | ||
$refund =new Refund($business); | ||
$refund->out_refund_no= md5(uniqid(microtime()));//退单单号 | ||
$refund->total_fee=1; //订单金额 | ||
$refund->refund_fee=1;//退款金额 | ||
$refund->out_trade_no=$order_id;//原商户订单号 | ||
var_dump($trans->getResponse()); | ||
* | ||
*/ | ||
|
||
namespace Overtrue\Wechat\Payment; | ||
use Overtrue\Wechat\Payment; | ||
use Overtrue\Wechat\Utils\XML; | ||
use Overtrue\Wechat\Utils\SignGenerator; | ||
use Overtrue\Wechat\Http; | ||
|
||
|
||
|
||
|
||
class Refund | ||
{ | ||
/** | ||
* 退款接口链接:https://api.mch.weixin.qq.com/secapi/pay/refund | ||
*/ | ||
const REFUNDORDER_URL = 'https://api.mch.weixin.qq.com/secapi/pay/refund'; | ||
|
||
/** | ||
* 商户信息 | ||
* | ||
* @var Business | ||
*/ | ||
protected $business; | ||
|
||
/** | ||
* 退款订单必填项 | ||
* | ||
* @var array | ||
*/ | ||
protected static $required = array('out_refund_no', 'total_fee', 'refund_fee'); | ||
|
||
/** | ||
* 退款订单选填项 | ||
* | ||
* @var array | ||
*/ | ||
protected static $optional = array('out_trade_no', 'transaction_id', 'device_info', 'fee_type','op_user_id'); | ||
|
||
/** | ||
* | ||
* @var array | ||
*/ | ||
protected static $params = array(); | ||
protected static $allowParams = array(); | ||
|
||
/** | ||
* 退款返回信息 | ||
* | ||
* @var Array | ||
*/ | ||
protected $refundInfo = null; | ||
|
||
public function __construct(Business $business = null) | ||
{ | ||
if(!is_null($business)) { | ||
$this->setBusiness($business); | ||
} | ||
if(sizeof(static::$allowParams)==0){ | ||
static::$allowParams=array_merge(static::$required, static::$optional); | ||
} | ||
|
||
} | ||
|
||
/** | ||
* 设置商户 | ||
* | ||
* @param Business $business | ||
* | ||
* @return $this | ||
* @throws Exception | ||
*/ | ||
public function setBusiness(Business $business) | ||
{ | ||
if(!is_null($business) ) { | ||
try { | ||
$business->checkParams(); | ||
} catch (Exception $e) { | ||
throw new Exception($e->getMessage()); | ||
} | ||
$this->business = $business; | ||
$this->refundInfo = null; | ||
} | ||
return $this; | ||
} | ||
|
||
/** | ||
* 获取商户 | ||
* | ||
* @return Business | ||
*/ | ||
public function getBusiness() | ||
{ | ||
return $this->business; | ||
} | ||
|
||
/** | ||
* 获取退款结果 | ||
* @return array | ||
* @throws Exception | ||
*/ | ||
public function getResponse() | ||
{ | ||
if(is_null($this->business) ) { | ||
throw new Exception('Business is required'); | ||
} | ||
|
||
static::$params['appid'] = $this->business->appid; | ||
static::$params['mch_id'] = $this->business->mch_id; | ||
$this->checkParams(); | ||
$signGenerator = new SignGenerator(static::$params); | ||
$signGenerator->onSortAfter(function(SignGenerator $that) { | ||
$that->key = $this->business->mch_key; | ||
}); | ||
static::$params['sign'] = $signGenerator->getResult(); | ||
|
||
$request = XML::build(static::$params); | ||
//设置Http使用的证书 | ||
$options['sslcert_path']=$this->business->getClientCert(); | ||
$options['sslkey_path']=$this->business->getClientKey(); | ||
|
||
$http = new Http(); | ||
$response = $http->request(static::REFUNDORDER_URL, Http::POST, $request,$options); | ||
if(empty($response)) { | ||
throw new Exception('Get Refund Failure:'); | ||
} | ||
$redundOrder = XML::parse($response); | ||
|
||
//返回签名数据校验 | ||
if(empty($redundOrder) || empty($redundOrder['sign'])) { | ||
throw new Exception('check sign error'); | ||
} | ||
$sign = $redundOrder['sign']; | ||
unset($redundOrder['sign']); | ||
$signGenerator = new SignGenerator($redundOrder); | ||
$signGenerator->onSortAfter(function(SignGenerator $that) { | ||
$that->key = $this->business->mch_key; | ||
}); | ||
if($sign !== $signGenerator->getResult()) { | ||
throw new Exception('check sign error'); | ||
} | ||
|
||
//返回结果判断 | ||
if(isset($redundOrder['result_code']) && | ||
($redundOrder['result_code'] === 'FAIL') ) { | ||
throw new Exception($redundOrder['err_code'].': '.$redundOrder['err_code_des']); | ||
} | ||
|
||
if(isset($redundOrder['return_code']) && | ||
$redundOrder['return_code'] === 'FAIL' ) { | ||
throw new Exception($redundOrder['return_code'].': '.$redundOrder['return_msg']); | ||
} | ||
|
||
return $this->refundInfo = $redundOrder; | ||
} | ||
|
||
|
||
/** | ||
* 检测参数值是否有效 | ||
* | ||
* @throws Exception | ||
*/ | ||
public function checkParams() | ||
{ | ||
foreach(static::$required AS $paramName) { | ||
if(!array_key_exists($paramName,static::$params)) { | ||
throw new Exception(sprintf('"%s" is required', $paramName)); | ||
} | ||
} | ||
|
||
if(!array_key_exists('transaction_id',static::$params) && !array_key_exists('out_trade_no',static::$params)) | ||
{ | ||
throw new Exception('transaction_id or out_trade_no is required'); | ||
} | ||
|
||
if(!array_key_exists('nonce_str',static::$params)) { | ||
static::$params['nonce_str'] = md5(uniqid(microtime())); | ||
} | ||
|
||
if(!array_key_exists('op_user_id',static::$params)) { | ||
static::$params['op_user_id'] =$this->business->mch_id; | ||
} | ||
|
||
} | ||
|
||
public function __set($property, $value) { | ||
if(!in_array($property, static::$allowParams)) { | ||
throw new Exception(sprintf('"%s" is not required', $property)); | ||
} | ||
return static::$params[$property]=$value; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
注释不能少 * 号