23
11月
2021
摘要:php语言接入平台支付接口
<?php
/**
* author:huangguoheng
* date:2008-02-02
* email:470136534@qq.com
* description:http请求类
*/
class Request
{
private $timeout;
public function __construct($timeout = 30)
{
$this->timeout = $timeout;
}
/**
* post发送json的数据请求到服务器
* @param unknown $url
* 请求的url
* @param unknown $data
* 发送的json字符串数据
*/
public function sendJson($url, $data)
{
$header = array("Accept:application/json", "Content-Type:application/json;charset=utf-8");
return $this->send($url, $data, $header);
}
/**
* 发送post请求
* @param unknown $url
* 请求的url地址
* @param unknown $data
* 发送的数据键值对数组
*
*/
public function sendPost($url, $data, $cookie = '', $header = '')
{
return $this->send($url, $data, $header, 1, $cookie);
}
/**
* 发送get请求
* @param unknown $url
* 请求的url
*/
public function sendGet($url)
{
return $this->send($url, null, null);
}
/**
* 发送https或http请求
* @param unknown $url
* 请求url
* @param unknown $data
* 发送的数据
* @param unknown $header
* 请求头
* @param number $post
* 默认1是post请求
*/
private function send($url, $data, $header, $post = 1, $cookie = '')
{
//初始化curl
$ch = curl_init();
//参数设置
$res = curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, $post);
curl_setopt($ch, CURLOPT_TIMEOUT, $this->timeout);
if ($cookie) {
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
}
if ($post) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
if ($header) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
}
$result = curl_exec($ch);
//连接失败
if ($result == false) {
return null;
}
curl_close($ch);
return $result;
}
}
/**
* author:huangguoheng
* date:2008-02-02
* email:470136534@qq.com
* description:通用工具类
*/
class Common
{
/**
* 获取当前的时间戳
* @return number
*/
public static function getTimeStamp($dateStr = null)
{
if ($dateStr) {
return strtotime($dateStr);
}
return time();
}
/**
* 交换两个元素的位置的方法
* @param strSort 需要交换元素的数组
* @param i 索引i
* @param j 索引j
*/
private static function swap(&$strSort, $i, $j)
{
$t = $strSort[$i];
$strSort[$i] = $strSort[$j];
$strSort[$j] = $t;
}
public static function sort($array, $asc = true)
{
if ($asc) {
asort($array, SORT_NATURAL);
} else {
sort($array, SORT_NATURAL);
}
}
/***
* 获取格式的日期
* 返回的格式如:2008-02-02 23:33:22
* @return string
*/
public static function dateYmdHms($fmt = 'Y-m-d G:i:s')
{
return date($fmt);
}
/**
* 获取年月日
*/
public static function getYmd()
{
return date('Ymd', time());
}
/**
* md5加密
* @param unknown $str
* @param string $saft
* @return string
*/
public static function Md5($str = '', $saft = '')
{
return md5($str . $saft);
}
/***
* author:huangguoheng
* date:20190706
* description:生成hash签名
* @data 是参数数组
* @APPSECRET:32位的值
*/
public static function createHash($datas, $APPSECRET)
{
ksort($datas);
reset($datas);
$arg = '';
foreach ($datas as $key => $val) {
if ($key == 'sign' || is_null($val) || $val === '') {
continue;
}
if ($arg) {
$arg .= '&';
}
$arg .= "$key=$val";
}
return md5($arg . $APPSECRET);
}
/**
* 通过传入int值代表的时间返回日期格式的字符串
* 2011-03-30 21:23:12
* @param unknown $time
* 整形值的日期
* @return string
*/
public static function getTimestamp2Date($time1)
{
return date("Y-m-d H:i:s", $time1);
}
/**
* author:huangguoheng
* date:2019-07-28
* description:判断字符串是否为空
*/
public static function isEmpty($val = '')
{
$b = isset($val);
if (!$b || is_null($val)) {
// Logger::log($val.'');
return true;
}
$val = \preg_replace('/^\s+|\s+$/', '', $val . '');
return strlen($val) < 1;
}
/***
* author:huangguoheng
* date:20190706
* @length:字符串长度
* description:
* 获取一个随机字符串
*/
public static function randStr($length = 4)
{
$str = 'qwertyuiopasdfghjklzxcvbnm1234567890';
$len = strlen($str);
$result = '';
for ($k = 0; $k < $length; $k++) {
$index = rand(1, $len) - 1;
$result .= $str[$index];
}
return $result;
}
/**
* author:huangguoheng
* date:2019-08-11
* description:
* $td: 默认true代表二维数组,false 代表一维数组
*
*/
public static function asArray(&$dbRecord, $td = true)
{
$data = array();
if ($td) {
foreach ($dbRecord as &$value) {
$item = [];
foreach ($value as $key => $val) {
$item[$key] = $val;
}
array_push($data, $item);
}
return $data;
} else {
foreach ($dbRecord as $key => $value) {
$data[$key] = $value;
}
return $data;
}
}
}
//下面的代码是发起一个支付的请求
$rq = new Request();
$data = [
"AppId" => "g8162294909200000001", //应用的AppID,在平台注册的应用获取
"Title" => "付款测试",
"RandStr" => Common::randStr(32),
"Timestamp" => Common::getTimeStamp(),
"EnterpriceId" => 0, //默认值
"Price" => 100, //价格是分
"PayType" => 1, //1是微信支付
"AppSecret" => "1ly2ths416229490920000000001", //应用的AppSecret
"OrderNo" => "202111231120", // Common::randStr(12), //客户生成的订单号
"Description" => "ggg",
"CallbackUrl" => "http://www.baidu.com", //支付成功的回调路径
];
$data['sign'] = Common::createHash($data, $data['AppSecret']);
$data['AppSecret'] = "";
$v = $rq->sendPost("http://www.love2where.com/api/pay/ppay", $data
);
header("Content-type: text/html; charset=utf-8");
print_r($v);
//echo "hello world"
非特殊说明,本文版权归HPay所有,转载请注明出处.
本文类型: Java工具