appid = '';//小程序appid
$this->secret = '';//小程序secret
$this->mch_id = '';//商户id
$this->key = '';//v3key
$this->serial_no = '';//v3序列号
}
public function ffff(){
$rrr= $this->unifiedOrder(1,'oDdl-6TWLTkclAZVNh7U1y7-kX4c',rand(1000,9999));
return json(['code' => 200, 'msg' => '获取成功', 'data' => $rrr]);
}
/**
* 下单方法
* @param $params 下单参数
*/
public function unifiedOrder($product_type, $openid, $phone = '')
{
//订单总价
$total_fee = 79900;
if ($product_type == 1) {
$total_fee = 79900;
}
if ($product_type == 2) {
$total_fee = 99900;
}
$money = $total_fee;
$url = "https://api.mch.weixin.qq.com/v3/pay/transactions/jsapi";
$urlarr = parse_url($url);
$appid = '';
$mchid ='';//商户id
$xlid = '';//秘钥序列号
$data = [];
$time = time();
$data['appid'] = $appid;
$data['mchid'] = $mchid;
$data['description'] = '描述';//商品描述
$data['out_trade_no'] = $phone . '_' . time() . '_xingji';//订单编号
//$data['notify_url'] = $this->request->domain() . "/api/xjmpzl/notify";//回调接口 需根据自己的情况修改
$data['notify_url'] = "/notify";//回调接口 需根据自己的情况修改
//$money = 1;
$data['amount']['total'] = $money;//金额 单位 分
$data['payer']['openid'] = $openid;//用户openID
$data = json_encode($data);
$noncestr = $time;
$key = $this->getSign($data, $urlarr['path'], $noncestr, $time);//签名
$token = sprintf('mchid="%s",serial_no="%s",nonce_str="%s",timestamp="%d",signature="%s"', $mchid, $xlid, $noncestr, $time, $key);//头部信息
$header = [
'Content-Type:' . 'application/json; charset=UTF-8',
'Accept:application/json',
'User-Agent:*/*',
'Authorization: WECHATPAY2-SHA256-RSA2048 ' . $token
];
$ret = $this->curl_post_https($url, $data, $header);
$ret = ltrim($ret, '{"prepay_id":"');
$ret = rtrim($ret, '}"');
//微信支付(小程序)签名
$str = $this->getWechartSign($appid, time(), $noncestr, 'prepay_id=' . $ret);
$arr = [
'appId' => $appid,
'timeStamp' => time() . "",
'package' => 'prepay_id=' . $ret,
'paySign' => $str,
'nonceStr' => $noncestr . ""
];
return $arr;
}
//微信支付签名
public function getSign($data = [], $url, $randstr, $time)
{
$str = "POST" . "\n" . $url . "\n" . $time . "\n" . $randstr . "\n" . $data . "\n";
$key = file_get_contents('/apiclient_key.pem');//在商户平台下载的秘钥
$str = $this->getSha256WithRSA($str, $key);
return $str;
}
//调起支付的签名
public function getWechartSign($appid, $timeStamp, $noncestr, $prepay_id)
{
$str = $appid . "\n" . $timeStamp . "\n" . $noncestr . "\n" . $prepay_id . "\n";
$key = file_get_contents('/apiclient_key.pem');
$str = $this->getSha256WithRSA($str, $key);
return $str;
}
public function getSha256WithRSA($content, $privateKey)
{
$binary_signature = "";
$algo = "SHA256";
openssl_sign($content, $binary_signature, $privateKey, $algo);
$sign = base64_encode($binary_signature);
return $sign;
}
/* PHP CURL HTTPS POST */
public function curl_post_https($url, $data, $header)
{
$action = curl_init();
curl_setopt($action, CURLOPT_URL, $url);
curl_setopt($action, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($action, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($action, CURLOPT_HEADER, 0);
curl_setopt($action, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($action, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($action, CURLOPT_POST, 1);
curl_setopt($action, CURLOPT_POSTFIELDS, $data);
curl_setopt($action, CURLOPT_HTTPHEADER, $header);
$result = curl_exec($action);
curl_close($action);
return $result;
}
}
本文共 个字数,平均阅读时长 ≈ 分钟,您已阅读:0时0分0秒。
649494848