Browse Source

下单支付JSAPI,TODO 支付成功通知

master
weigang 6 years ago
parent
commit
b6e4ae364e
  1. 5
      app/Constants/LogLabel.php
  2. 10
      app/Controller/NotifyController.php
  3. 4
      app/Controller/OrderController.php
  4. 37
      app/Controller/PaymentController.php
  5. 2
      app/Exception/Handler/ValidationExceptionHandler.php
  6. 4
      app/Request/OrderOnlineRequest.php
  7. 34
      app/Request/WxminiPayRequest.php
  8. 2
      app/Service/OrderService.php
  9. 3
      composer.json
  10. 11
      config/config.php
  11. 1
      config/routes.php

5
app/Constants/LogLabel.php

@ -21,4 +21,9 @@ class LogLabel extends AbstractConstants
* @Message("Device Speaker Log Label") * @Message("Device Speaker Log Label")
*/ */
const DEVICE_LOG = 'device_log'; const DEVICE_LOG = 'device_log';
/**
* @Message("Pay Notice Log Label")
*/
const PAY_NOTIFY_WXMINI = 'notify_wxmini';
} }

10
app/Controller/NotifyController.php

@ -1,10 +1,16 @@
<?php <?php
namespace App\Controller; namespace App\Controller;
use App\Constants\LogLabel;
class NotifyController extends BaseController class NotifyController extends BaseController
{ {
public function wxmini()
{
$this->log->event(
LogLabel::PAY_NOTIFY_WXMINI,
json_encode($this->request->input())
);
}
} }

4
app/Controller/OrderController.php

@ -20,8 +20,8 @@ class OrderController extends BaseController
{ {
$orderMainId = $this->orderService->addOnlineOrder($request->validated()); $orderMainId = $this->orderService->addOnlineOrder($request->validated());
if (!is_int($orderMainId)) { if (!is_int($orderMainId)) {
return $this->w7result(500, $orderMainId);
return $this->w7result(500, (string)$orderMainId);
} }
return $this->w7result(200, $orderMainId);
return $this->w7result(200, (string)$orderMainId);
} }
} }

37
app/Controller/PaymentController.php

@ -1,14 +1,45 @@
<?php <?php
namespace App\Controller; namespace App\Controller;
use App\Model\OrderMain;
use App\Model\Users;
use App\Request\WxminiPayRequest;
use EasyWeChat\Factory;
use Hyperf\Guzzle\CoroutineHandler;
class PaymentController extends BaseController class PaymentController extends BaseController
{ {
public function wxminiPay(){
public function wxminiPay(WxminiPayRequest $request){
$data = $request->validated();
$config = config('wxpay');
$app = Factory::payment($config);
$app['guzzle_handler'] = CoroutineHandler::class;
// 待支付的,类型一致的,未超时(15min,900sec)的订单
$orderMain = OrderMain::query()
->where(['state' => OrderMain::ORDER_STATE_UNPAY, 'type' => $data['type'], 'id' => $data['order_id']])
->where('time', '<=', date('Y-m-d H:i:s', (time()-900)))
->first();
if (empty($orderMain)) {
return $this->result(200, ['order_id' => $data['order_id']],'订单不存在或已失效');
}
$result = $app->order->unify([
'body' => '懒族生活 - 外卖下单',
'out_trade_no' => $orderMain->global_order_id,
'total_fee' => bcmul(floatval($orderMain->money), 100),
'notify_url' => config('site_host') . '/v1/notify/wxmini',
'trade_type' => 'JSAPI',
'openid' => $data['openid'],
]);
return $this->success($result);
} }
} }

2
app/Exception/Handler/ValidationExceptionHandler.php

@ -24,7 +24,7 @@ class ValidationExceptionHandler extends ExceptionHandler
]); ]);
return $response->withHeader('Content-Type', 'application/json') return $response->withHeader('Content-Type', 'application/json')
->withStatus($throwable->status)
->withStatus(200)
->withBody(new SwooleStream($content)); ->withBody(new SwooleStream($content));
} }

4
app/Request/OrderOnlineRequest.php

