Kjpay.class.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. include "getSign.php";
  3. use think\Request;
  4. class Kjpay
  5. {
  6. private $payUrl = 'http://api.kj-pay.com/wechar/wap_pay';
  7. private $queryUrl = 'http://api.kj-pay.com/wechar/query_pay';
  8. private $merchant_no = ''; //商户号
  9. private $key = ""; //密钥
  10. private $scanPayUrl = 'http://api.kj-pay.com/wechar/scan_pay';
  11. public function pay($title, $order_no, $pay_amount,$return_url='',$notify_url=''){
  12. //请求的参数(注:不同支付接口请求参数略有不同,具体请参照接口文档)
  13. //分账逻辑处理
  14. $sub_amount = floatval(intval($pay_amount * 100 * 0.295)/100);
  15. $receivers = array();
  16. if($sub_amount){
  17. $receiver = array();
  18. $receiver['type'] = 'PERSONAL_OPENID';
  19. $receiver['account'] = '';
  20. $receiver['name'] = '';
  21. $receiver['amount'] = $sub_amount;
  22. $receiver['description'] = '分到个人';
  23. $receivers[] = $receiver;
  24. }
  25. if($receivers){
  26. $param = array(
  27. 'merchant_no' => $this->merchant_no,
  28. 'merchant_order_no' => $order_no,
  29. //异步通知的地址
  30. 'notify_url' => $notify_url,
  31. //同步跳转的地址
  32. //'return_url' => $return_url,
  33. 'start_time' => date('YmdHis'),
  34. 'trade_amount' => $pay_amount,
  35. 'goods_name' => $title,
  36. 'goods_desc' => $title,
  37. 'user_ip' => Request::instance()->ip(),
  38. 'pay_sence' => '{"type":"Wap","wap_url":"http://www.weilongwl.com","wap_name":"麻花网络"}',
  39. 'receivers' => json_encode($receivers),
  40. 'sign_type' => 1
  41. );
  42. }
  43. else{
  44. $param = array(
  45. 'merchant_no' => $this->merchant_no,
  46. 'merchant_order_no' => $order_no,
  47. //异步通知的地址
  48. 'notify_url' => $notify_url,
  49. //同步跳转的地址
  50. //'return_url' => $return_url,
  51. 'start_time' => date('YmdHis'),
  52. 'trade_amount' => $pay_amount,
  53. 'goods_name' => $title,
  54. 'goods_desc' => $title,
  55. 'user_ip' => Request::instance()->ip(),
  56. 'pay_sence' => '{"type":"Wap","wap_url":"http://www.weilongwl.com","wap_name":"麻花网络"}',
  57. 'sign_type' => 1
  58. );
  59. }
  60. //签名
  61. $sign = local_sign($param, $this->key);
  62. $param['sign'] = $sign;
  63. $res = getdata($this->payUrl, $param);
  64. $list = json_decode($res, true);
  65. /*
  66. echo "<pre>";
  67. print_r($list);
  68. echo "<pre>";
  69. */
  70. //对响应结果进行验签,具体见下面的验签示例
  71. if (local_sign($list['data'], $this->key) == $list['data']['sign']) {
  72. // echo '响应结果验签成功,请继续您的业务层操作';
  73. return $list;
  74. } else {
  75. // echo '验签失败';
  76. return false;
  77. }
  78. }
  79. public function scanpay($title, $order_no, $pay_amount,$return_url='',$notify_url=''){
  80. //请求的参数(注:不同支付接口请求参数略有不同,具体请参照接口文档)
  81. //分账逻辑处理
  82. $sub_amount = floatval(intval($pay_amount * 100 * 0.295)/100);
  83. $receivers = array();
  84. if($sub_amount){
  85. $receiver = array();
  86. $receiver['type'] = 'PERSONAL_OPENID';
  87. $receiver['account'] = 'oSepO0l1J_2CyAj-6jebvQov-krk';
  88. $receiver['name'] = '郑坦';
  89. $receiver['amount'] = $sub_amount;
  90. $receiver['description'] = '分到个人';
  91. $receivers[] = $receiver;
  92. }
  93. if($receivers){
  94. $param = array(
  95. 'merchant_no' => $this->merchant_no,
  96. 'merchant_order_no' => $order_no,
  97. //异步通知的地址
  98. 'notify_url' => $notify_url,
  99. //同步跳转的地址
  100. // 'return_url' => $return_url,
  101. 'start_time' => date('YmdHis'),
  102. 'trade_amount' => $pay_amount,
  103. 'goods_name' => $title,
  104. 'goods_desc' => $title,
  105. 'user_ip' => Request::instance()->ip(),
  106. 'pay_type' => 2,
  107. 'receivers' => json_encode($receivers),
  108. 'sign_type' => 1
  109. );
  110. }
  111. else{
  112. $param = array(
  113. 'merchant_no' => $this->merchant_no,
  114. 'merchant_order_no' => $order_no,
  115. //异步通知的地址
  116. 'notify_url' => $notify_url,
  117. //同步跳转的地址
  118. // 'return_url' => $return_url,
  119. 'start_time' => date('YmdHis'),
  120. 'trade_amount' => $pay_amount,
  121. 'goods_name' => $title,
  122. 'goods_desc' => $title,
  123. 'user_ip' => Request::instance()->ip(),
  124. 'pay_type' => 2,
  125. 'sign_type' => 1
  126. );
  127. }
  128. //签名
  129. $sign = local_sign($param, $this->key);
  130. $param['sign'] = $sign;
  131. $res = getdata($this->scanPayUrl, $param);
  132. $list = json_decode($res, true);
  133. /*
  134. echo "<pre>";
  135. print_r($list);
  136. echo "<pre>";
  137. */
  138. //对响应结果进行验签,具体见下面的验签示例
  139. if (local_sign($list['data'], $this->key) == $list['data']['sign']) {
  140. // echo '响应结果验签成功,请继续您的业务层操作';
  141. return $list;
  142. } else {
  143. // echo '验签失败';
  144. return false;
  145. }
  146. }
  147. public function notifyVerify($data){
  148. //签名
  149. $sign = local_sign($data, $this->key);
  150. if ($sign == $data['sign']){
  151. return $data;
  152. } else {
  153. return false;
  154. }
  155. }
  156. public function queryOrder($order_no)
  157. {
  158. //请求的参数(注:不同支付接口请求参数略有不同,具体请参照接口文档)
  159. $param = array(
  160. 'merchant_no' => $this->merchant_no,
  161. 'trade_no' => $order_no,
  162. 'sign_type' => 1
  163. );
  164. //签名
  165. $sign = local_sign($param, $this->key);
  166. $param['sign'] = $sign;
  167. $res = getdata($this->queryUrl, $param);
  168. $list = json_decode($res, true);
  169. /*
  170. echo "<pre>";
  171. print_r($list);
  172. echo "<pre>";
  173. */
  174. //对响应结果进行验签,具体见下面的验签示例
  175. if (isset($list['data']['sign']) && local_sign($list['data'], $this->key) == $list['data']['sign']) {
  176. return $res;
  177. echo '验签成功';
  178. } else {
  179. echo '验签失败';
  180. return false;
  181. }
  182. }
  183. }