|
@@ -1,24 +1,102 @@
|
|
|
<?php
|
|
|
namespace app\common\service;
|
|
|
-use app\common\model\DiscountActivity;
|
|
|
-use app\common\model\DiscountGoods;
|
|
|
-use app\common\model\GroupActivity;
|
|
|
-use app\common\model\GroupGoods;
|
|
|
-use app\common\model\ReductionActivity;
|
|
|
-use app\common\model\SeckillActivity;
|
|
|
-use app\common\model\SeckillGoods;
|
|
|
+use app\common\model\ActivityApply;
|
|
|
+use app\common\model\ActivityApplyItem;
|
|
|
+use app\common\model\GoodsOrderRefund;
|
|
|
+use app\common\model\StoreOrderRefund;
|
|
|
+use app\api\controller\Alipay;
|
|
|
+use EasyWeChat\Factory;
|
|
|
+use library\tools\Data;
|
|
|
+use think\Db;
|
|
|
+use think\Exception;
|
|
|
|
|
|
/**
|
|
|
* 活动管理
|
|
|
* Class Activity
|
|
|
*/
|
|
|
-class Activity
|
|
|
+class Activity extends SerBase
|
|
|
{
|
|
|
|
|
|
- //订单退款
|
|
|
- public static function orderRefund()
|
|
|
+ /**
|
|
|
+ * 订单申请退款
|
|
|
+ * @param $order_id 订单id
|
|
|
+ * @param $item_id 票id
|
|
|
+ * @param $is_refund 是否立即退款
|
|
|
+ */
|
|
|
+ public static function orderApplyRefund($order_id,$item_id,$is_refund = 0)
|
|
|
{
|
|
|
+ //$ret_val = static::$ret_val;// ['code' => 200 , 'msg'=>'ok','extend'=>''];
|
|
|
+ $order_info = ActivityApply::where('id',$order_id)->find()->toArray();
|
|
|
+ $item_info = ActivityApplyItem::where('id',$item_id)->find()->toArray();
|
|
|
+ if(!$order_info['pay_state']) return ['code'=>201,'msg' => '订单未支付'];
|
|
|
+ $refund_money = StoreOrderRefund::getRefundMoney($order_id);
|
|
|
+ if($refund_money >= $order_info['money'])return ['code'=>201,'msg' => '订单已全额退款'];
|
|
|
+ $refund_info = StoreOrderRefund::where(['order_id'=>$order_id,'item_id'=>$item_id,'type'=>1])->find();
|
|
|
+ if(!$refund_info && in_array($refund_info->status,[0,1,3])) return ['code'=>201,'msg' => '订单已申请退款'];
|
|
|
+ if($item_info['is_hx'] || $item_info['ticket_status'])return ['code'=>201,'msg' => '票已核销无法申请'];
|
|
|
+ // 申请记录详情
|
|
|
+ Data::save('StoreOrderRefund',[
|
|
|
+ 'user_id' => $order_info['user_id'],
|
|
|
+ 'order_id' => $order_id,
|
|
|
+ 'item_id' => $item_id,
|
|
|
+ 'type' => 1,
|
|
|
+ 'is_deleted' => 0,
|
|
|
+ 'sell_price' => $item_info['total_money'],
|
|
|
+ 'refund_money' => $item_info['money'],//优惠后价格
|
|
|
+ 'refund_no' => get_order_sn(),
|
|
|
+ ],'order_id',['order_id'=>$order_id,'item_id'=>$item_id,'type'=>1]);
|
|
|
+ if($is_refund) {
|
|
|
+ $refund_id = StoreOrderRefund::where(['order_id'=>$order_id,'item_id'=>$item_id,'type'=>1])->value('id');
|
|
|
+ $refund_res = static::orderRefund($refund_id);
|
|
|
+ }
|
|
|
+ return ['code'=>200,'msg' => '申请成功'];
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
+ /**
|
|
|
+ * 确定退款
|
|
|
+ * @param $refund_id 退款记录id
|
|
|
+ */
|
|
|
+ public static function orderRefund($refund_id)
|
|
|
+ {
|
|
|
+ $refund_info = StoreOrderRefund::where('id',$refund_id)->find()->toArray();
|
|
|
+ if(in_array($refund_info['status'],[2,3,5]) || $refund_info['is_deleted']) return ['code'=>201,'msg' => '退款记录异常'];
|
|
|
+ $order_info = ActivityApply::where('id',$refund_info['order_id'])->find()->toArray();
|
|
|
+ $item_info = ActivityApplyItem::where('id',$refund_info['item_id'])->find()->toArray();
|
|
|
+ if($item_info['is_hx'] || $item_info['ticket_status'])return ['code'=>201,'msg' => '票已核销无法退款'];
|
|
|
+ // (1.h5微信,2.h5支付宝,3.app微信,4.app支付宝,5.h5微信内支付,9线下支付)
|
|
|
+ if($refund_info['refund_money'] <= 0 || $refund_info['refund_money'] > $order_info['money']) return ['code'=>201,'订单退款金额有误'];
|
|
|
+ Db::startTrans();
|
|
|
+ try {
|
|
|
+ $refund_no = $refund_info['refund_no'] ? $refund_info['refund_no'] : get_order_sn();
|
|
|
+ //微信退款
|
|
|
+ if(in_array($order_info['pay_type'],[1,3,5])){ // h5
|
|
|
+ $app = $order_info['pay_type'] == 3 ? Factory::payment(config('app.app_wx')) : Factory::payment(config('app.wx_pay'));
|
|
|
+ $result = $app->refund->byOutTradeNumber($order_info['pay_no'], $refund_no, $order_info['money'] * 100, $refund_info['refund_money']*100, ['refund_desc' => '订单退款']);
|
|
|
+ if($result['return_code'] != 'SUCCESS') throw new Exception('微信退款异常');
|
|
|
+ if($result['result_code'] != 'SUCCESS') throw new Exception($result['err_code_des']);
|
|
|
+ // 修改退款记录状态
|
|
|
+ GoodsOrderRefund::where(['id'=>$refund_info['id']])->update(['refund_no'=>$refund_no,'status'=>3]);
|
|
|
+ }
|
|
|
+ // 支付宝退款
|
|
|
+ if(in_array($order_info['pay_type'],[2,4]))
|
|
|
+ {
|
|
|
+ $result = Alipay::aliRefund($order_info['pay_no'], $refund_info['refund_money'],$order_info['pay_type'] == 2 ?'H5':'APP');
|
|
|
+ if(!$result) throw new Exception('支付宝退款异常');
|
|
|
+ // 修改退款记录状态
|
|
|
+ GoodsOrderRefund::where(['id'=>$refund_info['id']])->update(['refund_no'=>$refund_no,'status'=>3]);
|
|
|
+ }
|
|
|
+ if(in_array($order_info['pay_type'],[9])) {
|
|
|
+ // 修改退款记录状态
|
|
|
+ GoodsOrderRefund::where(['id'=>$refund_info['id']])->update(['refund_no'=>$refund_no,'status'=>3]);
|
|
|
+ }
|
|
|
+ Db::commit();
|
|
|
+ }catch (\Exception $e){
|
|
|
+ Db::rollback();
|
|
|
+ GoodsOrderRefund::where(['id'=>$refund_info['id']])->update(['status'=>4]);
|
|
|
+ return ['code'=>201 ,'msg'=>$e->getMessage()];
|
|
|
+ }
|
|
|
+ return ['code'=>200,'msg'=>'退款成功'];
|
|
|
}
|
|
|
|
|
|
|