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.
161 lines
4.7 KiB
161 lines
4.7 KiB
<?php
|
|
|
|
namespace App\Controller;
|
|
|
|
use App\Constants\ErrorCode;
|
|
use App\Model\Order;
|
|
use App\Model\OrderGoods;
|
|
use App\Model\OrderMain;
|
|
use App\Request\WxminiPayRequest;
|
|
|
|
use App\Service\PurchaseLimitServiceInterface;
|
|
use EasyWeChat\Factory;
|
|
use Hyperf\DbConnection\Db;
|
|
use Hyperf\Guzzle\CoroutineHandler;
|
|
|
|
class PaymentController extends BaseController
|
|
{
|
|
/**
|
|
* @Inject
|
|
* @var PurchaseLimitServiceInterface
|
|
*/
|
|
protected $purchaseLimitService;
|
|
|
|
public function wxminiPayOnline(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, 'id' => $data['order_id']])
|
|
->where('time', '>=', date('Y-m-d H:i:s', (time()-900)))
|
|
->first();
|
|
|
|
if (empty($orderMain)) {
|
|
return $this->result(ErrorCode::PAY_FAILURE, $data,'订单不存在或已失效');
|
|
}
|
|
|
|
//查询订单商品信息
|
|
$order = Order::query()
|
|
->where('order_main_id',$data['order_id'])
|
|
->select('id')
|
|
->get()
|
|
->toArray();
|
|
$orderGoods = OrderGoods::query()
|
|
->whereIn('order_id',$order)
|
|
->get();
|
|
//判断是否有购买多个特价商品
|
|
$result = $this->purchaseLimitService->PurchaseLimit($orderGoods);
|
|
if(!$result){
|
|
Db::rollBack();
|
|
return '同一个订单不能购买多个特价商品';
|
|
}
|
|
|
|
$result = $app->order->unify([
|
|
'body' => '懒族生活 - 外卖下单',
|
|
'out_trade_no' => $orderMain->global_order_id,
|
|
// 'total_fee' => bcmul(floatval($orderMain->money), 100, 0),
|
|
'total_fee' => 1,
|
|
'notify_url' => config('site_host') . '/wechat/notify/wxminionline',
|
|
'trade_type' => 'JSAPI',
|
|
'openid' => $data['openid'],
|
|
]);
|
|
|
|
// 返回支付参数给前端
|
|
$parameters = [
|
|
'appId' => $result['appid'],
|
|
'timeStamp' => '' . time() . '',
|
|
'nonceStr' => uniqid(),
|
|
'package' => 'prepay_id=' . $result['prepay_id'],
|
|
'signType' => 'MD5'
|
|
];
|
|
|
|
$parameters['paySign'] = $this->signture($parameters, $config['key']);
|
|
|
|
return $this->success($parameters);
|
|
}
|
|
|
|
public function wxminiPayOffline(WxminiPayRequest $request){
|
|
|
|
$data = $request->validated();
|
|
|
|
$config = config('wxpay');
|
|
$app = Factory::payment($config);
|
|
$app['guzzle_handler'] = CoroutineHandler::class;
|
|
|
|
// 待支付的,未超时(15min,900sec)的订单
|
|
$orderMain = OrderMain::query()
|
|
->where(['dm_state' => OrderMain::ORDER_STATE_UNPAY, 'id' => $data['order_id']])
|
|
->first();
|
|
|
|
if (empty($orderMain)) {
|
|
return $this->result(ErrorCode::PAY_FAILURE, $data,'订单不存在或已失效');
|
|
}
|
|
|
|
$result = $app->order->unify([
|
|
'body' => '懒族生活 - 当面支付',
|
|
'out_trade_no' => $orderMain->global_order_id,
|
|
'total_fee' => bcmul(floatval($orderMain->money), 100, 0),
|
|
'notify_url' => config('site_host') . '/wechat/notify/wxminioffline',
|
|
'trade_type' => 'JSAPI',
|
|
'openid' => $data['openid'],
|
|
]);
|
|
|
|
// 返回支付参数给前端
|
|
$parameters = [
|
|
'appId' => $result['appid'],
|
|
'timeStamp' => '' . time() . '',
|
|
'nonceStr' => uniqid(),
|
|
'package' => 'prepay_id=' . $result['prepay_id'],
|
|
'signType' => 'MD5'
|
|
];
|
|
|
|
$parameters['paySign'] = $this->signture($parameters, $config['key']);
|
|
|
|
return $this->success($parameters);
|
|
}
|
|
|
|
/**
|
|
* 支付参数加签
|
|
* @param $parameters
|
|
* @param $key
|
|
* @return string
|
|
*/
|
|
private function signture($parameters, $key)
|
|
{
|
|
// 按字典序排序参数
|
|
ksort($parameters);
|
|
|
|
// http_query
|
|
$queryParams = $this->http_query($parameters);
|
|
|
|
// 加入KEY
|
|
$queryParams = $queryParams . "&key=" . $key;
|
|
|
|
// MD5加密
|
|
$queryParams = md5($queryParams);
|
|
|
|
// 字符转为大写
|
|
return strtoupper($queryParams);
|
|
}
|
|
|
|
/**
|
|
* 参数转为http query字串
|
|
* @param $parameters
|
|
* @return string
|
|
*/
|
|
private function http_query($parameters) {
|
|
|
|
$http_query = [];
|
|
foreach ($parameters as $key => $value) {
|
|
$http_query[] = $key.'='.$value;
|
|
}
|
|
|
|
return implode('&', $http_query);
|
|
}
|
|
|
|
}
|