一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

php微信支付App服务端接口程序代码

时间:2022-06-25 02:28:10 编辑:袖梨 来源:一聚教程网


一:获取根据客户端传入数据 远程获取prepay_id

public function WxpayPost(){
 
// $data='{"login_id":"479090","nick_name":"150****1068","body":"weixin","source":"android","total_fee":"1","versionNum":"2.58"}';
// $data=json_decode($data,true);
$this->values['appid'] = APPID;
 
$this->values['body'] = trim($data['body']);
 
$this->values['mch_id']= MCHID;//商户号
 
$this->values['nonce_str'] = self::getNonceStr();
 
$this->values['notify_url'] = 'http://'.$_SERVER['HTTP_HOST'].'/App/WxPay/Nofity';
 
//$this->values['openid'] = $openid;
$this->values['out_trade_no'] = MCHID.date("YmdHis");
 
$this->values['spbill_create_ip']=$_SERVER['REMOTE_ADDR'];//终端ip
 
$this->values['total_fee'] = $data['total_fee'];
 
$this->values['trade_type'] = "APP";
//$this->values['sign'] = $data['sign'];
$order= $this->unifiedOrder( $this->values);
 
if($order){
 
//$order['xml']['prepay_id'];
 
}
 
}
 
 
 
public static function unifiedOrder($inputObj, $timeOut = 6)
{
$url = "https://api.mch.weixin.qq.com/pay/unifiedorder";
//检测必填参数
if(!WxPayService::IsOut_trade_noSet($inputObj)) {
throw_exception("缺少统一支付接口必填参数out_trade_no!");
}else if(!WxPayService::IsBodySet($inputObj)){
throw_exception("缺少统一支付接口必填参数body!");
}else if(!WxPayService::IsTotal_feeSet($inputObj)) {
throw_exception("缺少统一支付接口必填参数total_fee!");
}else if(!WxPayService::IsTrade_typeSet($inputObj)) {
throw_exception("缺少统一支付接口必填参数trade_type!");
}
 
//关联参数
if(WxPayService::GetTrade_type($inputObj) == "JSAPI" && !WxPayService::IsOpenidSet($inputObj)){
throw_exception("统一支付接口中,缺少必填参数openid!trade_type为JSAPI时,openid为必填参数!");
}
if(WxPayService::GetTrade_type($inputObj) == "NATIVE" && !WxPayService::IsProduct_idSet($inputObj)){
throw_exception("统一支付接口中,缺少必填参数product_id!trade_type为JSAPI时,product_id为必填参数!");
}
 
//异步通知url未设置,则使用配置文件中的url
if(!WxPayService::IsNotify_urlSet($inputObj)){
WxPayService::SetNotify_url(NOTIFY_URL);//异步通知url
}
 
// $inputObj['appid']= APPID ;//公众账号ID
 
// $inputObj['spbill_create_ip']= $_SERVER['REMOTE_ADDR'];//终端ip
//$inputObj->SetSpbill_create_ip("1.1.1.1");
//$inputObj['nonce_str']=self::getNonceStr();//随机字符串
//签名
$inputObj['sign']= WxPayService::SetSign($inputObj);
$xml = WxPayService::ToXml($inputObj);
 
$response = self::postXmlCurl($xml, $url, false, $timeOut);
$result = WxPayService::Init($response,$inputObj);
 
$results=array("sign"=>$inputObj['sign'],"xml"=>$result);
return $results;
}
 
 
 
/**
* 以post方式提交xml到对应的接口url
* Enter description here ...
* @param string $xml 需要post的xml数据
* @param string $url url
* @param bool $useCert 是否需要证书,默认不需要
* @param int $second url执行超时时间,默认30s
* @throws WxPayException
*/
private static function postXmlCurl($xml, $url, $useCert = false, $second = 30)
{
//初始化curl
$ch = curl_init();
//设置超时
curl_setopt($ch, CURLOPT_TIMEOUT, $second);
 
//如果有配置代理这里就设置代理
// if(self::CURL_PROXY_HOST != "0.0.0.0"
// && self::CURL_PROXY_PORT != 0){
// curl_setopt($ch,CURLOPT_PROXY, self::CURL_PROXY_HOST);
// curl_setopt($ch,CURLOPT_PROXYPORT, self::CURL_PROXY_PORT);
// }
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);
// if($useCert == true){
// //设置证书
// //使用证书:cert 与 key 分别属于两个.pem文件
// curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
// curl_setopt($ch,CURLOPT_SSLCERT, self::SSLCERT_PATH);
// curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
// curl_setopt($ch,CURLOPT_SSLKEY, self::SSLKEY_PATH);
// }
//post提交方式
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
//运行curl
$data = curl_exec($ch);
//返回结果
if($data){
curl_close($ch);
return $data;
} else {
$error = curl_errno($ch);
curl_close($ch);
throw_exception("curl出错,错误码:$error");
}
}

二:异步通知处理


public function Nofity(){
 
$postStr= $GLOBALS['HTTP_RAW_POST_DATA'];
 
$fp = @fopen( './notify.txt', 'w+');
@fwrite($fp, $postStr . "|||" . time() );
 
$xml=$this->verifyNotify($postStr);
//print_r($xml);
if($xml['return_code']=='SUCCESS'){
$orderxml=$this->orderQuery($postStr);
$orders=$this->getXmlArray($orderxml);
 
$user_order=M("dtb_user_order","","");
if($orders['result_code']=="SUCCESS"){
 
$time=date("Y-m-d H:i:s",time());
 
$user_order->execute("update dtb_user_order set `pay_status`=1 , pay_time='{$time}' where order_sn='{$orders['out_trade_no']}'");
}else{
$user_order->execute("update dtb_user_order set `pay_status`=0 where order_sn={$orders['out_trade_no']}");
}
}
 
}
 
 
 
public function orderQuery($data){
$xml = $this->getXmlArray($data);
$order['appid']=APPID;
$order['mch_id']=MCHID;
$order['nonce_str']=$xml['nonce_str'];
$order['transaction_id']=$xml['transaction_id'];
$order['sign']= WxPayService::SetSign($order);
$xml = WxPayService::ToXml($order);
 
$url="https://api.mch.weixin.qq.com/pay/orderquery";
$timeOut = 6;
$response = self::postXmlCurl($xml, $url, false, $timeOut);
return $response;
// print_r($response);
}

热门栏目