Jsapi支付
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
<?php
|
||||
|
||||
use addons\epay\library\Service;
|
||||
use think\Log;
|
||||
|
||||
class WxPay
|
||||
{
|
||||
@@ -475,4 +477,97 @@ class WxPay
|
||||
}
|
||||
return $output;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 获取支付参数 (统一下单)
|
||||
* @ApiMethod (POST)
|
||||
* @ApiRoute (/api/pay/create)
|
||||
*/
|
||||
public function WxPayMp($d)
|
||||
{
|
||||
$total_fee = abs(floatval($d['money'])) * 100;// 微信支付 单位为分
|
||||
// 1. 接收参数
|
||||
|
||||
$order_no = $d['order_sn'];
|
||||
$amount = $total_fee;
|
||||
$openid = $d['openid'];
|
||||
$pay_type = 'wechat'; // wechat 或 alipay
|
||||
|
||||
if (!$order_no || !$amount || !$openid) {
|
||||
$this->error('参数不完整');
|
||||
}
|
||||
|
||||
// 3. 生成支付参数
|
||||
try {
|
||||
$params = [
|
||||
'amount' => sprintf('%.2f', $amount), // 单位:元
|
||||
'orderid' => $order_no,
|
||||
'type' => $pay_type,
|
||||
'title' => '商品支付-' . $order_no,
|
||||
'notifyurl' => url('/api/pay/notify', '', true, true), // 异步通知
|
||||
'returnurl' => url('/index/index', '', true, true), // 支付后跳转
|
||||
'method' => 'mp', // mp=公众号支付, miniapp=小程序支付
|
||||
'openid' => $openid, // 必须
|
||||
];
|
||||
|
||||
// 4. 调用 epay 插件统一下单
|
||||
$result = Service::submitOrder($params);
|
||||
|
||||
// 5. 返回前端所需参数
|
||||
$this->success('成功', [
|
||||
'order_no' => $order_no,
|
||||
'pay_params' => $result, // 插件返回的支付参数
|
||||
'timestamp' => time(),
|
||||
]);
|
||||
|
||||
} catch (\Exception $e) {
|
||||
Log::error('支付下单失败: ' . $e->getMessage());
|
||||
$this->error('支付创建失败: ' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 微信支付Jsapa
|
||||
* @param string $data 业务参数 body out_trade_no total_fee
|
||||
* @param string $data ['out_trade_no'] 订单号 必填
|
||||
* @param string $data ['total_fee'] 订单金额 必填
|
||||
* @param string $data ['body'] 订单详情 必填
|
||||
* @return $response 返回app所需字符串
|
||||
*/
|
||||
public function WxPayJsapi($d) {
|
||||
$total_fee = abs(floatval($d['money'])) * 100;// 微信支付 单位为分
|
||||
|
||||
$ip = $this->get_client_ip();
|
||||
if ($ip == '::1')
|
||||
$ip = '1.1.1.1';
|
||||
|
||||
$data ["appid"] = \think\Env::get('wechatpay.appid');
|
||||
$data ["mch_id"] = \think\Env::get('wechatpay.mch_id');
|
||||
$data ["nonce_str"] = $this->getRandChar(32);
|
||||
$data ["body"] = $d['remarke'];
|
||||
$data ["out_trade_no"] = $d['order_sn'];
|
||||
$data ["total_fee"] = $total_fee;
|
||||
$data ["spbill_create_ip"] = $ip;
|
||||
$data ["notify_url"] = get_system_config_value("web_site")."/api/Payment/notify_wx";
|
||||
$data ["trade_type"] = "JSAPI";
|
||||
$data ["openid"] = $d['openid'];// 必须
|
||||
|
||||
$s = $this->getSign($data);
|
||||
$data ["sign"] = $s;
|
||||
|
||||
$xml = $this->arrayToXml($data);
|
||||
$url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
|
||||
$response = $this->postXmlCurl($xml, $url);
|
||||
$re = $this->xmlstr_to_array($response);
|
||||
|
||||
if ($re ['return_code'] == 'FAIL') {
|
||||
return array(-1, '订单创建失败!'.\think\Env::get('wechatpay.api_v3_key'), $re['return_msg']);
|
||||
}
|
||||
// 二次签名
|
||||
$reapp = $this->getOrder($re['prepay_id']);
|
||||
return $reapp;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user