家政小程序
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

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