Silent 6 years ago
parent
commit
40d2a9bedc

+ 50 - 0
app/Http/Controllers/WeChat/ApiController.php

xqd xqd xqd xqd
@@ -7,6 +7,7 @@ use App\Models\Content;
 use App\Models\FormData;
 use App\Models\FormSet;
 use App\Models\Leave;
+use App\Models\Order;
 use App\Models\Remark;
 use App\Models\RemarkDetail;
 use App\Models\RemarkTitle;
@@ -16,6 +17,7 @@ use App\Models\StudentCourse;
 use App\Models\StudentCourseTeacher;
 use App\Models\WeChatUser;
 use Carbon\Carbon;
+use EasyWeChat\Factory;
 use GuzzleHttp\Client;
 use Illuminate\Http\Request;
 use App\Http\Controllers\Controller;
@@ -522,6 +524,8 @@ class ApiController extends Controller
             return response()->json(['status' => 'fail', 'info' => '参数错误']);
         }
 
+        $type = $data['type'];
+
         $items = ['text_1', 'text_2', 'text_3', 'text_4', 'multi_text', 'radio'];
         foreach($items as $item) {
             if(isset($data[$item])) {
@@ -538,6 +542,52 @@ class ApiController extends Controller
         if(empty($res)) {
             return response()->json(['status' => 'fail', 'info' => '保存失败']);
         }
+
+        if($type == 'pay') {
+            return $this->makeOrder($request);
+        }
         return response()->json(['status' => 'success', 'info' => '提交成功']);
     }
+
+    public function makeOrder(Request $request)
+    {
+        if(empty($request->input('money'))) {
+            return response()->json(['status' => 'fail', 'info' => '金额错误']);
+        }
+        if(empty($request->input('id')) || empty($we_chat_user = WeChatUser::find($request->input('id')))) {
+            return response()->json(['status' => 'fail', 'info' => '找不到用户']);
+        }
+//        $student = Student::where('open_id', $we_chat_user->open_id);
+        $app = app('wechat.payment');
+
+        $out_trade_no = (new Order())->getOutTradeNo();
+
+        $result = $app->order->unify([
+            'body' => '钢琴时间',
+            'out_trade_no' => $out_trade_no,
+            'total_fee' => $request->input('money'),
+            'trade_type' => 'JSAPI',
+            'openid' => $we_chat_user->open_id,
+        ]);
+
+        if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS'){
+            $prepayId = $result['prepay_id'];
+            $config = [
+                // 前面的appid什么的也得保留哦
+                'app_id'             => $result['appid'],
+                'mch_id'             => $result['mch_id'],
+                'key'                => env('WECHAT_PAYMENT_KEY', 'key-for-signature'),
+                // 'device_info'     => '013467007045764',
+                // 'sub_app_id'      => '',
+                // 'sub_merchant_id' => '',
+                // ...
+            ];
+            $payment = Factory::payment($config);
+            $jssdk = $payment->jssdk;
+            $json = $jssdk->bridgeConfig($prepayId);
+        } else {
+            return response()->json(['status' => 'fail', 'info' => '支付失败']);
+        }
+        return response()->json(['status' => 'success', 'data' => $json]);
+    }
 }

+ 5 - 0
app/Models/Order.php

xqd
@@ -67,4 +67,9 @@ class Order extends Model
     {
         return '<span class="label label-primary">微信支付</span>';
     }
+
+    public function getOutTradeNo()
+    {
+        return date('YmdHis') . rand(10, 99);
+    }
 }

+ 12 - 12
config/wechat.php

