PaymentController.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Mead
  5. * Date: 2019/8/26
  6. * Time: 11:19 AM
  7. */
  8. namespace App\Http\Controllers\V1;
  9. use App\Models\CardRidingOrder;
  10. use App\Models\DepositCardOrder;
  11. use App\Models\DepositOrder;
  12. use App\Models\Order;
  13. use App\Models\PunishmentOrder;
  14. use App\Models\RechargeOrder;
  15. use App\Models\RefundLog;
  16. use App\Models\RentOrder;
  17. use App\Models\User;
  18. use Carbon\Carbon;
  19. use Dingo\Api\Http\Request;
  20. use Illuminate\Support\Facades\Log;
  21. /**
  22. * 支付回调模块
  23. * Class PaymentController
  24. * @package App\Http\Controllers\V1
  25. */
  26. class PaymentController extends BaseController
  27. {
  28. /**
  29. * 支付回调
  30. * @return mixed
  31. * User: Mead
  32. */
  33. public function notify()
  34. {
  35. try {
  36. $payment = app('wechat.payment');
  37. return $payment->handlePaidNotify(function ($message, $fail) {
  38. $type = isset($message['attach']) ? $message['attach'] : $this->getTypeTag($message['out_trade_no']);
  39. $class = $this->byTypeGetModel($type);
  40. $model = new $class;
  41. $order = $model->where('no', $message['out_trade_no'])->first();
  42. if (!$order) {
  43. return false;
  44. }
  45. if ((int)$order->pay_status === Order::PAY_STATUS_OK) { // 如果订单不存在 或者 订单已经支付过了
  46. return true; // 告诉微信,我已经处理完了,订单没找到,别再通知我了
  47. }
  48. if ($message['result_code'] === 'FAIL') {
  49. Log::warning('[wechat-pay]:' . json_encode($message, true));
  50. return true;
  51. } else if ($message['return_code'] === 'SUCCESS') {
  52. // TODO: 你的发货逻辑
  53. $order->pay_status = Order::PAY_STATUS_OK;
  54. $order->pay_time = now();
  55. $order->pay_type = Order::PAY_TYPE_WECHAT;
  56. $order->pay_money = bcdiv($message['total_fee'], 100, 2);
  57. $order->save();
  58. $order->pay_order_callback();
  59. return true;
  60. }
  61. });
  62. } catch (\Exception $exception) {
  63. Log::error($exception->getMessage());
  64. }
  65. }
  66. /**
  67. * 退款结果通知回调
  68. * @return mixed
  69. * User: Mead
  70. */
  71. public function refundNotify()
  72. {
  73. try {
  74. $payment = app('wechat.payment');
  75. return $payment->handleRefundedNotify(function ($message, $reqInfo, $fail) {
  76. if ($message['return_code'] === 'FAIL') {
  77. Log::warning('[wechat-pay-refund]:' . json_encode($message, true));
  78. return true;
  79. }
  80. $no = $reqInfo['out_trade_no'];
  81. $type = $this->getTypeTag($no);
  82. Log::info("refund::{$type}==>{$no}");
  83. $class = $this->byTypeGetModel($type);
  84. if (empty($class)) {
  85. //其他项目
  86. Log::info("no_Tag:" . $no);
  87. // $this->sendOtherRefundOrder($reqInfo['out_trade_no'], 1);
  88. return false;
  89. }
  90. $model = new $class;
  91. $order = $model->where('no', $no)->first();
  92. if (!$order) {
  93. return false;
  94. }
  95. $is_ok = ($message['return_code'] == 'SUCCESS' && $reqInfo['refund_status'] == 'SUCCESS');
  96. if (!$is_ok) {
  97. Log::error($reqInfo);
  98. return true;
  99. }
  100. //除押金之外退款操作
  101. if (!in_array($type, [makeNoTag(DepositOrder::NO_TAG), makeNoTag(RefundLog::NO_TAG)])) {
  102. if ($is_ok) {
  103. $order->refund_order_callback();
  104. }
  105. return true;
  106. }
  107. if ((int)$order->is_refund === DepositOrder::REFUND_OK) {
  108. return true;
  109. }
  110. if ($is_ok) {
  111. $order->is_refund = DepositOrder::REFUND_OK;
  112. $order->save();
  113. $order->refund_order_callback();
  114. }
  115. return true; // 返回 true 告诉微信“我已处理完成”
  116. });
  117. } catch (\Exception $exception) {
  118. Log::error($exception->getMessage());
  119. }
  120. }
  121. /**
  122. * 根据订单号实例model
  123. * @param $type
  124. * @return string
  125. * User: Mead
  126. */
  127. protected function byTypeGetModel($type)
  128. {
  129. $class = '';
  130. switch ($type) {
  131. case config('bike.no_tag') . DepositOrder::NO_TAG:
  132. // 缴纳押金
  133. $class = DepositOrder::class;
  134. break;
  135. case config('bike.no_tag') . RechargeOrder::NO_TAG:
  136. // 充值
  137. $class = RechargeOrder::class;
  138. break;
  139. case config('bike.no_tag') . Order::NO_TAG:
  140. // 骑行订单
  141. $class = Order::class;
  142. break;
  143. case config('bike.no_tag') . RefundLog::NO_TAG:
  144. // 退款
  145. $class = RefundLog::class;
  146. break;
  147. case config('bike.no_tag') . CardRidingOrder::NO_TAG:
  148. // 骑行卡
  149. $class = CardRidingOrder::class;
  150. break;
  151. case config('bike.no_tag') . DepositCardOrder::NO_TAG:
  152. // 免押金卡
  153. $class = DepositCardOrder::class;
  154. break;
  155. case config('bike.no_tag') . PunishmentOrder::NO_TAG:
  156. // 罚单
  157. $class = PunishmentOrder::class;
  158. break;
  159. }
  160. return $class;
  161. }
  162. /**
  163. * 发送给小斑马
  164. * @param $no
  165. * @param int $is_order_no
  166. * @return bool
  167. * User: Mead
  168. */
  169. public function sendOtherRefundOrder($no, $is_order_no = 0)
  170. {
  171. $re = curl_get("http://api.xbm.weilaibike.com/api/payments/wechat-refund-api?no={$no}&is-order-no={$is_order_no}");
  172. if ($re['status']) {
  173. return true;
  174. }
  175. return false;
  176. }
  177. /**
  178. * 检查订单是否退款成功
  179. * @param Request $request
  180. * @return mixed
  181. * User: Mead
  182. */
  183. public function isOrderRefundPay(Request $request)
  184. {
  185. try {
  186. $no = $request->get('no');
  187. $is_order_no = $request->get('is-order-no', 0);
  188. $type = $this->getTypeTag($no);
  189. if (!in_array($type, [makeNoTag(RefundLog::NO_TAG), makeNoTag(DepositOrder::NO_TAG)])) {
  190. return $this->error('订单号类型不对');
  191. }
  192. $payment = app('wechat.payment');
  193. if ($is_order_no) {
  194. $re = $payment->refund->queryByOutTradeNumber($no);
  195. } else {
  196. $re = $payment->refund->queryByOutRefundNumber($no);
  197. }
  198. Log::log('info', 'isOrderRefundPay', $re);
  199. $is_ok = (($re['return_code'] === 'SUCCESS') && ($re['result_code'] === 'SUCCESS'));
  200. if (!$is_ok) {
  201. $return = [
  202. 'status' => false,
  203. 'code' => $re['err_code'],
  204. 'msg' => $re['err_code_des']
  205. ];
  206. return $return;
  207. }
  208. if ($re['refund_status_0'] !== 'SUCCESS') {
  209. $return = [
  210. 'status' => false,
  211. 'code' => '',
  212. 'msg' => RefundLog::$tradeStateMaps[$re['refund_status_0']]
  213. ];
  214. return $return;
  215. }
  216. $return = [
  217. 'status' => true,
  218. 'msg' => '退款成功'
  219. ];
  220. $order = DepositOrder::where('no', $re['out_trade_no'])->first();
  221. if ((int)$order->is_refund === DepositOrder::REFUND_OK) {
  222. return $return;
  223. }
  224. if ($is_ok) {
  225. switch ($re['refund_status_0']) {
  226. case 'SUCCESS':
  227. //退款成功
  228. $order->is_refund = DepositOrder::REFUND_OK;
  229. $order->save();
  230. $order->refund_order_callback();
  231. break;
  232. case 'REFUNDCLOSE':
  233. //退款关闭
  234. $return = [
  235. 'status' => false,
  236. 'msg' => '退款关闭'
  237. ];
  238. break;
  239. case 'PROCESSING':
  240. //退款处理中
  241. $return = [
  242. 'status' => false,
  243. 'msg' => '退款处理中,请稍后再试。'
  244. ];
  245. break;
  246. case 'CHANGE':
  247. //退款异常
  248. $return = [
  249. 'status' => false,
  250. 'msg' => $re['err_code_des']
  251. ];
  252. Log::error("isOrderRefundPay:", $re);
  253. RefundLog::where('deposit_id', $order->id)->update([
  254. 'pay_status' => RefundLog::PAY_STATUS_ERROR,
  255. 'pay_time' => date('Y-m-d H:i:s'),
  256. 'result' => $re['err_code_des']
  257. ]);
  258. break;
  259. }
  260. } else {
  261. $return = [
  262. 'status' => false,
  263. 'code' => $re['err_code'],
  264. 'msg' => $re['err_code_des']
  265. ];
  266. }
  267. return $return;
  268. } catch (\Exception $exception) {
  269. return $this->error($exception->getMessage());
  270. }
  271. }
  272. /**
  273. * 解析订单号tag
  274. * @param $no
  275. * @return mixed
  276. * User: Mead
  277. */
  278. protected function getTypeTag($no)
  279. {
  280. preg_match('/[A-Z]*/', $no, $types);
  281. return $types[0];
  282. }
  283. /**
  284. * 检查订单是否支付成功
  285. * @param Request $request
  286. * @return mixed
  287. * User: Mead
  288. */
  289. public function checkOrderPayStatus(Request $request)
  290. {
  291. try {
  292. $order_no = $request->get('order_no');
  293. $type = substr($order_no, 0, 1);
  294. $class = $this->byTypeGetModel($type);
  295. $model = new $class;
  296. $pay_status = $model->where('no', $order_no)->value('pay_status');
  297. return $this->response->array([
  298. 'is_pay' => !!$pay_status
  299. ]);
  300. } catch (\Exception $exception) {
  301. Log::error($exception->getMessage());
  302. }
  303. }
  304. /**
  305. * 处理租车订单支付结果
  306. * User: Mead
  307. */
  308. public function rentNotify()
  309. {
  310. try {
  311. $payment = app('wechat.payment');
  312. return $payment->handlePaidNotify(function ($message, $fail) {
  313. $type = isset($message['attach']) ? $message['attach'] : $this->getTypeTag($message['out_trade_no']);
  314. $rentOrder = RentOrder::query();
  315. if ($type === makeNoTag(RentOrder::NO_TAG)) {
  316. $order = $rentOrder->where('no', $message['out_trade_no'])->first();
  317. if (!$order || (int)$order->pay_status === RentOrder::PAY_STATUS_OK) { // 如果订单不存在 或者 订单已经支付过了
  318. return true; // 告诉微信,我已经处理完了,订单没找到,别再通知我了
  319. }
  320. if ($message['result_code'] === 'FAIL') {
  321. Log::warning('[wechat-pay]:' . json_encode($message, true));
  322. return true;
  323. } else if ($message['return_code'] === 'SUCCESS') {
  324. // TODO: 你的发货逻辑
  325. $order->pay_status = RentOrder::PAY_STATUS_OK;
  326. $order->pay_time = now();
  327. $order->pay_type = RentOrder::PAY_TYPE_WECHAT;
  328. $order->pay_money = bcdiv($message['total_fee'], 100, 2);
  329. $order->order_total_money = $order->pay_money;
  330. $order->status = RentOrder::STATUS_COMPLETE_ORDER;
  331. $order->save();
  332. $order->pay_rent_over_order_callback();
  333. return true;
  334. }
  335. }
  336. });
  337. } catch (\Exception $exception) {
  338. return $this->errorNoValidation($exception->getMessage());
  339. }
  340. }
  341. }