123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Mead
- * Date: 2019/8/26
- * Time: 11:19 AM
- */
- namespace App\Http\Controllers\V1;
- use App\Models\CardRidingOrder;
- use App\Models\DepositOrder;
- use App\Models\Order;
- use App\Models\RechargeOrder;
- use App\Models\RefundLog;
- use App\Models\RentOrder;
- use App\Models\User;
- use Dingo\Api\Http\Request;
- use Illuminate\Support\Facades\Log;
- /**
- * 支付回调模块
- * Class PaymentController
- * @package App\Http\Controllers\V1
- */
- class PaymentController extends BaseController
- {
- /**
- * 支付回调
- * @return mixed
- * User: Mead
- */
- public function notify()
- {
- try {
- $payment = app('wechat.payment');
- return $payment->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());
- }
- }
- }
|