xxxrrrdddd 3 年之前
父节点
当前提交
e8d131aed0
共有 3 个文件被更改,包括 42 次插入6 次删除
  1. 15 1
      addons/epay/library/Service.php
  2. 23 5
      application/common/service/Refund.php
  3. 4 0
      application/index/controller/Payment.php

+ 15 - 1
addons/epay/library/Service.php

@@ -269,6 +269,15 @@ class Service
         return $config;
     }
 
+    public static function getApp($type,$notify_url){
+        static $app=[];
+        $config=array_merge(self::getConfig($type),compact('notify_url'));
+        if(!isset($app[$type])){
+            $app[$type]=$type=='wechat'?Pay::wechat($config):Pay::alipay($config);
+        }
+        return $app[$type];
+    }
+
     /**
      * 获取微信Openid
      *
@@ -304,5 +313,10 @@ class Service
     public static function notifyUrlJd($order_no){
         return request()->domain()."/index/payment/notify_jd/order_no/$order_no";
     }
-
+    public static function refundUrl($type,$order_no){
+        return request()->domain()."/index/payment/refund_url/type/{$type}/order/$order_no";
+    }
+    public static function getWechatOpUser(){
+        return self::getConfig('wechat')['mch_id']??'';
+    }
 }

+ 23 - 5
application/common/service/Refund.php

@@ -2,17 +2,20 @@
 namespace app\common\service;
 
 
+use addons\epay\library\Service;
+use app\common\model\MobileOrder;
+
 class Refund{
     protected $type;
-    protected $amount;
+    protected $mobileOrder;
     protected $types=[
         1=>'wechat',
         2=>'alipay',
     ];
-    public static function setType($type,$amount){
+    public static function setType(MobileOrder $mobileOrder){
         $a=new self;
-        $a->type=$type;
-        $a->amount=$amount;
+        $a->mobileOrder=$mobileOrder;
+        $a->type=$mobileOrder['pay_type'];
     }
     public function refund(){
         if(!isset($this->types[$this->type])){
@@ -20,7 +23,22 @@ class Refund{
         }
         $this->{$this->types[$this->type]}();
     }
+    public function getAmount(){
+        return $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'],
+            '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']);
+        }
     }
 }

+ 4 - 0
application/index/controller/Payment.php

@@ -138,4 +138,8 @@ class Payment extends Frontend
         //下面这句必须要执行,且在此之前不能有任何输出
         return 'success';
     }
+
+    public function refund_url($type,$order){
+
+    }
 }