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.
 
 

113 lines
3.6 KiB

<?php
namespace App\Controller;
use App\Constants\ErrorCode;
use App\Model\OrderMain;
use App\Request\WxminiPayRequest;
use EasyWeChat\Factory;
use Hyperf\Guzzle\CoroutineHandler;
class PaymentController extends BaseController
{
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,'订单不存在或已失效');
}
$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') . '/v1/notify/wxminiOnline',
'trade_type' => 'JSAPI',
'openid' => $data['openid'],
]);
$parameters = [
'appId' => $result['appid'],
'timeStamp' => '' . time() . '',
'nonceStr' => uniqid(),
'package' => 'prepay_id=' . $result['prepay_id'],
'signType' => 'MD5'
];
//签名步骤一:按字典序排序参数
ksort($parameters);
$string = $this->formatBizQueryParaMap($parameters, false);
//签名步骤二:在string后加入KEY
$string = $string . "&key=" . $config['key'];
//签名步骤三:MD5加密
$string = md5($string);
//签名步骤四:所有字符转为大写
$parameters['paySign'] = strtoupper($string);
return $this->success($parameters);
}
///作用:格式化参数,签名过程需要使用
private function formatBizQueryParaMap($paraMap, $urlencode) {
$buff = "";
ksort($paraMap);
foreach ($paraMap as $k => $v) {
if ($urlencode) {
$v = urlencode($v);
}
$buff .= $k . "=" . $v . "&";
}
$reqPar = null;
if (strlen($buff) > 0) {
$reqPar = substr($buff, 0, strlen($buff) - 1);
}
return $reqPar;
}
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']])
->where('time', '>=', date('Y-m-d H:i:s', (time()-900)))
->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') . '/v1/notify/wxminiOffline',
'trade_type' => 'JSAPI',
'openid' => $data['openid'],
]);
$result['sign_type'] = 'MD5';
$result['timestamp'] = time();
return $this->success($result);
}
}