xieruidong il y a 2 ans
Parent
commit
a5e337807b

+ 1 - 1
application/common/library/upacp_demo_b2c/sdk/acp_service.php

@@ -156,7 +156,7 @@ class AcpService {
 
 	/**
 	 * 验签
-	 * @param $params 应答数组
+	 * @param $params array 应答数组
 	 * @return 是否成功
 	 */
 	static function validate($params) {

+ 4 - 0
application/common/model/Orders.php

@@ -9,6 +9,7 @@ use Yansongda\Supports\Arr;
  * @property OrderInfo info
  * @property User user
  * @property bool is_wait_pay
+ * @property Payment payment
  */
 class Orders extends Model
 {
@@ -69,6 +70,9 @@ class Orders extends Model
     public function user(){
         return $this->belongsTo(User::class);
     }
+    public function payment(){
+        return $this->belongsTo(Payment::class);
+    }
     public function address(){
         return $this->hasOne(OrderAddress::class,'order_id');
     }

+ 45 - 3
application/common/model/Refund.php

@@ -2,10 +2,12 @@
 
 namespace app\common\model;
 
+use app\common\service\OrderRefundService;
 use think\Model;
 
 /**
  * 短信验证码
+ * @property Orders orders
  */
 class Refund Extends Model
 {
@@ -36,6 +38,7 @@ class Refund Extends Model
         self::REFUND_TYPE_GOODS=>'仅退货',
     ];
     public static $refundTypeGoods=[self::REFUND_TYPE_ALL,self::REFUND_TYPE_GOODS];
+    public static $refundTypeMoney=[self::REFUND_TYPE_MONEY,self::REFUND_TYPE_ALL];
 
     /**
      * @return string[]
@@ -52,10 +55,21 @@ class Refund Extends Model
     {
         return self::$refundTypeGoods;
     }
+
+    /**
+     * @return int[]
+     */
+    public static function getRefundTypeMoney(): array
+    {
+        return self::$refundTypeMoney;
+    }
     protected $autoWriteTimestamp=true;
     public function orderInfo(){
         return $this->belongsTo(OrderInfo::class);
     }
+    public function orders(){
+        return $this->belongsTo(Orders::class,'order_id');
+    }
     public function allowCancel(){
         return in_array($this['refund_status'],[
             self::REFUND_ING,
@@ -71,17 +85,45 @@ class Refund Extends Model
     public function allowAudit(){
         return $this['refund_status']==self::REFUND_ING;
     }
+    #退款相关
+    /**
+     * 退款金额
+     */
+    public function getRefundAmount(){
+        return $this['amount'];
+    }
+    public function refundResult($succ,$remark=''){
+        $this['pay_status']=$succ?1:2;
+        $this['pay_remark']=$remark;
+        $this->save();
+    }
+    #end
     public function makeAudit($pass){
         $this['refund_status']=$pass?self::REFUND_PASS:self::REFUND_REJECT;
+        $refundMoney=false;
         if($pass){
             #如果退货
             if(in_array($this['refund_type'],self::getRefundTypeGoods())){
                 $this->orderInfo()->update(['is_return_goods'=>1]);
             }
-        }else{
-
+            #如果退款,执行退款
+            if(in_array($this['refund_type'],self::getRefundTypeMoney())){
+                $this['order_no']=order_no('TK');
+                $refundMoney=true;
+            }
+        }
+        if(!$this->save()){
+            throw_user('保存失败');
+        }
+        if($refundMoney){
+            $payment=$this->orders->payment??null;
+            if($payment){
+                $refund=new OrderRefundService();
+                $refund->setPayment($payment);
+                $refund->setRefund($this);
+                $refund->pay();
+            }
         }
-        $this->save();
     }
 
 

+ 62 - 0
application/common/service/OrderRefundService.php

@@ -0,0 +1,62 @@
+<?php
+namespace app\common\service;
+
+use addons\epay\library\Service;
+use app\common\model\Payment;
+use fast\Arr;
+use think\App;
+use think\Model;
+use Yansongda\Pay\Exceptions\GatewayException;
+
+class OrderRefundService{
+    /** @var Payment */
+    protected $payment;
+    /** @var Model */
+    protected $refund;
+    /**
+     * @param Payment $payment
+     */
+    public function setPayment(Payment $payment): void
+    {
+        $this->payment = $payment;
+    }
+
+    /**
+     * @param Model $refund
+     */
+    public function setRefund(Model $refund): void
+    {
+        $this->refund = $refund;
+    }
+    /**
+     * @return mixed
+     */
+    public function getAmount($unit='y')
+    {
+        return moneyFormat($this->refund->getRefundAmount(),$unit);
+    }
+
+   public function companyBank(){}
+   public function wechat(){
+
+   }
+   public function alipay(){
+       try {
+           Service::getApp('alipay','')->refund([
+               'out_trade_no' => $this->payment->order_no,
+               'refund_amount' => $this->getAmount(),
+           ]);
+           $this->refund->refundResult(true);
+       }catch (GatewayException $e){
+           throw_user($e->raw['alipay_trade_refund_response']['sub_msg']);
+       }
+
+   }
+   public function bankUnion(){}
+   public function otherUser(){}
+   public function offline(){}
+   public function pay(){
+        $method=Arr::get(OrderPayService::$methods,$this->payment->pay_type);
+        return App::invokeMethod([$this,$method]);
+   }
+}