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

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