家政小程序
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

PaymentController.php 4.0KB

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