handlePaidNotify(function ($message, $fail) { $type = isset($message['attach']) ? $message['attach'] : $this->getTypeTag($message['out_trade_no']); $class = $this->byTypeGetModel($type); $model = new $class; $order = $model->where('no', $message['out_trade_no'])->first(); if (!$order || (int)$order->pay_status === Order::PAY_STATUS_OK) { // 如果订单不存在 或者 订单已经支付过了 return true; // 告诉微信,我已经处理完了,订单没找到,别再通知我了 } if ($message['result_code'] === 'FAIL') { Log::warning('[wechat-pay]:' . json_encode($message, true)); return true; } else if ($message['return_code'] === 'SUCCESS') { // TODO: 你的发货逻辑 $order->pay_status = Order::PAY_STATUS_OK; $order->pay_time = now(); $order->pay_type = Order::PAY_TYPE_WECHAT; $order->pay_money = bcdiv($message['total_fee'], 100, 2); $order->save(); $order->pay_order_callback(); return true; } }); } catch (\Exception $exception) { Log::error($exception->getMessage()); } } /** * 退款结果通知回调 * @return mixed * User: Mead */ public function refundNotify() { try { $payment = app('wechat.payment'); return $payment->handleRefundedNotify(function ($message, $reqInfo, $fail) { if ($message['return_code'] === 'FAIL') { Log::warning('[wechat-pay-refund]:' . json_encode($message, true)); return true; } $type = isset($message['attach']) ? $message['attach'] : $this->getTypeTag($reqInfo['out_refund_no']); $class = $this->byTypeGetModel($type); if ($reqInfo['refund_request_source'] === 'VENDOR_PLATFORM') { //商户平台退款 $type = $this->getTypeTag($reqInfo['out_trade_no']); $class = $this->byTypeGetModel($type); if (empty($class)) { return $this->sendOtherRefundOrder($reqInfo['out_trade_no'], 1); } $model = new $class; $d_order = $model->where('no', $reqInfo['out_trade_no'])->first(); if ((int)$d_order->is_refund === DepositOrder::REFUND_OK) { return true; } if ($message['return_code'] == 'SUCCESS') { if ($reqInfo['refund_status'] == 'SUCCESS') { $d_order->is_refund = DepositOrder::REFUND_OK; $d_order->save(); User::where('id', $d_order->user_id)->update([ 'deposit_money' => 0, 'is_deposit' => User::DEPOSIT_NO ]); } return true; } } else { $no = $reqInfo['out_refund_no']; Log::error("{$type}==>{$no}"); if (empty($class)) { //其他项目 return $this->sendOtherRefundOrder($reqInfo['out_refund_no']); } $model = new $class; $refund = $model->where('no', $reqInfo['out_refund_no'])->first(); if (!$refund || (int)$refund->pay_status === RefundLog::PAY_STATUS_OK) { return true; } $refund->result = php2js($reqInfo); if ($message['return_code'] == 'SUCCESS') { if ($reqInfo['refund_status'] == 'SUCCESS') { $refund->pay_status = RefundLog::PAY_STATUS_OK; $refund->pay_time = date('Y-m-d H:i:s'); $refund->save(); $refund->refund_order_callback(); } else { $refund->pay_status = RefundLog::PAY_STATUS_ERROR; $refund->pay_time = date('Y-m-d H:i:s'); $refund->save(); } } return true; // 返回 true 告诉微信“我已处理完成” } }); } catch (\Exception $exception) { Log::error($exception->getMessage()); } } /** * 根据订单号实例model * @param $type * @return string * User: Mead */ protected function byTypeGetModel($type) { $class = ''; switch ($type) { case DepositOrder::NO_TAG: // 缴纳押金 $class = DepositOrder::class; break; case RechargeOrder::NO_TAG: // 充值 $class = RechargeOrder::class; break; case Order::NO_TAG: // 骑行订单 $class = Order::class; break; case RefundLog::NO_TAG: // 退款 $class = RefundLog::class; break; case CardRidingOrder::NO_TAG: // 骑行卡 $class = CardRidingOrder::class; break; } return $class; } /** * 发送给小斑马 * @param $no * @param int $is_order_no * @return bool * User: Mead */ public function sendOtherRefundOrder($no, $is_order_no = 0) { $re = curl_get("http://api.xbm.weilaibike.com/api/payments/wechat-refund-api?no={$no}&is-order-no={$is_order_no}"); if ($re['status']) { return true; } return false; } /** * 检查订单是否退款成功 * @param Request $request * @return mixed * User: Mead */ public function isOrderRefundPay(Request $request) { try { $no = $request->get('no'); $is_order_no = $request->get('is-order-no', 0); $type = $this->getTypeTag($no); $class = $this->byTypeGetModel($type); if (empty($class)) { return $this->error(); } $payment = app('wechat.payment'); if ($is_order_no) { $re = $payment->refund->queryByOutTradeNumber($no); } else { $re = $payment->refund->queryByOutRefundNumber($no); } if ($re['return_code'] === 'SUCCESS' && $re['refund_status_0'] === 'SUCCESS') { $model = new $class; $refund = $model->where('no', $no)->first(); if ((int)$refund->pay_status === RefundLog::PAY_STATUS_OK) { return $this->success(); } if ($re['return_code'] === 'SUCCESS' && $re['refund_status_0'] === 'SUCCESS') { $model = new $class; $refund = $model->where('no', $no)->first(); if (in_array($type, [RefundLog::NO_TAG])) { if ((int)$refund->pay_status === RefundLog::PAY_STATUS_OK) { return $this->success(); } //退押金 $refund->pay_status = RefundLog::PAY_STATUS_OK; $refund->pay_time = date('Y-m-d H:i:s'); $refund->save(); } $refund->refund_order_callback(); return $this->success(); } } return $this->error(); } catch (\Exception $exception) { return $this->error(); } } /** * 解析订单号tag * @param $no * @return mixed * User: Mead */ protected function getTypeTag($no) { preg_match('/[A-Z]*/', $no, $types); return $types[0]; } /** * 检查订单是否支付成功 * @param Request $request * @return mixed * User: Mead */ public function checkOrderPayStatus(Request $request) { try { $order_no = $request->get('order_no'); $type = substr($order_no, 0, 1); $class = $this->byTypeGetModel($type); $model = new $class; $pay_status = $model->where('no', $order_no)->value('pay_status'); return $this->response->array([ 'is_pay' => !!$pay_status ]); } catch (\Exception $exception) { Log::error($exception->getMessage()); } } /** * 处理租车订单支付结果 * User: Mead */ public function rentNotify() { try { $payment = app('wechat.payment'); return $payment->handlePaidNotify(function ($message, $fail) { $type = isset($message['attach']) ? $message['attach'] : $this->getTypeTag($message['out_trade_no']); $rentOrder = RentOrder::query(); if ($type === RentOrder::NO_TAG) { $order = $rentOrder->where('no', $message['out_trade_no'])->first(); if (!$order || (int)$order->pay_status === RentOrder::PAY_STATUS_OK) { // 如果订单不存在 或者 订单已经支付过了 return true; // 告诉微信,我已经处理完了,订单没找到,别再通知我了 } if ($message['result_code'] === 'FAIL') { Log::warning('[wechat-pay]:' . json_encode($message, true)); return true; } else if ($message['return_code'] === 'SUCCESS') { // TODO: 你的发货逻辑 $order->pay_status = RentOrder::PAY_STATUS_OK; $order->pay_time = now(); $order->pay_type = RentOrder::PAY_TYPE_WECHAT; $order->pay_money = bcdiv($message['total_fee'], 100, 2); $order->order_total_money = $order->pay_money; $order->status = RentOrder::STATUS_COMPLETE_ORDER; $order->save(); $order->pay_rent_over_order_callback(); return true; } } // // if ($type === RentOrder::NO_OVER_TAG) { // $order = $rentOrder->where('over_no', $message['out_trade_no'])->first(); // if (!$order || $order->pay_status === Order::PAY_STATUS_OK) { // 如果订单不存在 或者 订单已经支付过了 // return true; // 告诉微信,我已经处理完了,订单没找到,别再通知我了 // } // // if ($message['result_code'] === 'FAIL') { // Log::warning('[wechat-pay]:' . json_encode($message, true)); // return true; // } else if ($message['return_code'] === 'SUCCESS') { // // TODO: 你的发货逻辑 // // $order->pay_status = RentOrder::PAY_STATUS_OK; // $order->pay_time = now(); // $order->pay_type = RentOrder::PAY_TYPE_WECHAT; // $order->pay_money = bcdiv($message['total_fee'], 100, 2); // $order->order_total_money = $order->pay_money; // $order->status = RentOrder::STATUS_COMPLETE_ORDER; // // $order->save(); // // $order->pay_rent_over_order_callback(); // // return true; // } // } }); } catch (\Exception $exception) { return $this->errorNoValidation($exception->getMessage()); } } }