验证中...
开源中国 2018 年度最后一场技术盛会邀你来约~错过就要等明年啦!点此立即预约
(針對hbuilder h5开发原生APP,但是app原生也能使用)
原始数据 复制代码
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
<?php
/**
* Created by PhpStorm.
* User: Win10
* Date: 2018/11/27
* Time: 11:24
* 微信支付/支付宝接口 (針對hbuilder h5开发原生APP,但是app原生也能使用)
*/
namespace Api\Controller;
class NewpayController extends Controller
{
# 支付宝支付 + 微信支付
public function alipaying(){
# header('Access-Control-Allow-Origin: *');
header('Content-type: text/plain');
$out_trade_no = I('master_order_sn');
$sum_order_amount = M(db)->where("master_order_sn = '$out_trade_no'")->sum('order_amount');
if(!$sum_order_amount)
{
$return_arr = array('status'=>-1,'msg'=>'订单不存在','result'=>array());
exit(json_encode($return_arr));
}
// $sum_order_amount = 0.01;
if($sum_order_amount == 0){
$return_arr = array('status'=>-1,'msg'=>'金额错误','result'=>array());
exit(json_encode($return_arr));
}
if(I('type')==1)
{
$res = $this->weixin($out_trade_no,$sum_order_amount);
echo json_encode($res);exit;
}else{
$res = $this->alipay($out_trade_no,$sum_order_amount);
echo json_encode($res);exit;
}
}
/********微信支付******/
private function weixin($out_trade_no="",$sum_order_amount=0)
{
$data['appid'] = "xxxx"; //应用ID
$data['mch_id'] = "xxx"; //商户号
$paysignkey = "xxx"; // API密钥
$data['nonce_str'] = md5(rand());
$data['body'] = 'APP商品支付';
$data['attach'] = "app" . ':' . 1;
$data['total_fee'] = floatval($sum_order_amount)*100;
$data['out_trade_no'] = $out_trade_no;
$data['spbill_create_ip'] = $_SERVER['REMOTE_ADDR'];
$data['notify_url'] = SITE_URL.'/index.php/Api/Payment/xxx';
$data['trade_type'] = 'APP';
$data['sign'] = $this->weixingetSign($data,$paysignkey);
// dump($data);
$set = array(
'key' => trim($paysignkey),
'appid' => trim($data['appid']),
'mch_id' => trim($data['mch_id']),
'order_sn' => trim(trim($data['out_trade_no']))
);
$xmlData = $this->weixinToXml($data);
$postUrl = "https://api.mch.weixin.qq.com/pay/unifiedorder";
$resXML = $this->weixinglobalCurlPost($postUrl,$xmlData);
$resArr = $this->weixinFromXml($resXML);
$set = array_merge($resArr,$set);
return $this->weixinreturnData($set);
}
/**微信签名包
* @param $arr
* @param $key
* @return string
*/
private function weixingetSign($arr,$key)
{
ksort($arr);//排序
$str = $this->weixinToUrlParams($arr);
$str = $str . "&key=".$key;
$str = md5($str);
return strtoupper($str);
}
/**
* 输出xml字符
* @throws WxPayException
*数组转化成xml数据
**/
private function weixinToXml($arr)
{
if(!is_array($arr) || count($arr) <= 0)
{
return "";
}
$xml = "<xml>";
foreach ($arr as $key=>$val)
{
if (is_numeric($val)){
$xml.="<".$key.">".$val."</".$key.">";
}else{
$xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
}
}
$xml.="</xml>";
return $xml;
}
/**
* 将xml转为array
* @param string $xml
* @throws WxPayException
*/
private function weixinFromXml($xml)
{
if(!$xml){
return array();
}
//将XML转为array
//禁止引用外部xml实体
//libxml_disable_entity_loader(true);
$array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
return $array_data;
}
/**
* 格式化参数格式化成url参数
*/
private function weixinToUrlParams($arr)
{
$buff = "";
foreach ($arr as $k => $v)
{
if($k != "sign" && $v != "" && !is_array($v)){
$buff .= $k . "=" . $v . "&";
}
}
$buff = trim($buff, "&");
return $buff;
}
/**发送微信支付请求
* @param $url
* @param string $data
* @param int $second
* @return mixed
*/
private function weixinglobalCurlPost($url,$data='',$second=30){
$ch = curl_init();
$header = "Accept-Charset: utf-8";
curl_setopt($ch, CURLOPT_TIMEOUT, $second);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$temp = curl_exec($ch);
curl_close($ch);
return $temp;
}
private function weixingetPayApiSign($data){
$package = "prepay_id=".trim($data['prepay_id']);
$timeStamp = strval(time());
$nonceStr = md5(rand()); //随机字符串
$appid = trim($data['appid']);
$key = trim($data['key']);
$mch_id = trim($data['mch_id']);
$arr = array();
$arr['timestamp'] = $timeStamp;
$arr['appid'] = $appid;
$arr['partnerid'] = $mch_id;
$arr['noncestr'] = $nonceStr;
$arr['prepayid'] = trim($data['prepay_id']);
$arr['package'] = "Sign=WXPay";//$arr['package'] = $package;
$arr['sign'] = $this->weixingetSign($arr,$key);
return $arr;
}
////接口返回数据
private function weixinreturnData($arr){
if($arr['return_code'] != "SUCCESS"){
return array('status'=>0,'msg'=>"调用支付失败(".$arr['return_msg'].")",'data'=>array());
}
if($arr['result_code'] == "SUCCESS"){
//print_r($arr);exit;
$signPackage = $this->weixingetPayApiSign($arr); //jsapi 验签
// print_r($signPackage);exit;
if($arr['addr'] != "order"){
//成功调用接口,记录数据xxxxxxxxxxx
}
return array('status'=>1,'msg'=>'支付参数','data'=>$signPackage);
}else{
//调起支付失败
//错误代码:
$errArr = array(
"NOAUTH" => "商户无此接口权限" ,
"NOTENOUGH" => "余额不足" ,
"ORDERPAID" => "商户订单已支付" ,
"ORDERCLOSED" => "订单已关闭" ,
"SYSTEMERROR" => "系统错误/系统超时" ,
"APPID_NOT_EXIST" => "APPID不存在" ,
"MCHID_NOT_EXIST" => "MCHID不存在" ,
"APPID_MCHID_NOT_MATCH" => "appid和mch_id不匹配" ,
"LACK_PARAMS" => "缺少参数" ,
"OUT_TRADE_NO_USED" => "商户订单号重复" ,
"SIGNERROR" => "签名错误" ,
"XML_FORMAT_ERROR" => "XML格式错误" ,
"REQUIRE_POST_METHOD" => "请使用post方法" ,
"POST_DATA_EMPTY" => "post数据为空" ,
"NOT_UTF8" => "编码格式错误"
);
$errCode = $arr['err_code']; //记录错误码
$errMsg = "";
foreach ($errArr as $k=>$v){
if($k == $errCode){
$errMsg = $v;
}
}
return array('status'=>0,'msg'=>$errMsg."(".$arr['err_code_des'].")",'data'=>array());
}
}
/********支付宝支付******/
# 目前使用老接口
private function alipay($out_trade_no="",$sum_order_amount=0)
{
// 支付宝合作者身份ID,以2088开头的16位纯数字
$partner = "xxxx";
// 支付宝账号
$seller_id = 'xxxx';
// 商品网址
$base_path = urlencode('http://www.dcloud.io/helloh5/');
// 异步通知地址
$notify_url = urlencode(SITE_URL.'/index.php/Api/Payment/alipayAppNotify');
// 订单标题
$subject = '订单支付';
// 订单详情
$body = 'APP商品支付';
// 订单号,示例代码使用时间值作为唯一的订单ID号
$parameter = array(
'service' => 'mobile.securitypay.pay', // 必填,接口名称,固定值
'partner' => $partner, // 必填,合作商户号
'_input_charset' => 'UTF-8', // 必填,参数编码字符集
'out_trade_no' => $out_trade_no, // 必填,商户网站唯一订单号
'subject' => $subject, // 必填,商品名称
'payment_type' => '1', // 必填,支付类型
'seller_id' => $seller_id, // 必填,卖家支付宝账号
'total_fee' => $sum_order_amount, // 必填,总金额,取值范围为[0.01,100000000.00]
'body' => $body, // 必填,商品详情
'it_b_pay' => '1d', // 可选,未付款交易的超时时间
'notify_url' => $notify_url // 可选,服务器异步通知页面路径
// 'show_url' => $base_path // 可选,商品展示网站
);
//生成需要签名的订单
$orderInfo = $this->alipaycreateLinkstring($parameter);
//签名
$sign = $this->alipayrsaSign($orderInfo);
//生成订单
return array('status'=>1,'msg'=>'支付宝参数','data'=>$orderInfo.'&sign="'.$sign.'"&sign_type="RSA"');
}
# 支付宝对签名字符串转义
private function alipaycreateLinkstring($para) {
$arg = "";
while (list ($key, $val) = each ($para)) {
$arg.=$key.'="'.$val.'"&';
}
//去掉最后一个&字符
$arg = substr($arg,0,count($arg)-2);
//如果存在转义字符,那么去掉转义
if(get_magic_quotes_gpc()){$arg = stripslashes($arg);}
return $arg;
}
# 支付宝签名生成订单信息
private function alipayrsaSign($data) {
$priKey = "-----BEGIN RSA PRIVATE KEY-----
xxxx
-----END RSA PRIVATE KEY-----
";
$res = openssl_get_privatekey($priKey);
openssl_sign($data, $sign, $res);
openssl_free_key($res);
$sign = base64_encode($sign);
$sign = urlencode($sign);
return $sign;
}
}
/**
支付宝回调
*/
public function alipayAppNotify(){
$verify_result = $_POST?$_POST:0;
$input = file_get_contents('php://input');
//这里最好做下支付宝查询订单接口xxxxxxxxx
if($_POST['trade_status'] == 'TRADE_FINISHED')
{
//xxxxxxxxx
}
else if ($_POST['trade_status'] == 'TRADE_SUCCESS')
{
//xxxxxx
}
}
/**
微信支付
*/
public function weixinAppNotify()
{
$input = file_get_contents('php://input');
libxml_disable_entity_loader(true); // 防xml注入
if (!empty($input) && empty($_GET['out_trade_no']))
{
$obj = simplexml_load_string($input, 'SimpleXMLElement', LIBXML_NOCDATA);
$data = json_decode(json_encode($obj), true);
if (empty($data))
{
exit('fail');
}
if (($data['result_code'] != 'SUCCESS') || ($data['return_code'] != 'SUCCESS'))
{
$result = array('return_code' => 'FAIL', 'return_msg' => (empty($data['return_msg']) ? $data['err_code_des'] : $data['return_msg']));
echo $this->array2xml($result);
exit();
}
$get = $data;
}
else
{
$get = $_GET;
}
Log::write("微信APP支付回调打印数据====".json_encode($get),'WARN');
$strs = explode(':', $get['attach']);
$type = intval($strs[1]);
$total_fee = $get['total_fee'] / 100;
$tid = $get['out_trade_no'];
$order_sn = $tid;
ksort($get);
$string1 = '';
foreach ($get as $k => $v )
{
if (($v != '') && ($k != 'sign'))
{
$string1 .= $k . '=' . $v . '&';
}
}
$sign = strtoupper(md5($string1 . 'key=xxxx'));
if ($sign == $get['sign'])
{
//这里最好做下微信查询订单接口xxxxxxxxx
if($type)
{
if($type == 1)
{
// xxx修改订单支付状态
}
}
}
}

评论列表( 2 )

492389_jefferycai
JefferyCai 2018-12-07 12:03

回调方法在最下面

492389_jefferycai
JefferyCai 2018-12-07 12:03

开发hbuilder的时候 记得,生成私钥的时候,选择java适用(1024长度) (曾经入过这个坑) 其他自己看下。

你可以在登录后,发表评论

搜索帮助