微信----小程序支付

$appid = '';//若是是公衆號 就是公衆號的appid;小程序就是小程序的appid
$body = '描述';
$mch_id = '商戶號';
$KEY = '商戶平臺的key值';
$nonce_str = $this->getRandCode();//隨機字符串
$notify_url = 'https://xxx.xxxxxx.com/xxx/xxxx/xxxxx/login/notify_url'; //支付完成回調地址url,不能帶參數
$out_trade_no = date('YmdHis', time());//$order_id;//商戶訂單號
$spbill_create_ip = $_SERVER['SERVER_ADDR'];
$trade_type = 'JSAPI';//交易類型 默認JSAPI

//這裏是按照順序的 由於下面的簽名是按照(字典序)順序 排序錯誤 確定出錯
$post['appid'] = $appid;
$post['body'] = $body;
$post['mch_id'] = $mch_id;
$post['nonce_str'] = $nonce_str;//隨機字符串
$post['notify_url'] = $notify_url;
$post['openid'] = $openid['wx_openid'];
$post['out_trade_no'] = $out_trade_no;
$post['spbill_create_ip'] = $spbill_create_ip;//服務器終端的ip
$post['total_fee'] = intval($total_fee["current_price"]*100); //總金額 最低爲一分錢 必須是整數
$post['trade_type'] = $trade_type;
$sign = $this->MakeSign($post,$KEY); //簽名s

$post_xml = '<xml>
<appid><![CDATA['.$appid.']]></appid>
<body><![CDATA['.$body.']]></body>
<mch_id><![CDATA['.$mch_id.']]></mch_id>
<nonce_str><![CDATA['.$nonce_str.']]></nonce_str>
<notify_url><![CDATA['.$notify_url.']]></notify_url>
<openid><![CDATA['.$openid["wx_openid"].']]></openid>
<out_trade_no><![CDATA['.$out_trade_no.']]></out_trade_no>
<spbill_create_ip><![CDATA['.$spbill_create_ip.']]></spbill_create_ip>
<total_fee><![CDATA['.intval($total_fee["current_price"]*100).']]></total_fee>
<trade_type><![CDATA['.$trade_type.']]></trade_type>
<sign><![CDATA['.$sign.']]></sign>
</xml>';

//統一下單接口prepay_id
$url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
$xml = ihttp_post($url,$post_xml); //POST方式請求http
$array = $this->xml2array($xml['content']); //將【統一下單】api返回xml數據轉換成數組,全要大寫if($array['RETURN_CODE'] == 'SUCCESS' && $array['RESULT_CODE'] == 'SUCCESS $time = time();
 $tmp=''; //臨時數組用於簽名
$tmp['appId'] = $appid;
$tmp['nonceStr'] = $nonce_str;
$tmp['package'] = 'prepay_id='.$array['PREPAY_ID'];
$tmp['signType'] = 'MD5';
$tmp['timeStamp'] = $time;

$data['timeStamp'] = "$time"; //時間戳
$data['nonceStr'] = $nonce_str; //隨機字符串
$data['signType'] = 'MD5'; //簽名算法,暫支持 MD5
$data['package'] = 'prepay_id='.$array['PREPAY_ID']; //統一下單接口返回的 prepay_id 參數值,提交格式如:prepay_id=*
$data['paySign'] = $this->MakeSign($tmp,$KEY); //簽名,具體簽名方案參見微信公衆號支付幫助文檔;


}

//獲取到$nonce_str
public function getRandCode()
{
$charts = "ABCDEFGHJKLMNPQRSTUVWXYZabcdefghjkmnpqrstuvwxyz0123456789";
$max = strlen($charts);
$noncestr = "";
for($i = 0; $i < 16; $i++)
{
$noncestr .= $charts[mt_rand(0, $max)];
}
return $noncestr;
}

/**
* 生成簽名, $KEY就是支付key
* @return 簽名
*/
public function MakeSign($params,$KEY){
//簽名步驟一:按字典序排序數組參數
ksort($params);
$string = $this->ToUrlParams($params); //參數進行拼接key=value&k=v
//簽名步驟二:在string後加入KEY
$string = $string . "&key=".$KEY;
//簽名步驟三:MD5加密
$string = md5($string);
//簽名步驟四:全部字符轉爲大寫
$string = strtoupper($string);
return $string;
}

public function ToUrlParams( $params ){
$string = '';
if( !empty($params) ){
$array = array();
foreach( $params as $key => $value ){
$array[] = $key.'='.$value;
}
$string = implode("&",$array);
}
return $string;
}

//獲取xml裏面數據,轉換成array
public function xml2array($xml){
$p = xml_parser_create();
xml_parse_into_struct($p, $xml, $vals, $index);
xml_parser_free($p);
$data = "";
foreach ($index as $key=>$value) {
if($key == 'xml' || $key == 'XML') continue;
$tag = $vals[$value[0]]['tag'];
$value = $vals[$value[0]]['value'];
$data[$tag] = $value;
}
return $data;
}


//支付成功後的回調url處理 (https://xxx.xxxxxx.com/xxx/xxxx/xxxxx/login/notify_url)

這裏須要導入微信提供的SDK
public function notify_url() {
vendor('WeixinPay.lib.WxPay#Api');
$msg = '';
\WxPayApi::notify(function($res){
$log_file = 'b.txt';
//當前連接
//$url = $_SERVER['REQUEST_URI'] ? $_SERVER['REQUEST_URI'] : $_SERVER['PHP_SELF'];
//寫入的內容
$con = '';
foreach ($res as $k => $v) {
$con .= "[time:]".date('Y-m-d H:i:s',time()) . " [{$k}] => {$v} \r\n";
}
$con .= "===================================================== \r\n";
//寫入到log文本中
file_put_contents($log_file,$con, FILE_APPEND);

if($res['return_code'] == 'SUCCESS' && $res['result_code'] == 'SUCCESS'){
$data = array();
$data['weixin_order_id'] = $res['transaction_id'];
$data['out_trade_no'] = $res['out_trade_no'];
$data['bank_type'] = $res['bank_type'];
$data['status'] = 4;

$cus_id = M('customers')->where('wx_openid="%s"', $res['openid'])->field('id')->find();
$cus_id = $cus_id['id'];
$aa = M('orders')->where('customer_id="%s" and errmsg="" and package=""', $cus_id)->save($data);
if (!$aa) {
echo '';exit;
} else {
$this->return_success();
}
} else {
echo '';exit;
}
}, $msg);
//echo $msg;
}
public function return_success(){    $return['return_code'] = 'SUCCESS';    $return['return_msg'] = 'OK';    $xml_post = '<xml>                <return_code><![CDATA[' . $return["return_code"] . ']]></return_code>                <return_msg><![CDATA['. $return["return_msg"] .']]></return_msg>                </xml>';    echo $xml_post;exit;}
相關文章
相關標籤/搜索