微信v3app支付php,php微信支付之APP支付方法_php技巧
本文实例讲述了微信开放平台移动应用集成微信支付功能。分享给大家供大家参考。具体分析如下:
WechatAppPay文件代码如下:
代码如下:
namespace common\services\WechatPay;
class WechatAppPay extends WechatPayBase
{
//package参数
public $package = [];
//异步通知参数
public $notify = [];
//推送预支付订单参数
protected $config = [];
//存储access token和获取时间的文件
protected $file;
//access token
protected $accessToken;
//取access token的url
const ACCESS_TOKEN_URL = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s';
//生成预支付订单提交地址
const POST_ORDER_URL = 'https://api.weixin.qq.com/pay/genprepay?access_token=%s';
public function __construct()
{
$this->file = __DIR__ . '/payAccessToken.txt';
}
/**
* 创建APP支付最终返回参数
* @throws \Exception
* @return multitype:string NULL
*/
public function createAppPayData()
{
$this->generateConfig();
$prepayid = $this->getPrepayid();
try{
$array = [
'appid' => $this->appid,
'appkey' => $this->paySignkey,
'noncestr' => $this->getRandomStr(),
'package' => 'Sign=WXPay',
'partnerid' => $this->partnerId,
'prepayid' => $prepayid,
'timestamp' => (string)time(),
];
$array['sign'] = $this->sha1Sign($array);
unset($array['appkey']);
} catch(\Exception $e) {
throw new \Exception($e->getMessage());
}
return $array;
}
/**
* 验证支付成功后的通知参数
*
* @throws \Exception
* @return boolean
*/
public function verifyNotify()
{
try{
$staySignStr = $this->notify;
unset($staySignStr['sign']);
$sign = $this->signData($staySignStr);
return $this->notify['sign'] === $sign;
} catch(\Exception $e) {
throw new \Exception($e->getMessage());
}
}
/**
* 魔术方法,给添加支付参数进来
*
* @param string $name 参数名
* @param string $value 参数值
*/
public function __set($name, $value)
{
$this->$name = $value;
}
/**
* 设置access token
* @param string $token
* @throws \Exception
* @return boolean
*/
public function setAccessToken()
{
try{
if(!file_exists($this->file) || !is_file($this->file)) {
$f = fopen($this->file, 'a');
fclose($f);
}
$content = file_get_contents($this->file);
if(!empty($content)) {
$info = json_decode($content, true);
if( time() - $info['getTime'] < 7150 ) {
$this->accessToken = $info['accessToken'];
return true;
}
}
//文件内容为空或access token已失效,重新获取
$this->outputAccessTokenToFile();
} catch(\Exception $e) {
throw new \Exception($e->getMessage());
}
return true;
}
/**
* 写入access token 到文件
* @throws \Exception
* @return boolean
*/
protected function outputAccessTokenToFile()
{
try{
$f = fopen($this->file, 'wb');
$token = [
'accessToken' => $this->getAccessToken(),
'getTime' => time(),
];
flock($f, LOCK_EX);
fwrite($f, json_encode($token));
flock($f, LOCK_UN);
fclose($f);
$this->accessToken = $token['accessToken'];
} catch(\Exception $e) {
throw new \Exception($e->getMessage());
}
return true;
}
/**
* 取access token
*
* @throws \Exception
* @return string
*/
protected function getAccessToken()
{
$url = sprintf(self::ACCESS_TOKEN_URL, $this->appid, $this->appSecret);
$result = json_decode( $this->getUrl($url), true );
if(isset($result['errcode'])) {
throw new \Exception("get access token failed:{$result['errmsg']}");
}
return $result['access_token'];
}
/**
* 取预支付会话标识
*
* @throws \Exception
* @return string
*/
protected function getPrepayid()
{
$data = json_encode($this->config);
$url = sprintf(self::POST_ORDER_URL, $this->accessToken);
$result = json_decode( $this->postUrl($url, $data), true );
if( isset($result['errcode']) && $result['errcode'] != 0 ) {
throw new \Exception($result['errmsg']);
}
if( !isset($result['prepayid']) ) {
throw new \Exception('get prepayid failed, url request error.');
}
return $result['prepayid'];
}
/**
* 组装预支付参数
*
* @throws \Exception
*/
protected function generateConfig()
{
try{
$this->config = [
'appid' => $this->appid,
'traceid' => $this->traceid,
'noncestr' => $this->getRandomStr(),
'timestamp' => time(),
'package' => $this->generatePackage(),
'sign_method' => $this->sign_method,
];
$this->config['app_signature'] = $this->generateSign();
} catch(\Exception $e) {
throw new \Exception($e->getMessage());
}
}
/**
* 生成package字段
*
* 生成规则:
* 1、生成sign的值signValue
* 2、对package参数再次拼接成查询字符串,值需要进行urlencode
* 3、将sign=signValue拼接到2生成的字符串后面得到最终的package字符串
*
* 第2步urlencode空格需要编码成%20而不是+
*
* RFC 1738会把 空格编码成+
* RFC 3986会把空格编码成%20
*
* @return string
*/
protected function generatePackage()
{
$this->package['sign'] = $this->signData($this->package);
return http_build_query($this->package, '', '&', PHP_QUERY_RFC3986);
}
/**
* 生成签名
*
* @return string
*/
protected function generateSign()
{
$signArray = [
'appid' => $this->appid,
'appkey' => $this->paySignkey,
'noncestr' => $this->config['noncestr'],
'package' => $this->config['package'],
'timestamp' => $this->config['timestamp'],
'traceid' => $this->traceid,
];
return $this->sha1Sign($signArray);
}
/**
* 签名数据
*
* 生成规则:
* 1、字典排序,拼接成查询字符串格式,不需要urlencode
* 2、上一步得到的字符串最后拼接上key=paternerKey
* 3、MD5哈希字符串并转换成大写得到sign的值signValue
*
* @param array $data 待签名数据
* @return string 最终签名结果
*/
protected function signData($data)
{
ksort($data);
$str = $this->arrayToString($data);
$str .= "&key={$this->partnerKey}";
return strtoupper( $this->signMd5($str) );
}
/**
* sha1签名
* 签名规则
* 1、字典排序
* 2、拼接查询字符串
* 3、sha1运算
*
* @param array $arr
* @return string
*/
protected function sha1Sign($arr)
{
ksort($arr);
return sha1( $this->arrayToString($arr) );
}
}
希望本文所述对大家的php程序设计有所帮助。
本条技术文章来源于互联网,如果无意侵犯您的权益请点击此处反馈版权投诉
本文系统来源:php中文网
微信v3app支付php,php微信支付之APP支付方法_php技巧相关推荐
- iOS 拦截支付宝H5支付,完成掉起支付宝APP支付,回到自己APP,完整流程
先说一下这个想法的来源,然后有相关需求的朋友也可以这样来操作 公司产品用到了支付宝支付,但是避免上架审核问题(不能接入支付宝SDK,害怕检测),采用了支付宝的H5支付,作为产品的支付渠道:APP内部, ...
- 微信,支付宝,百度钱包三种APP支付成功关闭浏览器
下面是三种移动app的关闭方式: ? 1 2 3 WeixinJSBridge.call( 'closeWindow' ); //微信 AlipayJSBridge.call( 'closeWebvi ...
- 支付宝app支付回调php,php 支付宝新版本app支付以及回调
支付宝2017年新版本支付基本业务逻辑算法 服务端生成字符串 交给客户端,express 客户端调用接口,将这段字符串str传过去 调用起支付界面.json 其中字符串str包含了全部请求参数,以及请 ...
- JAVA微信APP支付接口整合
2019独角兽企业重金招聘Python工程师标准>>> 上次我们看到了支付宝的APP支付工具,那么这次就来封装封装微信的APP支付;如果已经清楚了支付宝的支付流程,那么微信支付也和它 ...
- 微信App支付全解析
简单介绍了微信移动支付的申请.接入.使用.确认支付结果等相关流程 0 系列文章 系列一 微信App支付全解析 系列二 支付宝App支付全解析 系列三 微信公众号支付全解析 系列四 微信扫码支付全解析 ...
- 2016年微信app支付开发填坑篇
之前开发过高德地图的,百度地图的,人家官网的资料,开发文档,官方论坛,应有尽有,特别详细.微信支付相对支付宝支付,操作繁琐了很多,而且有些文档上的说明太过专业,导致问题多多. 首先他们官网上面只有ec ...
- app提现到微信开通流程图_微信支付商户平台app支付开通方法详解
一.创建移动应用 1.创建移动应用之前,必须得有微信开放平台的认证账号,如无可查阅"网创商盟"历史消息进行申请认证操作. 2.登录微信开放平台创建移动应用,按照页面填写一下信息,包 ...
- 基于spring-boot+uni-app实现app支付功能(微信/支付宝)服务端
基于spring-boot+uni-app实现app支付功能(微信/支付宝)服务端 支付宝支付 1 准备工作 申请支付能力 接口加签方式 2代码 依赖 支付宝支付配置类 支付宝控制层 异步通知 微信支 ...
- 微信支付,JSAPI支付,APP支付,H5支付,Native支付,小程序支付功能详情以及回调处理
一.支付相关文档地址 支付wiki:https://pay.weixin.qq.com/wiki/doc/apiv3/index.shtml 支付api: https://pay.weixin.qq. ...
最新文章
- 使用sqlserver来存放和取得session
- 图解RxJava2(一)
- java 图片压缩 base64_图片改变像素,宽高,Base64编码处理
- node转发请求 .csv格式文件下载 中文乱码问题 + 文件上传笔记
- 我对计算机网络的期待,表白对女朋友说的情话短句 我愿是你最期待的风景线...
- python表达式3 2 3的值为_Python3中的表达式运算符
- csh shell_Shell基础知识
- Freeview%20Play是什么
- PCL Lesson5: 直通滤波+空间平面拟合+提供原始点云数据集PCD文件
- Java抓取淘宝/天猫商品详情 1
- java batik_Java Batik框架画SVG图 JSVGCanvas
- 中国石油大学《工程概预算与招投标》第三阶段在线作业
- 一分钟解决Chrome浏览器主页被hao123、360和2345篡改简单有效方法
- Git 六 时光穿梭机
- IT外企那点儿事(12):也说跳槽
- SpringBoot之RMI的简单使用
- 常见色域基础知识与色域转换公式(YUV/YCbCr/YIQ/RGB/R‘G‘B‘/CMYK)
- 计算机的基本组成 教案反思,《计算机系统组成》教学反思
- Python初级试题25道(含答案)
- Mybatis反射实现装载Mapper