家政小程序
Ви не можете вибрати більше 25 тем Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PaymentController.php 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. class PaymentController extends Controller
  5. {
  6. /**
  7. * 生成支付订单
  8. * @param Request $request
  9. * @return \Illuminate\Http\JsonResponse
  10. */
  11. public function buildOrder(Request $request)
  12. {
  13. $wechat_id = $request->header("openid");
  14. if (!$wechat_id) {
  15. $wechat_id = $request->input("openid");
  16. }
  17. $order_id = $request->input("order_id");
  18. $order_type = $request->input("order_type");
  19. $order = CommonController::getRecord($order_type, $order_id);
  20. if ($wechat_id && $order) {
  21. $order_id = $order_type . "_" . $order_id;
  22. $app = EasyWeChat::payment();
  23. $result = $app->order->unify([
  24. 'body' => '玥子轩家政',
  25. 'out_trade_no' => $order_id,
  26. 'total_fee' => $order->price,
  27. 'trade_type' => 'JSAPI',
  28. 'openid' => $wechat_id,
  29. ]);
  30. if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
  31. //第二次签名
  32. $result = $app->jssdk->appConfig($result['prepay_id']);
  33. return response()->json([
  34. "status" => 0,
  35. "code" => "success",
  36. "msg" => $result
  37. ]);
  38. } else {
  39. return response()->json([
  40. "status" => -1,
  41. "message" => '微信支付签名失败:' . var_export($result, 1)
  42. ]);
  43. }
  44. }
  45. }
  46. /**
  47. * 支付成功回调函数
  48. * @param Request $request
  49. * @return mixed
  50. */
  51. public function notify(Request $request)
  52. {
  53. $app = app('wechat.payment');
  54. $response = $app->handlePaidNotify(function($message, $fail){
  55. // 使用通知里的 "微信支付订单号" 或者 "商户订单号" 去自己的数据库找到订单
  56. list($order_type, $order_id) = spilt("_", $message['out_trade_no']);
  57. $order = CommonController::getRecord($order_type, $order_id);
  58. if (!$order || $order->paid_at) { // 如果订单不存在 或者 订单已经支付过了
  59. return true; // 告诉微信,我已经处理完了,订单没找到,别再通知我了
  60. }
  61. ///////////// <- 建议在这里调用微信的【订单查询】接口查一下该笔订单的情况,确认是已经支付 /////////////
  62. /*$app = app('wechat.payment');
  63. $order_result = $app->order->queryByOutTradeNumber($message['out_trade_no']);
  64. if($order_result && $order_result['return_code'] === 'SUCCESS') {
  65. // 如果支付成功
  66. if($order_result["trade_state"] != "SUCCESS") {
  67. $order->status = -1;
  68. } else {
  69. }
  70. }*/
  71. if ($message['return_code'] === 'SUCCESS') { // return_code 表示通信状态,不代表支付状态
  72. // 用户是否支付成功
  73. if (array_get($message, 'result_code') === 'SUCCESS') {
  74. $order->paid_at = time(); // 更新支付时间为当前时间
  75. $order->status = 2;
  76. // 用户支付失败
  77. } elseif (array_get($message, 'result_code') === 'FAIL') {
  78. $order->status = -1;
  79. }
  80. } else {
  81. return $fail('通信失败,请稍后再通知我');
  82. }
  83. $order->save(); // 保存订单
  84. return true; // 返回处理完成
  85. });
  86. return $response;
  87. }
  88. }