xqd
@@ -104,18 +104,18 @@ return [
     /*
      * 微信支付
      */
-    // 'payment' => [
-    //     'default' => [
-    //         'sandbox'            => env('WECHAT_PAYMENT_SANDBOX', false),
-    //         'app_id'             => env('WECHAT_PAYMENT_APPID', ''),
-    //         'mch_id'             => env('WECHAT_PAYMENT_MCH_ID', 'your-mch-id'),
-    //         'key'                => env('WECHAT_PAYMENT_KEY', 'key-for-signature'),
-    //         'cert_path'          => env('WECHAT_PAYMENT_CERT_PATH', 'path/to/cert/apiclient_cert.pem'),    // XXX: 绝对路径!!!!
-    //         'key_path'           => env('WECHAT_PAYMENT_KEY_PATH', 'path/to/cert/apiclient_key.pem'),      // XXX: 绝对路径!!!!
-    //         'notify_url'         => 'http://example.com/payments/wechat-notify',                           // 默认支付结果通知地址
-    //     ],
-    //     // ...
-    // ],
+     'payment' => [
+         'default' => [
+             'sandbox'            => env('WECHAT_PAYMENT_SANDBOX', false),
+             'app_id'             => env('WECHAT_PAYMENT_APPID', ''),
+             'mch_id'             => env('WECHAT_PAYMENT_MCH_ID', 'your-mch-id'),
+             'key'                => env('WECHAT_PAYMENT_KEY', 'key-for-signature'),
+             'cert_path'          => env('WECHAT_PAYMENT_CERT_PATH', 'path/to/cert/apiclient_cert.pem'),    // XXX: 绝对路径!!!!
+             'key_path'           => env('WECHAT_PAYMENT_KEY_PATH', 'path/to/cert/apiclient_key.pem'),      // XXX: 绝对路径!!!!
+             'notify_url'         => 'http://example.com/payments/wechat-notify',                           // 默认支付结果通知地址
+         ],
+         // ...
+     ],
 
     /*
      * 企业微信

+ 14 - 6
wechat/pages/form/index.js

xqd xqd xqd
@@ -6,7 +6,9 @@ Page({
    * 页面的初始数据
    */
   data: {
-    data: {}
+    data: {
+      formContainerClass: 'mode-1-container'
+    }
   },
 
   /**
@@ -19,8 +21,10 @@ Page({
       method: 'GET',
       success: res => {
         if(res.data.status == 'success') {
+          var formContainerClass = res.data.data.mode == 1 ? 'mode-1-container' : 'mode-2-container'
           that.setData({
-            data: res.data.data
+            data: res.data.data,
+            formContainerClass: formContainerClass
           })
           if(res.data.data.top_title) {
             wx.setNavigationBarTitle({
@@ -101,10 +105,14 @@ Page({
       success: res => {
         console.log(res)
         if (res.data.status == 'success') {
-          wx.showToast({
-            icon: 'none',
-            title: '提交成功',
-          })
+          if(value.type == 'pay') {
+            console.log(res);
+          } else {
+            wx.showToast({
+              icon: 'none',
+              title: '提交成功',
+            })
+          }
         }
       }
     })

+ 1 - 1
wechat/pages/form/index.wxml

xqd
@@ -3,7 +3,7 @@
   <form bindsubmit="formSubmit">
     <image wx:if="{{ data.top_image }}" src='{{ data.top_image }}' class='top-image'></image>
     <view class='top-desp' wx:if="{{ data.top_desp }}">{{ data.top_desp }}</view>
-    <view wx:if="{{ data.mode == 1 }}" class='mode-1-container'>
+    <view class='{{ formContainerClass }}'>
       <view class='form-body'>
         <view wx:if="{{ data.text_1_status == 1 }}" class='form-group'>
           <view class='form-label'>

+ 30 - 0
wechat/pages/form/index.wxss

xqd
@@ -25,4 +25,34 @@
 }
 .mode-1-container .form-btn {
   margin: 10px;
+  padding: 5px;
+}
+.mode-2-container {
+  margin: 10px;
+  overflow: hidden;
+  background-color: white;
+}
+.mode-2-container .form-group {
+  margin: 15px 7px 0 7px;
+}
+.mode-2-container .form-label {
+  display: inline-block;
+  width: 25%;
+  float: left;
+  margin-top: 7px;
+}
+.mode-2-container .form-label text {
+  vertical-align: middle;
+}
+.mode-2-container .form-input {
+  background-color: ghostwhite;
+  padding: 5px;
+  display: inline-block;
+  width: 67%;
+}
+.mode-2-container .need-label {
+  color: red;
+}
+.mode-2-container .form-btn {
+  margin: 10px;
 }