<?php
namespace feng;
class BaiduPay
{
private static $config = array(
'deal_id' => '',
'app_key' => '',
'private_key' => '',
'public_key' => '',
'notify_url' => '',
);
public function __construct($config=NULL){
$config && self::$config = $config;
}
public static function xcxPay($order)
{
if(!is_array($order) || count($order) < 3)
die("数组数据信息缺失!");
$config = self::$config;
$requestParamsArr = array(
'appKey' => $config['app_key'],
'dealId' => $config['deal_id'],
'tpOrderId' => $order['order_sn'],
'totalAmount' => $order['total_amount'],
);
$rsaSign = self::makeSign($requestParamsArr, $config['private_key']);
$bizInfo = array(
'tpData' => array(
"appKey" => $config['app_key'],
"dealId" => $config['deal_id'],
"tpOrderId" => $order['order_sn'],
"rsaSign" => $rsaSign,
"totalAmount" => $order['total_amount'],
"returnData" => '',
"displayData" => array(
"cashierTopBlock" => array(
array(
[ "leftCol" => "订单名称", "rightCol" => $order['body'] ],
[ "leftCol" => "数量", "rightCol" => "1" ],
[ "leftCol" => "订单金额", "rightCol" => $order['total_amount'] ]
),
array(
[ "leftCol" => "服务地址", "rightCol" => "北京市海淀区上地十街10号百度大厦" ],
[ "leftCol" => "服务时间", "rightCol" => "2018/10/29 14:51" ],
[ "leftCol" => "服务人员", "rightCol" => "百度App" ]
)
)
),
"dealTitle" => $order['body'],
"dealSubTitle" => $order['body'],
"dealThumbView" => "https://b.bdstatic.com/searchbox/icms/searchbox/img/swan-logo.png",
),
"orderDetailData" => ''
);
$bdOrder = array(
'dealId' => $config['deal_id'],
'appKey' => $config['app_key'],
'totalAmount' => $order['total_amount'],
'tpOrderId' => $order['order_sn'],
'dealTitle' => $order['body'],
'signFieldsRange' => 1,
'rsaSign' => $rsaSign,
'bizInfo' => json_encode($bizInfo),
);
return $bdOrder;
}
public static function refund($order=[], $type=1)
{
$config = self::$config;
$data = array(
'access_token' => $order['access_token'],
'applyRefundMoney' => $order['total_amount'],
'bizRefundBatchId' => $order['order_sn'],
'isSkipAudit' => 1,
'orderId' => $order['order_id'],
'refundReason' => $order['body'],
'refundType' => $type,
'tpOrderId' => $order['order_sn'],
'userId' => $order['user_id'],
);
$array = ['errno'=>0, 'msg'=>'success', 'data'=> ['isConsumed'=>2] ];
$url = 'https:
$response = self::post_curl($url, $data);
$result = json_decode($response, true);
return $result;
}
public static function notify()
{
$data = $_POST;
$config = self::$config;
if (!$data || empty($data['rsaSign']))
die('暂无回调信息');
$result = self::checkSign($data, $config['public_key']);
if ($result && $data['status']==2) {
return $data;
} else {
return false;
}
}
public static function success()
{
$array = ['errno'=>0, 'msg'=>'success', 'data'=> ['isConsumed'=>2] ];
die(json_encode($array));
}
public static function error()
{
$array = ['errno'=>0, 'msg'=>'success', 'data'=> ['isErrorOrder'=>1, 'isConsumed'=>2] ];
die(json_encode($array));
}
public static function makeSign(array $assocArr, $rsaPriKeyStr)
{
$sign = '';
if (empty($rsaPriKeyStr) || empty($assocArr)) {
return $sign;
}
if (!function_exists('openssl_pkey_get_private') || !function_exists('openssl_sign')) {
throw new Exception("openssl扩展不存在");
}
$rsaPriKeyPem = self::convertRSAKeyStr2Pem($rsaPriKeyStr, 1);
$priKey = openssl_pkey_get_private($rsaPriKeyPem);
if (isset($assocArr['sign'])) {
unset($assocArr['sign']);
}
ksort($assocArr);
$parts = array();
foreach ($assocArr as $k => $v) {
$parts[] = $k . '=' . $v;
}
$str = implode('&', $parts);
openssl_sign($str, $sign, $priKey);
openssl_free_key($priKey);
return base64_encode($sign);
}
public static function checkSign(array $assocArr, $rsaPubKeyStr)
{
if (!isset($assocArr['rsaSign']) || empty($assocArr) || empty($rsaPubKeyStr)) {
return false;
}
if (!function_exists('openssl_pkey_get_public') || !function_exists('openssl_verify')) {
throw new Exception("openssl扩展不存在");
}
$sign = $assocArr['rsaSign'];
unset($assocArr['rsaSign']);
if (empty($assocArr)) {
return false;
}
ksort($assocArr);
$parts = array();
foreach ($assocArr as $k => $v) {
$parts[] = $k . '=' . $v;
}
$str = implode('&', $parts);
$sign = base64_decode($sign);
$rsaPubKeyPem = self::convertRSAKeyStr2Pem($rsaPubKeyStr);
$pubKey = openssl_pkey_get_public($rsaPubKeyPem);
$result = (bool)openssl_verify($str, $sign, $pubKey);
openssl_free_key($pubKey);
return $result;
}
public static function convertRSAKeyStr2Pem($rsaKeyStr, $keyType = 0)
{
$pemWidth = 64;
$rsaKeyPem = '';
$begin = '-----BEGIN ';
$end = '-----END ';
$key = ' KEY-----';
$type = $keyType ? 'RSA PRIVATE' : 'PUBLIC';
$keyPrefix = $begin . $type . $key;
$keySuffix = $end . $type . $key;
$rsaKeyPem .= $keyPrefix . "\n";
$rsaKeyPem .= wordwrap($rsaKeyStr, $pemWidth, "\n", true) . "\n";
$rsaKeyPem .= $keySuffix;
if (!function_exists('openssl_pkey_get_public') || !function_exists('openssl_pkey_get_private')) {
return false;
}
if ($keyType == 0 && false == openssl_pkey_get_public($rsaKeyPem)) {
return false;
}
if ($keyType == 1 && false == openssl_pkey_get_private($rsaKeyPem)) {
return false;
}
return $rsaKeyPem;
}
public static function post_curl($url='',$postData='',$header=[]){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5000);
curl_setopt($ch, CURLOPT_TIMEOUT, 5000);
if($header){
curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
}
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$result = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curlErrNo = curl_errno($ch);
$curlErr = curl_error($ch);
curl_close($ch);
return $result;
}
}