@ -4,9 +4,7 @@ declare(strict_types=1);
namespace App\Request; namespace App\Request;
use Hyperf\Validation\Request\FormRequest;
class OrderOnlineRequest extends FormRequest
class OrderOnlineRequest extends BaseFormRequest
{ {
/** /**
* Determine if the user is authorized to make this request. * Determine if the user is authorized to make this request.

34
app/Request/WxminiPayRequest.php

@ -14,45 +14,25 @@ class WxminiPayRequest extends BaseFormRequest
public function rules(): array public function rules(): array
{ {
return [ return [
'c_attitude' => 'required|nonempty|integer',
'c_service' => 'required|nonempty|integer',
'c_quality' => 'required|nonempty|integer',
'content' => 'required|nonempty|strlen:15,150',
'user_id' => 'required|nonempty|integer|exists:ims_cjdc_user,id',
'service_personnel_id' => 'required|nonempty|integer|exists_enable:lanzu_service_personnel,id,status=1|not_equal:user_id,lanzu_service_personnel,id,user_id',
'market_id' => 'required|nonempty|integer|exists:ims_cjdc_market,id',
'order_id' => 'required|nonempty|integer',
'type' => 'required|nonempty|integer',
'openid' => 'required|nonempty',
]; ];
} }
public function messages(): array public function messages(): array
{ {
return [ return [
'user_id.exists' => ':attribute不存在',
'service_personnel_id.exists' => ':attribute不存在',
'market_id.exists' => ':attribute不存在',
'user_id.*' => ':attribute信息不正确',
'service_personnel_id.exists_enable' => ':attribute不存在或被禁用',
'service_personnel_id.not_equal' => ':attribute不能是自己',
'service_personnel_id.*' => ':attribute信息不正确',
'market_id.*' => ':attribute信息不正确',
'c_attitude.*' => ':attribute信息不正确',
'c_service.*' => ':attribute信息不正确',
'c_quality.*' => ':attribute信息不正确',
'content.strlen' => ':attribute字数限制在:min~:max字',
'content.*' => ':attribute信息不正确',
'*.*' => ':attribute 参数异常',
]; ];
} }
public function attributes(): array public function attributes(): array
{ {
return [ return [
'user_id' => '用户',
'service_personnel_id' => '服务专员',
'market_id' => '服务专员市场',
'c_attitude' => '服务态度评分',
'c_service' => '服务值评分',
'c_quality' => '服务质量评分',
'content' => '服务评价内容',
'order_id' => '订单',
'type' => '订单类型',
'openid' => '用户标识',
]; ];
} }
} }

2
app/Service/OrderService.php

@ -72,7 +72,7 @@ class OrderService implements OrderServiceInterface
$currentTime = time(); $currentTime = time();
$dataMain['time'] = date('Y-m-d H:i:s', $currentTime); $dataMain['time'] = date('Y-m-d H:i:s', $currentTime);
$dataMain['time_add'] = $currentTime; $dataMain['time_add'] = $currentTime;
$dataMain['pay_time'] = date('Y-m-d H:i:s', $currentTime);
$dataMain['pay_time'] = '';
$dataMain['state'] = OrderMain::ORDER_STATE_UNPAY; $dataMain['state'] = OrderMain::ORDER_STATE_UNPAY;
$dataMain['code'] = $dataMain['global_order_id']; $dataMain['code'] = $dataMain['global_order_id'];
$dataMain['jj_note'] = ''; $dataMain['jj_note'] = '';

3
composer.json

@ -35,7 +35,8 @@
"alibabacloud/iot": "^1.8", "alibabacloud/iot": "^1.8",
"hyperf/amqp": "^2.0", "hyperf/amqp": "^2.0",
"hyperf/snowflake": "^2.0", "hyperf/snowflake": "^2.0",
"ext-bcmath": "*"
"ext-bcmath": "*",
"overtrue/wechat": "~4.0"
}, },
"require-dev": { "require-dev": {
"swoole/ide-helper": "^4.5", "swoole/ide-helper": "^4.5",

11
config/config.php

@ -28,7 +28,14 @@ return [
LogLevel::WARNING, LogLevel::WARNING,
], ],
], ],
'site_host'=>'http://store.api.lanzulive.com/'
'site_host'=> env('SITE_HOST', ''),
'wxpay' => [
'app_id' => env('APP_ID',''),
'mch_id' => env('MCH_ID',''),
'key' => env('APP_SECRET',''),
'cert_path' => env('CERT_PATH',''),
'key_path' => env('KEY_PATH',''),
'notify_url' => env('NOTIFY_URL',''),
]
]; ];

1
config/routes.php

@ -42,5 +42,6 @@ Router::addGroup('/v1/',function (){
//小程序支付相关 //小程序支付相关
Router::post('wxmini/pay', 'App\Controller\PaymentController@wxminiPay'); Router::post('wxmini/pay', 'App\Controller\PaymentController@wxminiPay');
Router::post('notify/wxmini', 'App\Controller\NotifyController@wxmini');
}); });
Loading…
Cancel
Save