本版本参考网友

<?php

namespace App\Tools;

class Alipay
{ //应用ID,您的APPID。
private $appID = '111';
//商户私钥
private $rsaPrivateKey = '11' private $notifyUrl = '/pay/alipay/notify';
//同步跳转
private $returnUrl = '/pay/alipay/notify';
//编码格式
private $charset = 'UTF-8';
//签名方式
private $signType = 'RSA2';
//支付宝网关
private $gatewayUrl = 'https://openapi.alipay.com/gateway.do';
//支付宝公钥,查看地址:https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
private $rsaPublicKey = 'MIIBIjANBgkqhkiG9w0gws9xPckXVEoGDtrQTEeKvHmoJ81R+wKAHdXnHwzkts1pCYlvfQoAeJf8ibr6qyWkWX/aTrrM72Dd2HewIDAQAB';
private $fileCharset = "UTF-8";
// 表单提交字符集编码
public $postCharset = "UTF-8";
//私钥文件路径
public $rsaPrivateKeyFilePath; /**
* 发起订单
* @param float $totalFee 收款总费用 单位元
* @param string $outTradeNo 唯一的订单号
* @param string $orderName 订单名称
* @param string $notifyUrl 支付结果通知url 不要有问号
* @param string $timestamp 订单发起时间
* @return array
*/
public function pcPay($totalFee, $outTradeNo, $orderName, $httpmethod = "POST")
{
//公共提交参数
$commonConfigs = array(
'app_id' => $this->appID,
'method' => 'alipay.trade.page.pay', //接口名称
'format' => 'JSON',
'return_url' => $this->returnUrl,
'charset' => $this->charset,
'sign_type' => 'RSA2',
'timestamp' => date('Y-m-d H:i:s'),
'version' => '1.0',
'notify_url' => $this->notifyUrl,
);
//请求参数
$requestConfigs = array(
'out_trade_no' => $outTradeNo,
'product_code' => 'FAST_INSTANT_TRADE_PAY',
'total_amount' => $totalFee, //单位 元
'subject' => $orderName, //订单标题
);
$apiParams['biz_content'] = json_encode($requestConfigs); //合并数组
$totalParams = array_merge($apiParams, $commonConfigs);
//待签名字符串
$preSignStr = $this->getSignContent($totalParams);
//生成签名
$totalParams["sign"] = $this->generateSign($totalParams, $this->signType); if ("GET" == strtoupper($httpmethod)) {
// //value做urlencode
$preString = $this->getSignContentUrlencode($totalParams);
//拼接GET请求串
$requestUrl = $this->gatewayUrl . "?" . $preString;
return $requestUrl;
} else {
//拼接表单字符串
return $this->buildRequestForm($totalParams);
}
} /**
* 支付回调
* @param type $param
*/
public function notify($param)
{
$result = $this->check($param);
return $result;
} /**
* 验签方法
* @param $arr 验签支付宝返回的信息,使用支付宝公钥。
* @return boolean
*/
protected function check($arr)
{
$result = $this->rsaCheckV1($arr, $this->rsaPublicKey, $this->signType);
return $result;
} /**
* 建立请求,以表单HTML形式构造(默认)
* @param $para_temp 请求参数数组
* @return 提交表单HTML文本
*/
protected function buildRequestForm($para_temp)
{ $sHtml = "<form id='alipaysubmit' name='alipaysubmit' action='" . $this->gatewayUrl . "?charset=" . trim($this->postCharset) . "' method='POST' >";
while (list($key, $val) = each($para_temp)) {
if (false === $this->checkEmpty($val)) {
//$val = $this->characet($val, $this->postCharset);
$val = str_replace("'", "&apos;", $val);
//$val = str_replace("\"","&quot;",$val);
$sHtml .= "<input type='hidden' name='" . $key . "' value='" . $val . "'/>";
}
}
// foreach ($para_temp as $key => $val) {
// if (false === $this->checkEmpty($val)) {
// //$val = $this->characet($val, $this->postCharset);
// $val = str_replace("'", "&apos;", $val);
// //$val = str_replace("\"","&quot;",$val);
// $sHtml .= "<input type='hidden' name='" . $key . "' value='" . $val . "'/>";
// }
// } //submit按钮控件请不要含有name属性
$sHtml = $sHtml . "<input type='submit' value='ok' style='display:none;''></form>"; $sHtml = $sHtml . "<script>document.forms['alipaysubmit'].submit();</script>"; return $sHtml;
} /**
* 生成签名所需字符串
* @param type $params
* @return string
*/
public function getSignContent($params)
{
ksort($params); $stringToBeSigned = "";
$i = 0;
foreach ($params as $k => $v) {
if (false === $this->checkEmpty($v) && "@" != substr($v, 0, 1)) { // 转换成目标字符集
$v = $this->characet($v, $this->postCharset); if ($i == 0) {
$stringToBeSigned .= "$k" . "=" . "$v";
} else {
$stringToBeSigned .= "&" . "$k" . "=" . "$v";
}
$i++;
}
} unset($k, $v);
return $stringToBeSigned;
} /**
* url拼接转义字符
* 此方法对value做urlencode
* @param type $params
* @return string
*/
public function getSignContentUrlencode($params)
{
ksort($params); $stringToBeSigned = "";
$i = 0;
foreach ($params as $k => $v) {
if (false === $this->checkEmpty($v) && "@" != substr($v, 0, 1)) { // 转换成目标字符集
$v = $this->characet($v, $this->postCharset); if ($i == 0) {
$stringToBeSigned .= "$k" . "=" . urlencode($v);
} else {
$stringToBeSigned .= "&amp" . "$k" . "=" . urlencode($v);
}
$i++;
}
} unset($k, $v);
return $stringToBeSigned;
} /**
* 生成签名
* @param type $data
* @param type $signType
* @return type
*/
protected function sign($data, $signType = "RSA")
{
if ($this->checkEmpty($this->rsaPrivateKeyFilePath)) {
$priKey = $this->rsaPrivateKey;
$res = "-----BEGIN RSA PRIVATE KEY-----\n" .
wordwrap($priKey, 64, "\n", true) .
"\n-----END RSA PRIVATE KEY-----";
} else {
$priKey = file_get_contents($this->rsaPrivateKeyFilePath);
$res = openssl_get_privatekey($priKey);
} ($res) or die('您使用的私钥格式错误,请检查RSA私钥配置'); if ("RSA2" == $signType) {
openssl_sign($data, $sign, $res, OPENSSL_ALGO_SHA256);
} else {
openssl_sign($data, $sign, $res);
} if (!$this->checkEmpty($this->rsaPrivateKeyFilePath)) {
openssl_free_key($res);
}
$sign = base64_encode($sign);
return $sign;
} /**
* 校验$value是否非空
* if not set ,return true;
* if is null , return true;
* */
protected function checkEmpty($value)
{
if (!isset($value)) {
return true;
} if ($value === null) {
return true;
} if (trim($value) === "") {
return true;
} return false;
} /**
* 转换字符集编码
* @param $data
* @param $targetCharset
* @return string
*/
protected function characet($data, $targetCharset)
{
if (!empty($data)) {
$fileType = $this->fileCharset;
if (strcasecmp($fileType, $targetCharset) != 0) {
$data = mb_convert_encoding($data, $targetCharset, $fileType);
// $data = iconv($fileType, $targetCharset.'//IGNORE', $data);
}
}
return $data;
} /**
*
* @param type $params
* @param type $signType
* @return type
*/
public function generateSign($params, $signType = "RSA")
{
return $this->sign($this->getSignContent($params), $signType);
} /**
*
* @param type $params
* @param type $signType
* @return type
*/
public function rsaSign($params, $signType = "RSA")
{
return $this->sign($this->getSignContent($params), $signType);
} /** rsaCheckV1 & rsaCheckV2
* 验证签名
* 在使用本方法前,必须初始化AopClient且传入公钥参数。
* 公钥是否是读取字符串还是读取文件,是根据初始化传入的值判断的。
* */
public function rsaCheckV1($params, $rsaPublicKeyFilePath, $signType = 'RSA')
{
$sign = $params['sign'];
$params['sign_type'] = null;
$params['sign'] = null;
return $this->verify($this->getSignContent($params), $sign, $rsaPublicKeyFilePath, $signType);
} public function rsaCheckV2($params, $rsaPublicKeyFilePath, $signType = 'RSA')
{
$sign = $params['sign'];
$params['sign'] = null;
return $this->verify($this->getSignContent($params), $sign, $rsaPublicKeyFilePath, $signType);
} /**
* 验证
* @param type $data
* @param type $sign
* @param type $rsaPublicKeyFilePath
* @param type $signType
* @return type
*/
public function verify($data, $sign, $rsaPublicKeyFilePath, $signType = 'RSA')
{ if ($this->checkEmpty($this->rsaPrivateKeyFilePath)) { $pubKey = $this->rsaPublicKey;
$res = "-----BEGIN PUBLIC KEY-----\n" .
wordwrap($pubKey, 64, "\n", true) .
"\n-----END PUBLIC KEY-----";
} else {
//读取公钥文件
$pubKey = file_get_contents($rsaPublicKeyFilePath);
//转换为openssl格式密钥
$res = openssl_get_publickey($pubKey);
} ($res) or die('支付宝RSA公钥错误。请检查公钥文件格式是否正确'); //调用openssl内置方法验签,返回bool值 if ("RSA2" == $signType) {
$result = (bool) openssl_verify($data, base64_decode($sign), $res, OPENSSL_ALGO_SHA256);
} else {
$result = (bool) openssl_verify($data, base64_decode($sign), $res);
} if (!$this->checkEmpty($this->rsaPrivateKeyFilePath)) {
//释放资源
openssl_free_key($res);
} return $result;
} }

