<?php
namespace org;
/**
* 微信支付服务器端下单
* 微信APP支付文档地址: https://pay.weixin.qq.com/wiki/doc/api/app.php?chapter=8_6
* 使用示例
* 构造方法参数
* 'appid' => //填写微信分配的公众账号ID
* 'mch_id' => //填写微信支付分配的商户号
* 'notify_url'=> //填写微信支付结果回调地址
* 'key' => //填写微信商户支付密钥
* );
* 统一下单方法
* $WechatAppPay = new wechatAppPay($options);
* $params['body'] = '商品描述'; //商品描述
* $params['out_trade_no'] = '1217752501201407'; //自定义的订单号,不能重复
* $params['total_fee'] = '100'; //订单金额 只能为整数 单位为分
* $params['trade_type'] = 'APP'; //交易类型 JSAPI | NATIVE |APP | WAP
* $wechatAppPay->unifiedOrder( $params );
新版微信支付(APP支付)没有了服务端demo,并且老版本的php服务端demo也不能使用了,需要商户自己开发来实现。
仔细看了下微信支付提供的接口说明,开发起来也不是那么复杂,完全可以把这些放到一个php中,后续方便其他项目的调用。
参考了csdn的一篇文章及下载资料,先把php代码贴出来。
*/
class WechatAppPay
{
//接口API URL前缀
const API_URL_PREFIX = 'https://api.mch.weixin.qq.com';
//下单地址URL
const UNIFIEDORDER_URL = "/pay/unifiedorder";
//查询订单URL
const ORDERQUERY_URL = "/pay/orderquery";
//关闭订单URL
const CLOSEORDER_URL = "/pay/closeorder";
//公众账号ID
public $appid;
//商户号
public $mch_id;
//随机字符串
public $nonce_str;
//签名
public $sign;
//商品描述
public $body;
//微信用户号
public $openid;
//商户订单号
public $out_trade_no;
//支付总金额
public $total_fee;
//终端IP
public $spbill_create_ip;
//支付结果回调通知地址
public $notify_url;
//交易类型
public $trade_type;
//支付密钥
public $key;
//证书路径
public $SSLCERT_PATH;
public $SSLKEY_PATH;
//所有参数
public $params = array();
public function __construct($appid, $mch_id, $notify_url, $key)
{
$this->appid = $appid;
$this->mch_id = $mch_id;
$this->notify_url = $notify_url;
$this->key = $key;
}
/**
* 下单方法
* @param $params 下单参数
*/
public function unifiedOrder( $params ){
$this->body = $params['body'];
$this->out_trade_no = $params['out_trade_no'];
$this->total_fee = $params['total_fee'];
$this->trade_type = $params['trade_type'];
$this->nonce_str = $this->genRandomString();
$this->spbill_create_ip = getClientIp();
trace($this->spbill_create_ip);
// $this->openid = $params['openid'];
$this->params['appid'] = $this->appid;
$this->params['mch_id'] = $this->mch_id;
$this->params['nonce_str'] = $this->nonce_str;
$this->params['body'] = $this->body;
$this->params['out_trade_no'] = $this->out_trade_no;
$this->params['total_fee'] = $this->total_fee;
$this->params['spbill_create_ip'] = $this->spbill_create_ip;
$this->params['notify_url'] = $this->notify_url;
$this->params['trade_type'] = $this->trade_type;
//$this->params['openid'] = $this->openid;
//获取签名数据
$this->sign = $this->MakeSign( $this->params );
$this->params['sign'] = $this->sign;
$xml = $this->data_to_xml($this->params);
$myfile = fopen("xml.txt", "a") or die("Unable to open file!");
fwrite($myfile, date('Y-m-d H:i:s').'\r\n'.$xml."\r\n");
fclose($myfile);
$response = $this->postXmlCurl($xml, self::API_URL_PREFIX.self::UNIFIEDORDER_URL);
if( !$response ){
return false;
}
$result = $this->xml_to_data( $response );
$myfile = fopen("xml.txt", "a") or die("Unable to open file!");
fwrite($myfile, date('Y-m-d H:i:s').'\r\n'.$response."\r\n");
fclose($myfile);
if( !empty($result['result_code']) && !empty($result['err_code']) ){
$result['err_msg'] = $this->error_code( $result['err_code'] );
}
return $result;
}
/**
* 查询订单信息
* @param $out_trade_no 订单号
* @return array
*/
public function orderQuery( $out_trade_no ){
$this->params['appid'] = $this->appid;
$this->params['mch_id'] = $this->mch_id;
$this->params['nonce_str'] = $this->genRandomString();
$this->params['out_trade_no'] = $out_trade_no;
//获取签名数据
$this->sign = $this->MakeSign( $this->params );
$this->params['sign'] = $this->sign;
$xml = $this->data_to_xml($this->params);
$response = $this->postXmlCurl($xml, self::API_URL_PREFIX.self::ORDERQUERY_URL);
if( !$response ){
return false;
}
$result = $this->xml_to_data( $response );
if( !empty($result['result_code']) && !empty($result['err_code']) ){
$result['err_msg'] = $this->error_code( $result['err_code'] );
}
return $result;
}
/**
* 关闭订单
* @param $out_trade_no 订单号
* @return array
*/
public function closeOrder( $out_trade_no ){
$this->params['appid'] = $this->appid;
$this->params['mch_id'] = $this->mch_id;
$this->params['nonce_str'] = $this->genRandomString();
$this->params['out_trade_no'] = $out_trade_no;
//获取签名数据
$this->sign = $this->MakeSign( $this->params );
$this->params['sign'] = $this->sign;
$xml = $this->data_to_xml($this->params);
$response = $this->postXmlCurl($xml, self::API_URL_PREFIX.self::CLOSEORDER_URL);
if( !$response ){
return false;
}
$result = $this->xml_to_data( $response );
return $result;
}
/**
*
* 获取支付结果通知数据
* return array
*/
public function getNotifyData(){
//获取通知的数据
$xml = $GLOBALS['HTTP_RAW_POST_DATA'];
$data = array();
if( empty($xml) ){
return false;
}
$data = $this->xml_to_data( $xml );
if( !empty($data['return_code']) ){
if( $data['return_code'] == 'FAIL' ){
return false;
}
}
return $data;
}
/**
* 接收通知成功后应答输出XML数据
* @param string $xml
*/
public function replyNotify(){
$data['return_code'] = 'SUCCESS';
$data['return_msg'] = 'OK';
$xml = $this->data_to_xml( $data );
echo $xml;
die();
}
/**
* 生成APP端支付参数
* @param $prepayid 预支付id
*/
public function getAppPayParams( $pr
thinkphp5微信h5支付/手机浏览器调用微信支付
3星 · 超过75%的资源 需积分: 36 180 浏览量
2018-10-10
14:33:03
上传
评论 1
收藏 6KB RAR 举报
爱星爷爱拉芳
- 粉丝: 20
- 资源: 5
最新资源
- 2023 年电子设计竞赛试题.zip
- springboot+activiti+angular 这是spring boot框架集成activiti工作流实现.zip
- 基于Python的PCA人脸识别算法的原理及实现代码+文档详解.zip
- 《人工智能AI生成图片》
- springboot 整合jpa框架和querydsl框架,实现多数据源数据增删改查.zip
- SpringBoot + LayUI 框架快速搭建WEB网站.zip
- 基于python开发的的验证码识别+源码(期末大作业&课程设计&项目开发)
- douban.ipynb
- 基于C#的游戏服务器后台.zip
- 基于neo4j制作的医药领域-疾病诊断知识图谱dump文件
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