打你一下

php微信支付

<?php

namespace lib;

use think\Loader;

/**
* Description of aliyunsms
*
* @author lsf
*/
class Wepay {

/*服务商支付
* appid公众号APPID,appsecret公众号appsecret,mchid微信商户号,key微信秘钥,title订单详情 ordersn单号,money订单金额
*/

public function serverWepayPayment($option = array()) {
Loader::import('wxpay/Weapp', EXTEND_PATH);
Loader::import('wxpay/WxPayConfig', EXTEND_PATH);
Loader::import('wxpay/WxPayException', EXTEND_PATH);
Loader::import('wxpay/WxPayData', EXTEND_PATH);
Loader::import('wxpay/JsApiPayclass', EXTEND_PATH);
$wxPayConfig = new \WxPayConfig($option);
$input = new \WxPayUnifiedOrder();
$input->SetBody(mb_substr($option["title"], 0, 30, 'utf-8'));
$input->SetOut_trade_no($option["ordersn"]);
$input->SetTotal_fee($option["money"] * 100);
$input->SetTime_start(date('Y-m-d'));
$input->SetTime_expire(date("YmdHis", time() + 600));
$input->SetNotify_url(GetHttp() . $_SERVER['HTTP_HOST'] . "/wy/index/return_notice");
$input->SetTrade_type("JSAPI");
$input->SetSubOpenid($option["openid"]);
$order = \WxPayApi::unifiedOrder($input);
$tools = new \JsApiPay();
$jsApiParameters = $tools->GetJsApiParameters($order);
$jsApiParameters = json_decode($jsApiParameters, true);
return $jsApiParameters;
}


/*
* appid公众号APPID,appsecret公众号appsecret,mchid微信商户号,key微信秘钥,title订单详情 ordersn单号,money订单金额
*/

public function wepayPayment($option = array()) {
Loader::import('wxpay/Weapp', EXTEND_PATH);
Loader::import('wxpay/WxPayConfig', EXTEND_PATH);
Loader::import('wxpay/WxPayException', EXTEND_PATH);
Loader::import('wxpay/WxPayData', EXTEND_PATH);
Loader::import('wxpay/JsApiPayclass', EXTEND_PATH);
$wxPayConfig = new \WxPayConfig($option);
$input = new \WxPayUnifiedOrder();
$input->SetBody(mb_substr($option["title"], 0, 30, 'utf-8'));
$input->SetOut_trade_no($option["ordersn"]);
$input->SetTotal_fee($option["money"] * 100);
$input->SetTime_start(date('Y-m-d'));
$input->SetTime_expire(date("YmdHis", time() + 600));
$input->SetNotify_url(noticeWepayUrl());
$input->SetTrade_type("JSAPI");
$input->SetOpenid($option["openid"]);
$order = \WxPayApi::unifiedOrder($input);

$tools = new \JsApiPay();
$jsApiParameters = $tools->GetJsApiParameters($order);
$jsApiParameters = json_decode($jsApiParameters, true);
return $jsApiParameters;
}

/*
* 微信app支付
* appid公众号APPID,appsecret公众号appsecret,mchid微信商户号,key微信秘钥,title订单详情 ordersn单号,money订单金额
*/

public function appWepayPayment($option = array()) {
$time = time();
$nonce_str = $this->rand_code();
$data['appid'] = $option["appid"];
$data['mch_id'] = $option["mchid"];
$data['body'] = mb_substr($option["title"], 0, 30, 'utf-8');
$data['spbill_create_ip'] = getIp();
$data['total_fee'] = $option["price"] * 100;
$data['out_trade_no'] = $option["ordersn"];
$data['nonce_str'] = $nonce_str;
$data['notify_url'] = noticeWepayUrl();
$data['trade_type'] = 'APP';
$sign = $this->getSign($data, $option);
$data['sign'] = $sign;
$xml = ToXml($data);
$url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
if (stripos($url, "https://") !== FALSE) {
curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
} else {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
}
curl_setopt($ch, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
$data = curl_exec($ch);
if ($data) {
curl_close($ch);
$re = FromXml($data);

if ($re['result_code'] != 'SUCCESS') {
return array("success" => false, "msg" => $re["return_msg"], "data" => "");
} else {
$time = time();
$arr = array(
'appid' => $option["appid"],
'partnerid' => $option["mchid"],
'prepayid' => $re['prepay_id'],
'package' => 'Sign=WXPay',
'noncestr' => $nonce_str,
'timestamp' => $time
);
$sign = $this->getSign($arr, $option);
$newarray["appId"] = $option["appid"];
$newarray["prepayId"] = $re['prepay_id'];
$newarray["machId"] = $option["mchid"];
$newarray["timeStamp"] = $time;
$newarray["nonceStr"] = $nonce_str;
$newarray["paySign"] = $sign;

return array("success" => true, "msg" => "", "data" => $newarray);
}
} else {
$error = curl_errno($ch);
curl_close($ch);
return array("success" => false, "msg" => $error, "data" => "");
}
}

/**
* 微信获取随机数
* @return type
*/
function rand_code() {
$str = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; //62个字符
$str = str_shuffle($str);
$str = substr($str, 0, 32);
return $str;
}

/**
* 获取签名
* @param type $params
* @return type
*/
private function getSign($params, $option) {
ksort($params);
foreach ($params as $key => $item) {
if (!empty($item)) {
$newArr[] = $key . '=' . $item;
}
}
$stringA = implode("&", $newArr);
$stringSignTemp = $stringA . "&key=" . $option["apikey"];
// key是在商户平台API安全里自己设置的
$stringSignTemp = MD5($stringSignTemp);
$sign = strtoupper($stringSignTemp);
return $sign;
}

/*
* 微信h5支付
* appid公众号APPID,appsecret公众号appsecret,mchid微信商户号,key微信秘钥,title订单详情 ordersn单号,money订单金额
*/

public function h5WepayPayment($option = array()) {
$userip = getIp();
$appid = $option["appid"];
$mch_id = $option["mchid"];
$key = $option["key"];
$out_trade_no = $option['ordersn'];
$nonce_str = $this->rand_code();
$body = mb_substr($option["title"], 0, 30, 'utf-8');
$total_fee = $option["money"] * 100;
$spbill_create_ip = $userip;
$notify_url = noticeWepayUrl();
$trade_type = 'MWEB';
$scene_info = '{"h5_info":{"type":"Wap","wap_url":"' . GetHttp . $_SERVER['HTTP_HOST'] . '","wap_name":"支付"}}';
$signA = "appid=$appid&attach=$out_trade_no&body=$body&mch_id=$mch_id&nonce_str=$nonce_str&notify_url=$notify_url&out_trade_no=$out_trade_no&scene_info=$scene_info&spbill_create_ip=$spbill_create_ip&total_fee=$total_fee&trade_type=$trade_type";
$strSignTmp = $signA . "&key=$key";
$sign = strtoupper(MD5($strSignTmp));
$returnUrl = $option["return_url"];
$return_Url = urlencode($returnUrl);
$post_data = "<xml>
<appid>$appid</appid>
<mch_id>$mch_id</mch_id>
<body>$body</body>
<out_trade_no>$out_trade_no</out_trade_no>
<total_fee>$total_fee</total_fee>
<spbill_create_ip>$spbill_create_ip</spbill_create_ip>
<notify_url>$notify_url</notify_url>
<trade_type>$trade_type</trade_type>
<scene_info>$scene_info</scene_info>
<attach>$out_trade_no</attach>
<nonce_str>$nonce_str</nonce_str>
<sign>$sign</sign>
</xml>";
$url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
$dataxml = httpRequest($url, $post_data);
$objectxml = (array) simplexml_load_string($dataxml, 'SimpleXMLElement', LIBXML_NOCDATA);
if ($objectxml['return_code'] != "FAIL") {
return array("success" => false, "msg" => $objectxml['return_msg'], "data" => "");
} else {
return array("success" => true, "msg" => "", "data" => $objectxml['mweb_url'] . "&redirect_url=" . $return_Url);
}
}

public function weChatrefund($option) {

$param = array(
'appid' => $option["appid"],
'mch_id' => $option["mchid"],
'nonce_str' => $this->rand_code(),
'out_refund_no' => $option["ordersn"],
'transaction_id' => $option["transaction_id"],
'total_fee' => $option["money"] * 100,
'refund_fee' => $option["money"] * 100,
);
$param['sign'] = $this->getSign($param, $option);
$xmldata = ToXml($param);
$xmlresult = $this->postXmlSSLCurl($xmldata, 'https://api.mch.weixin.qq.com/secapi/pay/refund', 30, $option);
if ($xmlresult["success"]) {
return array("success" => true, "msg" => "", "data" => FromXml($xmlresult["data"]));
} else {
return array("success" => false, "msg" => $xmlresult["msg"], "data" => "");
}
}

//需要使用证书的请求
private function postXmlSSLCurl($xml, $url, $second = 30, $option) {
$ch = curl_init();
//超时时间
curl_setopt($ch, CURLOPT_TIMEOUT, $second);
//这里设置代理,如果有的话
//curl_setopt($ch,CURLOPT_PROXY, '8.8.8.8');
//curl_setopt($ch,CURLOPT_PROXYPORT, 8080);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
//设置header
curl_setopt($ch, CURLOPT_HEADER, FALSE);
//要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
//设置证书
//使用证书:cert 与 key 分别属于两个.pem文件
//默认格式为PEM,可以注释
curl_setopt($ch, CURLOPT_SSLCERTTYPE, 'PEM');
curl_setopt($ch, CURLOPT_SSLCERT, $option["apiclient_cert"]);
//默认格式为PEM,可以注释
curl_setopt($ch, CURLOPT_SSLKEYTYPE, 'PEM');
curl_setopt($ch, CURLOPT_SSLKEY, $option["apiclient_key"]);
//post提交方式
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
$data = curl_exec($ch);
//返回结果
if ($data) {
curl_close($ch);
return array("success" => true, "msg" => "", "data" => $data);
} else {
$error = curl_errno($ch);
curl_close($ch);
return array("success" => false, "msg" => $error, "data" => "");
}
}
/**
* [sendMoney 企业付款到零钱]
* @param [type] $amount [发送的金额(分)目前发送金额不能少于1元]
* @param [type] $re_openid [发送人的 openid]
* @param string $desc [企业付款描述信息 (必填)]
* @param string $check_name [收款用户姓名 (选填)]
* @return [type] [description]
*/
function sendMoney($amount,$option) {
$total_amount = (100) * $amount;
$data = array(
'mch_appid' => $option["appid"], //商户账号appid
'mchid' => $option["mchid"], //商户号
'nonce_str' => $this->rand_code(), //随机字符串
'partner_trade_no' => $option["partner_trade_no"], //商户订单号
'openid' => $option["openid"], //用户openid
'check_name' => $option["check_name"] ? 'FORCE_CHECK' : 'NO_CHECK', //校验用户姓名选项,
're_user_name' => $option["true_name"], //收款用户姓名
'amount' => $total_amount, //金额
'desc' => $option["desc"], //企业付款描述信息
'spbill_create_ip' => getIp(), //Ip地址
);
$data['sign'] = $this->getSign($data, $option);
$xmldata = ToXml($data);
$xmlresult = $this->postXmlSSLCurl($xmldata, 'https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers', 30, $option);
file_put_contents("./log/isfirst.text", date("Y-m-d H:i:s", time()) . json_encode($xmlresult).$option["partner_trade_no"] . PHP_EOL, FILE_APPEND);
if ($xmlresult["success"]) {
return array("success" => true, "msg" => "", "data" => FromXml($xmlresult["data"]));
} else {
return array("success" => false, "msg" => $xmlresult["msg"], "data" => "");
}
}

}

posted on 2020-06-27 15:52  打你一下  阅读(403)  评论(0编辑  收藏  举报

导航