des.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. <?php
  2. use think\Request;
  3. class kdpay
  4. {
  5. function oil_fee_jsapi_get($body, $orderid, $real_amount, $return_url = '', $notify_url = '')
  6. {
  7. $randsi = date("YmdHis") . date("YmdHis") . rand(1000, 9999);
  8. $array =
  9. [
  10. "mch_id" => 'KP10008973',
  11. "timestamp" => time(),
  12. "nonce_str" => $randsi,
  13. "sign_type" => 'MD5',
  14. "content" => json_encode([
  15. "client_ip" => Request::instance()->ip(),
  16. "out_trade_no" => $orderid,
  17. "fee" => $real_amount * 100,//0.01
  18. "body" => '南京倾枫充值',
  19. "open_id" => '',
  20. "notify_url" => $notify_url,
  21. "return_url" => $return_url,
  22. ]),
  23. ];
  24. foreach (array_filter($array) as $k => $v) {
  25. $Parameters[$k] = $v;
  26. }
  27. //签名步骤一:按字典序排序参数
  28. ksort($Parameters);
  29. $buff = "";
  30. ksort($Parameters);
  31. foreach ($Parameters as $k => $v) {
  32. $buff .= $k . "=" . $v . "&";
  33. }
  34. $String = '';
  35. if (strlen($buff) > 0) {
  36. $String = substr($buff, 0, strlen($buff) - 1);
  37. }
  38. $String = $String . "&secret_key=" . 'psVr7sLJHNLNK7XEQrPKNBszbQaeHko6';
  39. //签名步骤三:MD5加密
  40. $String = md5($String);
  41. //签名步骤四:所有字符转为大写
  42. $strings = strtoupper($String);
  43. $array['sign'] = $strings;
  44. $res = json_decode(self::http_post('https://pay.kudianvip.com/openapi/pay/kdh5pay', json_encode($array)), true);
  45. if ($res['code'] != 0) {
  46. return '错误信息为:' . $res['msg'];
  47. }
  48. //返回参数格式设计
  49. if (!empty($res['result'])) {
  50. $res_data = json_decode($res['result'], true);
  51. if (!empty($res_data)) {
  52. $rs = $res_data['url'];
  53. return $rs;
  54. } else {
  55. return '错误信息为:' . $res['msg'];
  56. }
  57. } else {
  58. return '错误信息为:' . $res['msg'];
  59. }
  60. }
  61. /**
  62. * 酷点扫码支付
  63. * @param $body
  64. * @param $orderid
  65. * @param $real_amount
  66. * @param string $return_url
  67. * @param string $notify_url
  68. * @return mixed|string
  69. */
  70. function oil_fee_sm_get($body, $orderid, $real_amount, $return_url = '', $notify_url = '')
  71. {
  72. $randsi = date("YmdHis") . date("YmdHis") . rand(1000, 9999);
  73. $array =
  74. [
  75. "mch_id" => 'KP10008973',
  76. "timestamp" => time(),
  77. "nonce_str" => $randsi,
  78. "sign_type" => 'MD5',
  79. "content" => json_encode([
  80. "payment" => 'WXPAY',
  81. 'trade_type' => 'SCANCODE',
  82. "client_ip" => Request::instance()->ip(),
  83. "out_trade_no" => $orderid,
  84. "fee" => $real_amount * 100,//0.01
  85. "body" => '南京倾枫充值',
  86. "open_id" => '',
  87. "notify_url" => $notify_url,
  88. "sence_info" => [
  89. "return_url" => $return_url,
  90. ]
  91. // "return_url"=>$return_url,
  92. ]),
  93. ];
  94. foreach (array_filter($array) as $k => $v) {
  95. $Parameters[$k] = $v;
  96. }
  97. //签名步骤一:按字典序排序参数
  98. ksort($Parameters);
  99. $buff = "";
  100. ksort($Parameters);
  101. foreach ($Parameters as $k => $v) {
  102. $buff .= $k . "=" . $v . "&";
  103. }
  104. $String = '';
  105. if (strlen($buff) > 0) {
  106. $String = substr($buff, 0, strlen($buff) - 1);
  107. }
  108. $String = $String . "&secret_key=" . 'psVr7sLJHNLNK7XEQrPKNBszbQaeHko6';
  109. //签名步骤三:MD5加密
  110. $String = md5($String);
  111. //签名步骤四:所有字符转为大写
  112. $strings = strtoupper($String);
  113. $array['sign'] = $strings;
  114. $res = json_decode(self::http_post('https://pay.kudianvip.com/openapi/pay/unifiedpay', json_encode($array)), true);
  115. if ($res['code'] != 0) {
  116. return '错误信息为:' . $res['msg'];
  117. }
  118. //返回参数格式设计
  119. if (!empty($res['result'])) {
  120. $res_data = json_decode($res['result'], true);
  121. if (!empty($res_data) && $res_data['status'] == 0) {
  122. $rs = $res_data['pay_info']['url'];
  123. return $rs;
  124. } else {
  125. return '错误信息为:' . $res['msg'];
  126. }
  127. } else {
  128. return '错误信息为:' . $res['msg'];
  129. }
  130. }
  131. function notifyVerify($data)
  132. {
  133. unset($data['sign']);
  134. foreach ($data as $k => $v) {
  135. $Parameters[$k] = $v;
  136. }
  137. //签名步骤一:按字典序排序参数
  138. ksort($Parameters);
  139. $buff = "";
  140. ksort($Parameters);
  141. foreach ($Parameters as $k => $v) {
  142. $buff .= $k . "=" . $v . "&";
  143. }
  144. $String = '';
  145. if (strlen($buff) > 0) {
  146. $String = substr($buff, 0, strlen($buff) - 1);
  147. }
  148. $String = $String . "&secret_key=" . 'psVr7sLJHNLNK7XEQrPKNBszbQaeHko6';
  149. //签名步骤三:MD5加密
  150. $String = md5($String);
  151. //签名步骤四:所有字符转为大写
  152. $strings = strtoupper($String);
  153. return $strings;
  154. }
  155. public function queryOrder($order_no)
  156. {
  157. $randsi = date("YmdHis") . date("YmdHis") . rand(1000, 9999);
  158. $array =
  159. [
  160. "mch_id" => 'KP10008973',
  161. "timestamp" => time(),
  162. "nonce_str" => $randsi,
  163. "sign_type" => 'MD5',
  164. "content" => json_encode([
  165. "out_trade_no" => $order_no,
  166. ]),
  167. ];
  168. foreach (array_filter($array) as $k => $v) {
  169. $Parameters[$k] = $v;
  170. }
  171. //签名步骤一:按字典序排序参数
  172. ksort($Parameters);
  173. $buff = "";
  174. ksort($Parameters);
  175. foreach ($Parameters as $k => $v) {
  176. $buff .= $k . "=" . $v . "&";
  177. }
  178. $String = '';
  179. if (strlen($buff) > 0) {
  180. $String = substr($buff, 0, strlen($buff) - 1);
  181. }
  182. $String = $String . "&secret_key=" . 'psVr7sLJHNLNK7XEQrPKNBszbQaeHko6';
  183. //签名步骤三:MD5加密
  184. $String = md5($String);
  185. //签名步骤四:所有字符转为大写
  186. $strings = strtoupper($String);
  187. $array['sign'] = $strings;
  188. $res = json_decode(self::http_post('https://pay.kudianvip.com/openapi/pay/query', json_encode($array)), true);
  189. $res_data = json_decode($res, true);
  190. //对响应结果进行验签,具体见下面的验签示例
  191. if ($res_data['msg'] == 'SUCCESS') {
  192. return $res_data;
  193. echo '验签成功';
  194. } else {
  195. echo '验签失败';
  196. return false;
  197. }
  198. }
  199. /**
  200. * 使用http_post提交
  201. * @param $url
  202. * @param $params
  203. * @param bool $contentType
  204. * @return bool|mixed|string
  205. */
  206. function GetRandStr($length)
  207. {
  208. //字符组合
  209. $str = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  210. $len = strlen($str) - 1;
  211. $randstr = '';
  212. for ($i = 0; $i < $length; $i++) {
  213. $num = mt_rand(0, $len);
  214. $randstr .= $str[$num];
  215. }
  216. return $randstr;
  217. }
  218. /**
  219. * 使用http_post提交
  220. * @param $url
  221. * @param $params
  222. * @param bool $contentType
  223. * @return bool|mixed|string
  224. */
  225. protected function http_post($url, $params, $contentType = true)
  226. {
  227. if (function_exists('curl_init')) {
  228. // curl方式
  229. $oCurl = curl_init();
  230. if (stripos($url, 'https://') !== FALSE) {
  231. curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
  232. curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
  233. }
  234. $string = $params;
  235. if (is_array($params)) {
  236. $aPOST = array();
  237. foreach ($params as $key => $val) {
  238. $aPOST[] = $key . '=' . urlencode($val);
  239. }
  240. $string = join('&', $aPOST);
  241. }
  242. curl_setopt($oCurl, CURLOPT_URL, $url);
  243. curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
  244. curl_setopt($oCurl, CURLOPT_POST, TRUE);
  245. //$contentType json处理
  246. if ($contentType) {
  247. $headers = array(
  248. "Content-type: application/json;charset='utf-8'",
  249. );
  250. curl_setopt($oCurl, CURLOPT_HTTPHEADER, $headers);
  251. // echo $params;
  252. curl_setopt($oCurl, CURLOPT_POSTFIELDS, $params);
  253. } else {
  254. curl_setopt($oCurl, CURLOPT_POSTFIELDS, $string);
  255. }
  256. $response = curl_exec($oCurl);
  257. curl_close($oCurl);
  258. return $response;
  259. } elseif (function_exists('stream_context_create')) {
  260. // php5.3以上
  261. $opts = array(
  262. 'http' => array(
  263. 'method' => 'POST',
  264. 'header' => 'Content-type: application/x-www-form-urlencoded',
  265. 'content' => http_build_query($params),
  266. )
  267. );
  268. $_opts = stream_context_get_params(stream_context_get_default());
  269. $context = stream_context_create(array_merge_recursive($_opts['options'], $opts));
  270. return file_get_contents($url, false, $context);
  271. } else {
  272. return FALSE;
  273. }
  274. }
  275. }