1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- <?php
- namespace app\common\service;
- use addons\epay\library\Service;
- use app\common\model\MobileOrder;
- class Refund{
- protected $type;
- protected $mobileOrder;
- protected $types=[
- 1=>'wechat',
- 2=>'alipay',
- ];
- public static function setType(MobileOrder $mobileOrder){
- $a=new self;
- $a->mobileOrder=$mobileOrder;
- $a->type=$mobileOrder['pay_type'];
- return $a;
- }
- public function refund(){
- if(!isset($this->types[$this->type])){
- throw_user('此付款方式不支持退款');
- }
- $this->{$this->types[$this->type]}();
- }
- public function getAmount($unit='f'){
- return $unit=='y'?$this->mobileOrder['amount_refund']:$this->mobileOrder['amount_refund']*100;
- }
- public function wechat(){
- $res=Service::getApp('wechat',Service::refundUrl('wechat',$this->mobileOrder['pay_no']))->refund([
- 'transaction_id'=>$this->mobileOrder['pay_no'],
- 'out_refund_no'=>$this->mobileOrder['refund_no'],
- 'total_fee'=>$this->mobileOrder['amount']*100,
- 'refund_fee'=>$this->getAmount(),
- 'op_user_id'=>Service::getWechatOpUser(),
- ]);
- if($res['return_code']!=='SUCCESS'){
- throw_user($res['return_msg']);
- }
- if($res['result_code']!='SUCCESS'){
- throw_user($res['err_code_des']);
- }
- }
- /**
- *["code"] => string(5) "10000"
- ["msg"] => string(7) "Success"
- ["buyer_logon_id"] => string(16) "xie***@gmail.com"
- ["buyer_user_id"] => string(16) "2088702452773896"
- ["fund_change"] => string(1) "Y"
- ["gmt_refund_pay"] => string(19) "2022-06-30 09:56:23"
- ["out_trade_no"] => string(26) "7dris4160g3k4hlmk69fdplav7"
- ["refund_fee"] => string(4) "0.01"
- ["send_back_fee"] => string(4) "0.00"
- ["trade_no"] => string(28) "2022063022001473891456574041"
- */
- public function alipay(){
- $res=Service::getApp('alipay',Service::refundUrl('alipay',$this->mobileOrder['pay_no']))->refund([
- 'trade_no'=>$this->mobileOrder['pay_no'],
- 'refund_amount'=>$this->getAmount('y'),
- 'refund_reason'=>$this->reason(),
- 'out_request_no'=>$this->mobileOrder['refund_no'],
- ]);
- if($res['code']!=10000){
- throw_user($res['msg']);
- }
- /*if($res['sub_code']!='TRADE_HAS_SUCCESS'){
- throw_user($res['sub_msg']);
- }*/
- }
- public function reason(){
- return sprintf("订单[%s]退款",$this->mobileOrder['order_no']);
- }
- }
|