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

PaymentController.php 4.7KB

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