|
分享源码
界面截图: |
- |
是否带模块: |
纯源码 |
备注说明: |
- |
[PHP] 纯文本查看 复制代码
pay.php
<?php
header('Content-type:text/html; Charset=utf-8');
ini_set('date.timezone','Asia/Shanghai');
$meal = isset($_GET['meal']) ? $_GET['meal'] : '001'; // 套餐ID 001月,002半年,003年
$useraccount = isset($_GET['useraccount']) ? $_GET['useraccount'] : 'null'; // user账号
//统一下单
function wechartAddOrder($money,$product,$useraccount){
$url = "https://api.mch.weixin.qq.com/v3/pay/transactions/native";
$urlarr = parse_url($url); //拆解为:[scheme=>https,host=>api.mch.weixin.qq.com,path=>/v3/pay/transactions/native]
$time = time(); //时间戳
$noncestr = $time;
$orderNumber = $useraccount.'-'.$time;
$appid = '';//appID
$mchid = '';//商户ID
$xlid = '';//秘钥序列号
$data = array();
$data['appid'] = $appid;
$data['mchid'] = $mchid;
$data['description'] = $product;//商品描述
$data['out_trade_no'] = $orderNumber;//商家自己的订单编号,订单号在VX支fu里是唯一的
$data['notify_url'] = "notify.php";//需根据自己的情况修改回调接口,也可以为空
$data['amount']['total'] = $money;//金额 单位 分
$data = json_encode($data); //变为json格式
//签名,包含了$data数据、VX指定地址、随机数和时间
$key = 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 = array(
'Content-Type:'.'application/json; charset=UTF-8',
'Accept:application/json',
'User-Agent:*/*',
'Authorization: WECHATPAY2-SHA256-RSA2048 '.$token
);
$ret = curl_post_https($url,$data,$header);
$response = json_decode($ret, true);
$payurl = $response['code_url'];
echo json_encode(array('code' => 200, 'payurl' => $payurl, 'orderNumber' => $orderNumber));;
}
function curl_post_https($url,$data,$header){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); // 从证书中检查SSL加密算法是否存在,如果出错则修改为0,默认为1
curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
curl_setopt($curl, CURLOPT_TIMEOUT, 30);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
$tmpInfo = curl_exec($curl); // 执行操作
if (curl_errno($curl)) {
echo 'Errno'.curl_error($curl);//捕抓异常
}
curl_close($curl);
return $tmpInfo; // 返回数据,json格式
}
//VX支fu签名
function getSign($data=array(),$url,$randstr,$time){
$str = "POST"."\n".$url."\n".$time."\n".$randstr."\n".$data."\n";
$key = file_get_contents('apiclient_key.pem');//在商户平台下载的证书,注意所放路径
$str = getSha256WithRSA($str,$key); //将上传内容与api证书结合加密
return $str;
}
//加密
function getSha256WithRSA($content, $privateKey){
$binary_signature = "";
$algo = "SHA256";
openssl_sign($content, $binary_signature, $privateKey, $algo);
$sign = base64_encode($binary_signature);
return $sign;
}
if($meal == '001'){
$money = 1;
$product = '支fu测试1';
wechartAddOrder($money,$product,$useraccount);
}elseif ($meal == '002') {
$money = 2;
$product = '支fu测试12';
wechartAddOrder($money,$product,$useraccount);
}elseif ($meal == '003') {
$money = 3;
$product = '支fu测试123';
wechartAddOrder($money,$product,$useraccount);
}
notify.php
[PHP] 纯文本查看 复制代码 <?php
$getCallBackJson = file_get_contents('php://input');
$getCallBackArray = json_decode($getCallBackJson, true);
//获取需要解密字段
$associatedData = $getCallBackArray['resource']['associated_data'];
$nonceStr = $getCallBackArray['resource']['nonce'];
$ciphertext = $getCallBackArray['resource']['ciphertext'];
//执行解密
$apiKey = ''; //这里需要填写APIv3秘钥
$getData = new NotifyService($apiKey);
$resultJson = $getData->decryptToString($associatedData, $nonceStr, $ciphertext);
//解密结果,为关联数组格式
$resultArray = json_decode($resultJson, true);
//交易成功
if ($resultArray['trade_state'] === 'SUCCESS') {
//这里填写交易成功的相关业务,如更新账单状态,其中可能需要用到的参数如下
$resultArray['out_trade_no']; //商户订单号
$resultArray['transaction_id']; //VX支fu的订单号
$money = $resultArray['amount']['total']; //订单金额
$parts = explode("-", $resultArray['out_trade_no']);
$acctno = $parts[0];
}
class NotifyService
{
protected $apiKey;
const AUTH_TAG_LENGTH_BYTE = 16;
public function __construct($apiKey)
{
$this->apiKey = $apiKey;
}
public function decryptToString(string $associatedData, string $nonceStr, string $ciphertext)
{
$ciphertext = \base64_decode($ciphertext);
if (strlen($ciphertext) <= self::AUTH_TAG_LENGTH_BYTE) {
return false;
}
$ctext = substr($ciphertext, 0, -self::AUTH_TAG_LENGTH_BYTE);
$authTag = substr($ciphertext, -self::AUTH_TAG_LENGTH_BYTE);
return \openssl_decrypt($ctext, 'aes-256-gcm', $this->apiKey, \OPENSSL_RAW_DATA, $nonceStr,
$authTag, $associatedData);
}
}
?>
下载交易流水的接口
[PHP] 纯文本查看 复制代码 <?php
header('Content-type:text/html; Charset=utf-8');
ini_set('date.timezone','Asia/Shanghai');
function downorderstatus(){
$orderNumber = isset($_GET['orderNumber']) ? $_GET['orderNumber'] : 'NO ourderNumber';
$mchid = '';//商户ID
$xlid = '';//秘钥序列号
$time = time(); //时间戳
$noncestr = $time;
$url = "https://api.mch.weixin.qq.com/v3/bill/tradebill?bill_date=2024-06-28&bill_type=ALL&tar_type=GZIP";
$urlarr = parse_url($url);
$domain = $urlarr['path']."?".$urlarr['query'];
$key = getSign($domain,$noncestr,$time);
$token = sprintf('mchid="%s",nonce_str="%s",signature="%s",timestamp="%d",serial_no="%s"',$mchid,$noncestr,$key,$time,$xlid);
$header = array(
'Content-Type: application/json; charset=UTF-8',
'Accept: application/json',
'User-Agent: */*',
'Authorization: WECHATPAY2-SHA256-RSA2048 '.$token
);
$ret = curl_get_https($url,$header);
$data = json_decode($ret);
$download_url = $data->download_url;
$urlarr = parse_url($download_url);
$domain = $urlarr['path']."?".$urlarr['query'];
$key = getSign($domain,$noncestr,$time);
$token = sprintf('mchid="%s",nonce_str="%s",signature="%s",timestamp="%d",serial_no="%s"',$mchid,$noncestr,$key,$time,$xlid);
$header = array(
'Content-Type: application/json; charset=UTF-8',
'Accept: application/json',
'User-Agent: */*',
'Authorization: WECHATPAY2-SHA256-RSA2048 '.$token
);
$down = curl_get_https($download_url,$header);
$decompressedPdfData = gzdecode($down);
if ($decompressedPdfData !== false) {
header('Content-Type: application/xls');
header('Content-Disposition: attachment; filename="decompressed.xls"');
echo $decompressedPdfData;
exit;
}
echo($decompressedPdfData);
}
function curl_get_https($url,$header){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$response = curl_exec($ch);
if($response !== FALSE){
return $response;
}
curl_close($ch);
}
//VX支fu签名
function getSign($url,$randstr,$time){
$str = "GET"."\n".$url."\n".$time."\n".$randstr."\n"."\n";
$key = file_get_contents('apiclient_key.pem');
$str = getSha256WithRSA($str,$key);
return $str;
}
//加密
function getSha256WithRSA($content, $privateKey){
$binary_signature = "";
$algo = "SHA256";
openssl_sign($content, $binary_signature, $privateKey, $algo);
$sign = base64_encode($binary_signature);
return $sign;
}
downorderstatus();
根据商家的订单号cha询支fu状态
[PHP] 纯文本查看 复制代码 <?php
header('Content-type:text/html; Charset=utf-8');
ini_set('date.timezone','Asia/Shanghai');
function orderstatus(){
$orderNumber = isset($_GET['orderNumber']) ? $_GET['orderNumber'] : 'NO ourderNumber';
$mchid = '';//商户ID
$xlid = '';//秘钥序列号
$time = time(); //时间戳
$noncestr = $time;
$url = "https://api.mch.weixin.qq.com/v3/pay/transactions/out-trade-no/".$orderNumber."?mchid=".$mchid;
$urlarr = parse_url($url);
$domain = $urlarr['path']."?mchid=".$mchid;
$key = getSign($domain,$noncestr,$time);
$token = sprintf('mchid="%s",nonce_str="%s",signature="%s",timestamp="%d",serial_no="%s"',$mchid,$noncestr,$key,$time,$xlid);
$header = array(
'Content-Type: application/json; charset=UTF-8',
'Accept: application/json',
'User-Agent: */*',
'Authorization: WECHATPAY2-SHA256-RSA2048 '.$token
);
$ret = curl_get_https($url,$header);
$data = json_decode($ret);
// 访问对象的属性来获取所需的值
$out_trade_no = $data->out_trade_no;
$trade_state = $data->trade_state;
$trade_state_desc = $data->trade_state_desc;
if($trade_state=='SUCCESS'){
echo json_encode(array('code' => 200, 'orderNumber' => $out_trade_no, 'state' => $trade_state,'tradeState' => $trade_state_desc));
}elseif ($trade_state=='NOTPAY') {
echo json_encode(array('code' => 201, 'orderNumber' => $out_trade_no, 'state' => $trade_state,'tradeState' => $trade_state_desc));
}elseif($trade_state=='CLOSED') {
echo json_encode(array('code' => 202, 'orderNumber' => $out_trade_no, 'state' => $trade_state,'tradeState' => $trade_state_desc));
}
}
function curl_get_https($url,$header){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$response = curl_exec($ch);
if($response !== FALSE){
return $response;
}
curl_close($ch);
}
//VX支fu签名
function getSign($url,$randstr,$time){
$str = "GET"."\n".$url."\n".$time."\n".$randstr."\n"."\n";
$key = file_get_contents('apiclient_key.pem');
$str = getSha256WithRSA($str,$key);
return $str;
}
//加密
function getSha256WithRSA($content, $privateKey){
$binary_signature = "";
$algo = "SHA256";
openssl_sign($content, $binary_signature, $privateKey, $algo);
$sign = base64_encode($binary_signature);
return $sign;
}
orderstatus();
|
评分
-
查看全部评分
|