123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- <?php
-
- namespace App\Http\Controllers;
-
- use Illuminate\Http\Request;
- use Carbon\Carbon;
-
-
- class PaymentController extends Controller
- {
- /**
- * 生成支付订单
- * @param Request $request
- * @return \Illuminate\Http\JsonResponse
- */
- public function buildOrder(Request $request)
- {
-
- $wechat_id = $request->header("openid");
- if (!$wechat_id) {
- $wechat_id = $request->input("openid");
- }
- $order_id = $request->input("order_id");
- $order_type = $request->input("order_type");
- $order = CommonController::getRecord($order_type, $order_id);
-
- // 先判断微信交易是否失效,失效的话需要重新生成份新的系统内部订单,再重新统一下单
- if ($wechat_id && $order) {
- // 判断微信交易是否失效
- if(Carbon::now()->subHour()->gt($order->created_at)) {
- $order_new = clone $order;
- $order_new->id = null;
- $order_new->exists = false;
- $order_new->save();
- $order->delete();
- $order_id = $order_new->id;
- }
-
-
- $out_trade_no = $order_type . "-" . $order_id;
- $app = app('wechat.payment');
-
- $result = $app->order->unify([
- 'body' => '玥子轩家政',
- 'out_trade_no' => $out_trade_no,
- 'total_fee' => $order->price * 100,
- 'trade_type' => 'JSAPI',
- 'openid' => $wechat_id,
- 'notify_url' => 'https://wechat.oskey.cn/api/payments/wechat-notify',
- 'time_expire' => Carbon::now()->addHour()->format('YmdHis')
- ]);
-
- if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
- //第二次签名
- //$result = $app->jssdk->appConfig($result['prepay_id']);
- //$config = $app->configForAppPayment($result['prepay_id']);
- $config = $app->jssdk->sdkConfig($result['prepay_id']);
- return response()->json([
- "status" => 0,
- "code" => "success",
- "msg" => $config,
- "out_trade_no" => $out_trade_no
- ]);
- } else {
- return response()->json([
- "status" => -1,
- "message" => '微信支付签名失败:' . var_export($result, 1)
- ]);
- }
-
- }
- }
-
- /**
- * 支付成功回调函数
- * @param Request $request
- * @return mixed
- */
- public function notify(Request $request)
- {
- $app = app('wechat.payment');
- $response = $app->handlePaidNotify(function($message, $fail){
- // 使用通知里的 "微信支付订单号" 或者 "商户订单号" 去自己的数据库找到订单
- $arrays = preg_split("/-/", $message['out_trade_no']);
- $order_type = $arrays[0];
- $order_id = $arrays[1];
- $order = CommonController::getRecord($order_type, $order_id);
-
- if (!$order || $order->paid_at) { // 如果订单不存在 或者 订单已经支付过了
- return true; // 告诉微信,我已经处理完了,订单没找到,别再通知我了
- }
-
- ///////////// <- 建议在这里调用微信的【订单查询】接口查一下该笔订单的情况,确认是已经支付 /////////////
-
- /*$app = app('wechat.payment');
- $order_result = $app->order->queryByOutTradeNumber($message['out_trade_no']);
- if($order_result && $order_result['return_code'] === 'SUCCESS') {
- // 如果支付成功
- if($order_result["trade_state"] != "SUCCESS") {
- $order->status = -1;
- } else {
-
- }
- }*/
-
- if ($message['return_code'] === 'SUCCESS') { // return_code 表示通信状态,不代表支付状态
- // 用户是否支付成功
- if (array_get($message, 'result_code') === 'SUCCESS') {
- $order->paid_at = Carbon::now(); // 更新支付时间为当前时间
- $order->status = 2;
-
- // 用户支付失败
- } elseif (array_get($message, 'result_code') === 'FAIL') {
- $order->status = -1;
- }
- } else {
- return $fail('通信失败,请稍后再通知我');
- }
-
- $order->save(); // 保存订单
-
- return true; // 返回处理完成
- });
- return $response;
- }
- }
|