Refund.php 7.36 KB
<?php

/**
 * 
 * @author tongdesheng
 *
 */
class YHMApi_App_V1_Refund extends YHMApi_Common {

    /**
     * 申请退款
     * @param array $params
     * @param string $fields
     */
    public static function req(array $params, $fields = "*") {
        if (empty($params['uid'])) {
            return self::result(400, 'uid不能为空');
        }
        if (empty($params['order_code'])) {
            return self::result(400, '订单号不能为空');
        }
        if (empty($params['refund_amount'])) {
            return self::result(400, '申请退款金额不能为空');
        }
        $receive_uid = $params['uid']; //申请人
        $order_code = $params['order_code'];  //订单号
        $refund_amount = $params['refund_amount']; //申请金额
        //检测是否已经申请
        $orders_status = YHMOrders_Models_Refund_Client::getByOrdercode($order_code);
        //如果卖家已拒绝退款,可再申请,并返回最新的申请记录
        if ($orders_status['refund_status'] == YHMConfig_Orderstatus::REFUND_BUYER_REQ) {
            return self::result(500, '您已经申请过了,请等待卖家回复');
        }
         
        //订单信息
        $order_info = YHMOrders_Models_Orders_Client::getOrdersInfoByOrderCode($order_code);
        $refund_uid = $order_info['seller_uid']; //退款人
       
          if ($orders_status['refund_status'] == YHMConfig_Orderstatus::REFUND_SELLER_CONFIRM) {
            return self::result(500, '卖家已经同意退款');
        }
        $id = YHMOrders_Models_Refund_Client::setRefund($refund_uid, $receive_uid, $order_code, YHMConfig_Orderstatus::REFUND_BUYER_REQ, $refund_amount);
        if ($id < 1) {
            return self::result(500, '申请失败');
        }
        
        
        //订单信息
        $order_goods_info = YHMOrders_Models_Orders_Client::getGoodsInfoByOrderCode($params['order_code']);
        $order_goods = current($order_goods_info);
      // $goods_info = YHMProduct_Models_Goods_Client::getOneByProductSkc($order_goods['product_skc']);
        $goods_image =$order_goods['goods_images'];// YHMProduct_Models_Goodsimages_Client::getDefaultImageBySkc($order_goods['product_skc']);
        //发送系统消息
        $message = new YHMIm_MessagesParam_YHMOrderStatus;
        $message->text = '您有一笔订单被申请退款';
        $message->seller_uid = $order_info['seller_uid'];
        $message->product_skc = $order_goods['product_skc'];
        $message->product_name = $order_goods['goods_name'];
        $message->image_url = YHMUpload_Images::template($goods_image, 'yhfair-product');
        $message->sale_price = (int) $order_goods['last_price'];
        $message->stock_number = $order_goods['buy_number'];
        $message->size_name = !empty($order_goods['size_name']) ? $order_goods['size_name'] : '-';
        $message->order_code = $params['order_code'];
        $send = YHMIm_Client::yhmOrderStatus(0, $order_info['seller_uid'], $message);
        //插入订单历史
        YHMOrders_Models_Status_Client::setOrderstatus($order_code, YHMConfig_Orderstatus::REFUND_BUYER_REQ, $receive_uid);
        return self::result(200, '申请成功', $id);
    }

    /**
     * 买家取消申请退款
     * @param array $params
     * @param string $fields
     */
    public static function cancel(array $params, $fields = "*") {
        if (empty($params['uid'])) {
            return self::result(400, 'uid不能为空');
        }
        if (empty($params['order_code'])) {
            return self::result(400, '订单号不能为空');
        }
        $order_code = $params['order_code'];
        //查看当前退款状态
        $orders_status = YHMOrders_Models_Refund_Client::getByOrdercode($order_code);
        if (empty($orders_status)) {
            return self::result(400, '暂无退款申请');
        }
        if ($orders_status['refund_status'] == YHMConfig_Orderstatus::REFUND_BUYER_CANCEL) {
            return self::result(500, '您已经取消退款');
        }
        if ($orders_status['refund_status'] == YHMConfig_Orderstatus::REFUND_SELLER_REJECT) {
            return self::result(500, '卖家已经拒绝退款');
        }
          if ($orders_status['refund_status'] == YHMConfig_Orderstatus::REFUND_SELLER_CONFIRM) {
            return self::result(500, '卖家已经同意退款');
        }
        //修改退货状态
        YHMOrders_Models_Refund_Client::updateStatus($order_code, YHMConfig_Orderstatus::REFUND_BUYER_CANCEL);
        //插入历史
        YHMOrders_Models_Status_Client::setOrderstatus($order_code, YHMConfig_Orderstatus::REFUND_BUYER_CANCEL, $params['uid']);
        return self::result(200, '取消申请退款成功');
    }

    /**
     * 拒绝退款申请
     * @param array $params
     * @param string $fields
     */
    public static function reject(array $params, $fields = "*") {
        if (empty($params['uid'])) {
            return self::result(400, 'uid不能为空');
        }
        if (empty($params['order_code'])) {
            return self::result(400, '订单号不能为空');
        }
        $order_code = $params['order_code'];
        $orders_status = YHMOrders_Models_Refund_Client::getByOrdercode($order_code);
        if (empty($orders_status)) {
            return self::result(400, '暂无退款申请');
        }
         if ($orders_status['refund_status'] == YHMConfig_Orderstatus::REFUND_BUYER_CANCEL) {
            return self::result(500, '卖家已经取消退款!');
        }
        if ($orders_status['refund_status'] == YHMConfig_Orderstatus::REFUND_SELLER_REJECT) {
            return self::result(500, '您已经拒绝退款');
        }
        //修改退款状态
        YHMOrders_Models_Refund_Client::updateStatus($order_code, YHMConfig_Orderstatus::REFUND_SELLER_REJECT);
        //插入历史
        YHMOrders_Models_Status_Client::setOrderstatus($order_code, YHMConfig_Orderstatus::REFUND_SELLER_REJECT, $params['uid']);
        return self::result(200, '拒绝退款申请成功');
    }

    /**
     * 确认退款申请
     * @param array $params
     * @param string $fields
     */
    public static function confirm(array $params, $fields = "*") {
        if (empty($params['uid'])) {
            return self::result(400, 'uid不能为空');
        }
        if (empty($params['order_code'])) {
            return self::result(400, '订单号不能为空');
        }
        $order_code = $params['order_code'];
        $orders_status = YHMOrders_Models_Refund_Client::getByOrdercode($order_code);
        if (empty($orders_status)) {
            return self::result(400, '暂无退款申请');
        }
         if ($orders_status['refund_status'] == YHMConfig_Orderstatus::REFUND_BUYER_CANCEL) {
            return self::result(500, '卖家已经取消退款!');
        }
        if ($orders_status['refund_status'] == YHMConfig_Orderstatus::REFUND_SELLER_CONFIRM) {
            return self::result(500, '您已确认退款');
        }
        YHMOrders_Models_Orders_Client::updateOrderStatus($order_code, YHMConfig_Orderstatus::REFUND_SELLER_CONFIRM);
        //修改退款状态
        YHMOrders_Models_Refund_Client::updateStatus($order_code, YHMConfig_Orderstatus::REFUND_SELLER_CONFIRM);
        //插入历史
        YHMOrders_Models_Status_Client::setOrderstatus($order_code, YHMConfig_Orderstatus::REFUND_SELLER_CONFIRM, $params['uid']);
        return self::result(200, '确认退款申请成功');
    }

}