Alipay 支付类的更多相关文章

  1. NopCommerce Alipay 支付插件

    NopCommerce Alipay 支付插件 1.查找及下载NopCommerce Alipay插件 http://www.nopcommerce.com/p/963/alipay-payment- ...

  2. php支付宝手机网页支付类实例

    <?php $alipayConfig = array( 'key' => 'xxxxx', //买卖安全校验码,用于签名的32位密钥 'transport' => 'https', ...

  3. Django对接支付宝Alipay支付接口

    最新博客更新见我的个人主页: https://xzajyjs.cn 我们在使用Django构建网站时常需要对接第三方支付平台的支付接口,这里就以支付宝为例(其他平台大同小异),使用支付宝开放平台的沙箱 ...

  4. 使用PHP实现手机端APP支付宝的支付功能

    最近应业务需求,做了支付宝支付和微信支付,今天分享一下手机端app支付宝支付对接流程,实际开发过程是前后端分离,前端调用后端API接口,实现功能返回数据,我所用的跨挤啊为TP5,大致可以分为四步: 1 ...

  5. PHP APP端支付宝支付

    应业务需求,做了支付宝支付和微信支付,今天分享一下手机端app支付宝支付对接流程,实际开发过程是前后端分离,前端调用后端API接口,实现功能返回数据,我所用的跨挤啊为TP5,大致可以分为四步: 1.在 ...

  6. alipay 当面付扫码支付实战开发

    alipay 当面付扫码支付开发 参考官网地址:https://opendocs.alipay.com/open/194/105072 1.当面付介绍: 当面付包括付款码支付和扫码支付两种收款方式.适 ...

  7. 【转载】关于Alipay支付宝接口(Java版)

    转载自:http://blog.163.com/lai_chao/blog/static/70340789201412724619514/ 1.alipay 双功能支付简介 2.alipay 提交支付 ...

  8. iOS:集成支付宝支付

    一.介绍 支付宝的集成还是比较简单的,按照文档来一步步操作,基本上很顺利.不过,仍然有两个地方会是坑.这里我集成成功了,在此整理一下.说先说一下我遇到的坑如下: 第一个坑:下载的SDK文件AliPay ...

  9. 支付宝支付后回调通知中responseTxt=true isSign=False可能的问题

    在做支付宝的二维码扫码支付功能,生成二维码成功,扫描后也能付款,付款后也能回调通知到我的页面,但是验证签名的时候出错,找了好久终于找到是什么原因: 引用 1. 用的RSA签名验证,默认密钥纯字符,并不 ...

随机推荐

  1. ASP.NET Core 进程内(InProcess)托管

    ASP.NET Core 进程内(InProcess)托管 在 ASP.NET Core 中的进程内(InProcess)托管模型 什么是 Kestrel 服务器 当一个 ASP.NET Core 应 ...

  2. CF1175D Array Splitting

    题目链接 题意 给出一个长度为\(n\)的序列\(a\),要求分为恰好\(K\)段.第\(i\)个点的贡献是\(a_i \times f(i)\),\(f(x)\)表示x所属的是第几段. 思路 非常巧 ...

  3. Educational Codeforces Round 70 题解

    噩梦场. 题目出奇的难,好像一群外国老哥看 A 看着看着就哭了-- A 找到 \(b\) 最低的 \(1\),这个 \(1\) 肯定要跟 A 中的一个 \(1\) 搭配,而且是能搭配的 \(1\) 中 ...

  4. C#获取屏幕鼠标坐标点颜色

    [DllImport("user32.dll")]        private static extern IntPtr GetDC(IntPtr hwnd);          ...

  5. long 在不同操作系统下所占用的字节数

    不同平台下C\C++数值数据类型长度如下: 类型 win32 win64 linux32 linux64 其中long类型和指针类型需要特别注意,编写跨平台的软件时尽量不要使用long类型,或者需要对 ...

  6. PHP 函数禁用设置方法

    PHP 函数禁用设置方法先找到php.ini 然后搜索disable_function 直接在上面添加你要禁用的函数就可以了 然后记得重启php-fpm(如果装了php-fpm)

  7. Charles设置断点- (超详解)

    1.选择你要设置断点的接口 2.右键选择 Breakpoints 3.断点的相关配置, Proxy ——>Breakpoint Settings 5.双击刚刚已经设置的断点接口,进行设置 6. ...

  8. Redis 内存管理 源码分析

    要想了解redis底层的内存管理是如何进行的,直接看源码绝对是一个很好的选择 下面是我添加了详细注释的源码,需要注意的是,为了便于源码分析,我把redis为了弥补平台差异的那部分代码删了,只需要知道有 ...

  9. [转帖]mDNS原理的简单理解

    mDNS原理的简单理解 https://binkery.com/archives/318.html 发现还有avahi-daemon mdns 设置ip地址 等等事项 网络部分 自己学习的还是不够多 ...

  10. C# 1.0 新特性之异步委托(AP、APM)

    Ø  前言 C# 异步委托也是属于异步编程中的一种,可以称为 Asynchronous Programming(异步编程)或者 Asynchronous Programming Model(异步编程模 ...