$value){ if($value != '' && !is_array($value)){ $str .= $key.'='.$value.'&'; } } $str .= 'key='.$merchant_key; return strtoupper(md5($str)); } //生成xml function toXml($params){ $xml = ''; foreach($params as $key => $value){ if(is_numeric($value)){ $xml .= '<'.$key.'>'.$value.''; }else{ $xml .= '<'.$key.'>'; } } $xml .= ''; return $xml; } //发送post请求 function curlPost($url, $data){ $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, $data); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $result = curl_exec($ch); curl_close($ch); return $result; } //生成预支付订单 function createPrePayOrder($merchant_id, $merchant_key, $notify_url, $order_no, $amount){ //请求地址 $url = 'https://api.mch.weixin.qq.com/pay/unifiedorder'; //生成随机字符串 $nonce_str = md5(time()); //构造参数 $params = array( 'appid' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 'mch_id' => $merchant_id, 'nonce_str' => $nonce_str, 'body' => '商品购买', 'out_trade_no' => $order_no, 'total_fee' => $amount, 'spbill_create_ip' => $_SERVER['REMOTE_ADDR'], 'notify_url' => $notify_url, 'trade_type' => 'JSAPI', 'openid' => $_SESSION['openid'] ); //生成签名 $params['sign'] = createSign($params, $merchant_key); //将参数转换为xml格式 $xml = toXml($params); //发送请求 $result = curlPost($url, $xml); //解析返回结果 $data = simplexml_load_string($result, 'SimpleXMLElement', LIBXML_NOCDATA); //获取prepay_id $prepay_id = $data->prepay_id; //生成支付签名 $params = array( 'appId' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 'timeStamp' => time(), 'nonceStr' => md5(time()), 'package' => 'prepay_id='.$prepay_id, 'signType' => 'MD5' ); $params['paySign'] = createSign($params, $merchant_key); return $params; } //生成支付按钮 function createPayButton($product_name, $product_image, $product_pay_link, $merchant_id, $merchant_key, $notify_url){ $params = createPrePayOrder($merchant_id, $merchant_key, $notify_url, $order_no, $amount); $button = '
'; $button .= ''; $button .= '

'.$product_name.'

'; $button .= ''; $button .= '
'; return $button; } //页面显示 echo createPayButton($product_name1, $product_image1, $product_pay_link1, $merchant_id, $merchant_key, $notify_url); echo createPayButton($product_name2, $product_image2, $product_pay_link2, $merchant_id, $merchant_key, $notify_url); echo createPayButton($product_name3, $product_image3, $product_pay_link3, $merchant_id, $merchant_key, $notify_url); //回调处理 $resultXml = file_get_contents('php://input'); $result = simplexml_load_string($resultXml); if($result->return_code=='SUCCESS' && $result->result_code=='SUCCESS'){ //处理订单逻辑 //发送通知 $params = array( 'return_code' => 'SUCCESS', 'return_msg' => 'OK', ); $params['sign'] = createSign($params, $merchant_key); echo toXml($params); } ?> 微信公众号商户收款PHP代码示例 - 支持3个商品展示及自动通知

原文地址: http://www.cveoy.top/t/topic/lMxU 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录