家政小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PaymentController.php 3.9KB

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