<?php
namespace fast;
class Ottpay
{
protected $merchant; //商户号 Provided by OTTPay
protected $operatorId; //Provided by OTTPay
protected $appId; //微信小程序唯一标识
protected $backurl; //商户号
protected $signKey; //加密秘钥
protected $errors;
public function __construct($merchant,$operatorId,$appId,$backurl,$signKey)
{
$this->merchant = $merchant;
$this->operatorId = $operatorId;
$this->appId = $appId;
$this->backurl = $backurl;
$this->signKey = $signKey;
}
/**
* 发起支付信息
*/
public function pay($orderSn, $amount, $openId)
{
$wechatOrder = [
'action' => 'MAPPPAY',
'version' => 1.1,
'merchant_id' => $this->merchant,
'data' => [
'order_id' => $orderSn,
'operator_id' => $this->operatorId,
'amount' => strval(($amount * 100)),
'biz_type' => 'WECHATPAY',
'app_id' => $this->appId,
'open_id' => $openId,
'call_back_url' => $this->backurl,
],
];
$sign = $this->enctry($wechatOrder, $this->signKey);
$wechatOrder["data"] = $sign['basesign'];
$wechatOrder["md5"] = $sign['sign'];
$resDate = $this->httpsClient($wechatOrder);
if ($resDate == "") {
return [];
}
return $this->decrypt($resDate);
}
/**
* 发起支付信息
*/
public function query($orderSn)
{
$wechatOrder = [
'action' => 'REFUND',
'version' => 1.0,
'merchant_id' => $this->merchant,
'data' => [
'order_id' => $orderSn,
],
];
$sign = $this->enctry($wechatOrder, $this->signKey);
$wechatOrder["data"] = $sign['basesign'];
$wechatOrder["md5"] = $sign['sign'];
$resDate = $this->httpsClient($wechatOrder);
if ($resDate == "") {
return [];
}
return $this->decrypt($resDate);
}
/**
* 发起退款
*/
public function refund($orderId, $oriOrderId, $refundAmt)
{
$wechatOrder = [
'action' => 'REFUND',
'version' => 1.0,
'merchant_id' => $this->merchant,
'data' => [
'order_id' => $orderId,
'ori_order_id' => $oriOrderId,
'refund_amt' => $refundAmt,
],
];
$sign = $this->enctry($wechatOrder, $this->signKey);
$wechatOrder["data"] = $sign['basesign'];
$wechatOrder["md5"] = $sign['sign'];
$resDate = $this->httpsClient($wechatOrder);
if ($resDate == "") {
return [];
}
return $this->decrypt($resDate);
}
/**
* @param 加密信息
*/
private function enctry($data, $key)
{
$data_array = $data['data'];
$temp_data_array = $data_array;
ksort($temp_data_array);
$data_str = implode(array_values($temp_data_array));
$data_md5 = strtoupper(md5($data_str));
$user_key = $key; //using your Sign Key provided by OTTPAY;
$aesKeyStr = strtoupper(substr(md5($data_md5.$user_key), 8, 16));
$data_json = json_encode($data_array);
$aes = openssl_encrypt($data_json, 'AES-128-ECB', $aesKeyStr, OPENSSL_RAW_DATA);
$basesign = base64_encode($aes);
return ['sign' => $data_md5, 'basesign' => $basesign];
// $newdata = $data["data"];
// ksort($newdata);
// $str = implode(array_values($newdata));
// $signs = md5($str);
// $signs = strtoupper($signs);
// var_dump($signs);
// $sign = strtoupper($signs . $key);
// var_dump($sign);
// var_dump(strtoupper(md5($sign)));
// $key = strtoupper(substr(strtoupper(md5($sign)), 8, 16));
// var_dump($key);
// $ordata = json_encode($newdata);
// var_dump($ordata);
// $aes = openssl_encrypt($ordata, 'AES-128-ECB', $key, OPENSSL_RAW_DATA);
// $basesign = base64_encode($aes);
// return ['sign' => $signs, 'basesign' => $basesign];
}
/**
* @param 解密信息
*/
private function decrypt($response)
{
$responseArr = json_decode($response, true);
if ($responseArr['rsp_code'] == "FAIL") {
$this->errors = $responseArr['rsp_msg'];
return [];
}
$signjm = strtoupper($responseArr["md5"] . $this->signKey);
$keyjm = strtoupper(substr(md5($signjm), 8, 16));
$d = base64_decode($responseArr['data']);
$responseDecr = openssl_decrypt($d, 'AES-128-ECB', $keyjm, OPENSSL_RAW_DATA);
return json_decode($responseDecr, true);
}
/**
* @param 获取错误信息
*/
public function getError()
{
return $this->errors;
}
private function httpsClient($data)
{
$dataHttp = json_encode($data, JSON_UNESCAPED_UNICODE);
$url = "https://frontapi.ottpay.com:443/processV2";
$header[] = "Content-type: application/json;charset=utf-8";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 兼容本地没有指定curl.cainfo路径的错误
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $dataHttp);
$ret = curl_exec($ch);
$err = curl_error($ch);
if (false === $ret || !empty($err)) {
// $errno = curl_errno($ch);d
// $info = curl_getinfo($ch);
curl_close($ch);
return "";
}
curl_close($ch);
return $ret;
}
}
?>
OTTPay对接微信小程序
该PHP类用于处理OTTPay的支付和退款操作,包括发起支付、查询订单状态和发起退款。它使用AES-128-ECB加密方法和MD5签名进行数据安全传输,并通过HTTPS发送请求到OTTPay的API接口。
摘要由CSDN通过智能技术生成