本版本参考网友

<?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. Cookie 技术

    Cookie 学习: 问题: HTTP 协议是没有记忆功能的,一次请求结束后,相关数据会被销毁.如果第二次的请求需要使用相同的请求数据怎么办呢?难道是让用户再次请求书写吗? 解决:使用 Cookie ...

  2. 树莓派安装opencv3及其扩展库

    https://www.cnblogs.com/Pyrokine/p/8921285.html 目标编译针对python的opencv以及扩展库 环境树莓派4和3B+都可以python3.7.3 py ...

  3. 打开navicat 报错????

    好心好意打开数据库图形化管理工具居然报错了???? 那怎么办呢????? google一下吧...... 下载两个文件 就是这两个 放入你的图形化页面的目录中 就可以正常打开了 帅不帅??? 那怎么下 ...

  4. Linux性能优化实战学习笔记:第三十八讲

    一.上节回顾 上一节,我们学习了 DNS 性能问题的分析和优化方法.简单回顾一下,DNS 可以提供域名和 IP 地址的映射关系,也是一种常用的全局负载均衡(GSLB)实现方法. 通常,需要暴露到公网的 ...

  5. [LeetCode] 395. Longest Substring with At Least K Repeating Characters 至少有K个重复字符的最长子字符串

    Find the length of the longest substring T of a given string (consists of lowercase letters only) su ...

  6. [LeetCode] 29. Divide Two Integers 两数相除

    Given two integers dividend and divisor, divide two integers without using multiplication, division ...

  7. ORACLE--10G安装问题( error while loading shared libraries)

    01,问题描述 问题一: WARNING: directory '/u01/app/oracle/product/10.2.0' is not owned by root WARNING: direc ...

  8. PHP stdclass转array的方法

    PHP stdclass转array的方法 <pre><?php$a = new stdClass();$a->id = '11 ';$a->username = 'me ...

  9. mysql操作(精简版)

    一.数据库操作(建库.删库) 1.查看数据库:show databases; 2.创建数据库:DROP DATABASE 数据库名; 3.删除数据库:CREATE DATABASE 数据库名; 4.使 ...

  10. vertica 中位数函数 MEDIAN 的使用

    中位数函数:MEDIAN 使用表达式:MEDIAN ( expression ) OVER ( [ window‑partition‑clause ] ) 准备测试数据: ), name ), sal ...