本版本参考网友

<?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. O(n log n)求最长上升子序列与最长不下降子序列

    考虑dp(i)表示新上升子序列第i位数值的最小值.由于dp数组是单调的,所以对于每一个数,我们可以二分出它在dp数组中的位置,然后更新就可以了,最终的答案就是dp数组中第一个出现正无穷的位置. 代码非 ...

  2. [RN] React Native ScrollView去掉自带的间隔

    React Native ScrollView去掉自带的间隔 使用ScrollView时,自带了一个类似marginTop的效果,将其去掉 <ScrollView automaticallyAd ...

  3. [LeetCode] 908. Smallest Range I 最小区间

    Given an array A of integers, for each integer A[i] we may choose any x with -K <= x <= K, and ...

  4. [LeetCode] 255. Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列

    Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...

  5. [解決方案]IIS配置后报错500.21

    如果报错这个信息,那么就是aspnet未注册造成的,需要安装一下 步骤: 1.打开CMD 2.输入cd %windir%\Microsoft.Net\Framework\v4.0.30319 3.执行 ...

  6. 使用VUECLI3

    $ yarn global add @vue/cli // OR $ npm install @vue/cli -g 然后在加入下面的代码 $ vue create my-app $ cd my-ap ...

  7. 如何解决aws解绑银行卡问题?

    首先先来说明一下我自己的情况? 一年的免费使用 前提:没有开启任何的实例服务 先贴一条官方的解释 关于我小白一个.学校课程要求使用aws,注册之后在网络上看到一堆人踩坑,aws的扣费就是个坑! 预授权 ...

  8. 图解微信小程序---scroll_view实现首页排行榜布局

    图解微信小程序---scroll_view实现首页排行榜布局 什么是scroll-view? 滚动视图可滚动视图区域.使用竖向滚动时,需要给scroll-view一个固定高度,通过 WXSS 设置 h ...

  9. 【华为云实战开发】8.如何快速搭建C#网站并实现持续集成?【华为云技术分享】

    1 概述 1.1 文章目的 本文通过一个实例介绍如何使用软件开发服务DevCloud完成一个C#Web项目的开发. 1.2 项目详情 1. 项目名称:超级冷笑话网站 2. 项目简介:一个Web网站,包 ...

  10. 记一个netcore HttpClient的坑

    异常信息 The SSL connection could not be established, see inner exception ---> AuthenticationExceptio ...