| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- <?php
- include 'HTTPRequester.php';
- include 'Security.php';
- use think\Request;
- class Sumpay
- {
- private $url = 'https://api.top-ws.com/xjapigateway/form/gateway.htm';
- private $merchant_no = ''; //商户号
- private $md5Key = ""; //密钥
- public function pay($title, $order_no, $pay_amount,$return_url='',$notify_url=''){
- $service = "payonline.order.apply.h5"; // 请求的方法 service 值,根据实际值填写
- // 请求的业务参数,根据实际填写
- $goodsInfo = array();
- $goodsInfo['goodsName'] = $title;
- $goodsInfo['goodsAmount'] = $pay_amount;
- $goodsInfo['goodsNum'] = 1;
- $bizParam = array(
- "merNo" => $this->merchant_no,
- "bankCode" => 'WXPAY',
- "orderNo" => $order_no,
- "orderAmount" => $pay_amount,
- "notifyUrl" => $notify_url,
- "returnUrl" => $return_url,
- "goodsInfo" => $goodsInfo,
- );
- // 取业务参数的json串
- $serviceJson = json_encode($bizParam);
- // 对json进行AES加密
- $serviceParam = Security::encrypt($serviceJson, $this->md5Key);
- // 对AES后的参数 serviceParam 进行 md5 加签, 注意输出格式必须是字节形式
- $plainText = $serviceParam . $this->md5Key;
- $md5Res = md5($plainText, true);
- // 对md5后的结果进行 base64, 作为签名
- $sign = base64_encode($md5Res);
- // 作为post请求body的参数
- $param = array(
- "serviceParam" => $serviceParam,
- "sign" => $sign
- );
- $date = date("yymdHis");
- // 设置请求头
- $header = array(
- "requestId: " . $date . $this->uuid(),
- "timestamp: " . $date, // yyyyMMddHHmmss
- "version: " . "1.0",
- "service: " . $service,
- "appId: " . $this->merchant_no
- );
- $postRes = HTTPRequester::HTTPPost($this->url, $param, $header);
- $resJson = json_decode($postRes);
- $resServiceParam = $resJson->serviceParam;
- $resSign = $resJson->sign;
- // 校验响应签名
- $checkSign = $this->checkSign($resServiceParam, $this->md5Key, $resSign);
- // 签名通过方可认为请求正常
- if ($checkSign) {
- $resServiceJson = Security::decrypt($resServiceParam, $this->md5Key);
- $list = json_decode($resServiceJson, true);
- return $list;
- }
- else{
- return false;
- }
- }
- public function notifyVerify($data){
- $resServiceParam = $data['serviceParam'];
- $resSign = $data['sign'];
- // 校验响应签名
- $checkSign = $this->checkSign($resServiceParam, $this->md5Key, $resSign);
- // 签名通过方可认为请求正常
- if ($checkSign) {
- $resServiceJson = Security::decrypt($resServiceParam, $this->md5Key);
- $list = json_decode($resServiceJson, true);
- return $list;
- }
- else{
- return false;
- }
- }
- public function queryOrder($order_no)
- {
- $service = "payonline.order.query"; // 请求的方法 service 值,根据实际值填写
- // 请求的业务参数,根据实际填写
- $bizParam = array(
- "merNo" => $this->merchant_no,
- "orderNo" => $order_no,
- "transType" => "01",
- );
- // 取业务参数的json串
- $serviceJson = json_encode($bizParam);
- // 对json进行AES加密
- $serviceParam = Security::encrypt($serviceJson, $this->md5Key);
- // 对AES后的参数 serviceParam 进行 md5 加签, 注意输出格式必须是字节形式
- $plainText = $serviceParam . $this->md5Key;
- $md5Res = md5($plainText, true);
- // 对md5后的结果进行 base64, 作为签名
- $sign = base64_encode($md5Res);
- // 作为post请求body的参数
- $param = array(
- "serviceParam" => $serviceParam,
- "sign" => $sign
- );
- $date = date("yymdHis");
- // 设置请求头
- $header = array(
- "requestId: " . $date . $this->uuid(),
- "timestamp: " . $date, // yyyyMMddHHmmss
- "version: " . "1.0",
- "service: " . $service,
- "appId: " . $this->merchant_no
- );
- $postRes = HTTPRequester::HTTPPost($this->url, $param, $header);
- $resJson = json_decode($postRes);
- $resServiceParam = $resJson->serviceParam;
- $resSign = $resJson->sign;
- // 校验响应签名
- $checkSign = $this->checkSign($resServiceParam, $this->md5Key, $resSign);
- // 签名通过方可认为请求正常
- if ($checkSign) {
- $resServiceJson = Security::decrypt($resServiceParam, $this->md5Key);
- return $resServiceJson;
- }
- else{
- return false;
- }
- }
- function uuid($prefix = '')
- {
- $chars = md5(uniqid(mt_rand(), true));
- $uuid = substr($chars,0,8);
- $uuid .= substr($chars,8,4);
- $uuid .= substr($chars,12,4);
- $uuid .= substr($chars,16,1);
- return $prefix . $uuid;
- }
- function checkSign($serviceParam, $md5Key, $sign) {
- $md5Res = md5($serviceParam . $md5Key, true);
- return base64_encode($md5Res) === $sign;
- }
- }
|