TonglianPayment.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. namespace app\admin\server\payment;
  3. class TonglianPayment
  4. {
  5. public static $url = 'https://vsp.allinpay.com/apiweb/unitorder';
  6. public static function createOrder($trxamt, $orderid, $pay_type = 'W01' ,$open_id = '')
  7. {
  8. $param = array(
  9. 'cusid' => static::_getConfig('cusid'),//商户号
  10. 'appid' => static::_getConfig('appid'),//平台分配ID
  11. 'version' => static::_getConfig('version'),//版本
  12. 'paytype' => $pay_type, //交易方式 W01=微信扫码 A01=支付宝扫码
  13. 'notify_url' => static::_getConfig('notify_url'),//异步通知地址
  14. 'trxamt' => $trxamt, //付款金额
  15. 'reqsn' => $orderid, //订单编号
  16. 'randomstr' => time(),
  17. 'signtype' => 'RSA', //加密方式
  18. );
  19. if($pay_type == 'W06'){
  20. $param['acct'] = $open_id;
  21. $param['sub_appid'] = static::_getConfig('sub_appid');//小程序appid
  22. }
  23. $param['sign'] = urlencode(static::Sign($param));
  24. $paramsStr = static::ToUrlParams($param);
  25. $url = static::$url . '/pay';
  26. $rsp = static::request($url, $paramsStr);
  27. $result = json_decode($rsp, true);
  28. if($result['retcode'] != 'SUCCESS')
  29. {
  30. throw new \Exception($result['retmsg'], 500);
  31. }
  32. return $result;
  33. }
  34. //交易撤销(当天)
  35. public static function cancel($trxamt, $orderid)
  36. {
  37. $param = array(
  38. 'cusid' => static::_getConfig('cusid'),//商户号
  39. 'appid' => static::_getConfig('appid'),//平台分配ID
  40. 'version' => static::_getConfig('version'),//版本
  41. 'trxamt' => $trxamt, //付款金额
  42. 'reqsn' => $orderid.'TK', //退款订单编号
  43. 'oldreqsn' => $orderid, //订单编号
  44. 'randomstr' => time(),
  45. 'signtype' => 'RSA', //加密方式
  46. );
  47. $param['sign'] = urlencode(static::Sign($param));
  48. $paramsStr = static::ToUrlParams($param);
  49. $url = 'https://vsp.allinpay.com/apiweb/tranx/cancel';
  50. $rsp = static::request($url, $paramsStr);
  51. $result = json_decode($rsp, true);
  52. if($result['retcode'] != 'SUCCESS')
  53. {
  54. throw new \Exception($result['retmsg'], 500);
  55. }
  56. return $result;
  57. }
  58. //交易退款(隔天)
  59. public static function refund($trxamt, $orderid)
  60. {
  61. $param = array(
  62. 'cusid' => static::_getConfig('cusid'),//商户号
  63. 'appid' => static::_getConfig('appid'),//平台分配ID
  64. 'version' => static::_getConfig('version'),//版本
  65. 'trxamt' => $trxamt, //付款金额
  66. 'reqsn' => $orderid, //退款订单编号
  67. 'oldreqsn' => $orderid, //订单编号
  68. 'randomstr' => time(),
  69. 'signtype' => 'RSA', //加密方式
  70. );
  71. $param['sign'] = urlencode(static::Sign($param));
  72. $paramsStr = static::ToUrlParams($param);
  73. $url = 'https://vsp.allinpay.com/apiweb/tranx/refund';
  74. $rsp = static::request($url, $paramsStr);
  75. $result = json_decode($rsp, true);
  76. if($result['retcode'] != 'SUCCESS')
  77. {
  78. throw new \Exception($result['retmsg'], 500);
  79. }
  80. return $result;
  81. }
  82. /**
  83. * Notes:RSA签名
  84. * @param array $array
  85. * @return string
  86. * @throws \Exception
  87. * User: yym
  88. * Date: 2022/9/2
  89. */
  90. public static function Sign(array $array){
  91. ksort($array);
  92. $bufSignSrc = static::ToUrlParams($array);
  93. $private_key = static::_getConfig('private_key');
  94. $private_key = chunk_split($private_key , 64, "\n");
  95. $key = "-----BEGIN RSA PRIVATE KEY-----\n".wordwrap($private_key)."-----END RSA PRIVATE KEY-----";
  96. // echo $key;
  97. if(openssl_sign($bufSignSrc, $signature, $key )){
  98. }else{
  99. throw new \Exception('签名生成失败:sign fail');
  100. }
  101. $sign = base64_encode($signature);//加密后的内容通常含有特殊字符,需要编码转换下,在网络间通过url传输时要注意base64编码是否是url安全的
  102. return $sign;
  103. }
  104. /**
  105. * Notes:排序生成
  106. * @param array $array
  107. * @return string
  108. * User: yym
  109. * Date: 2022/9/2
  110. */
  111. public static function ToUrlParams(array $array)
  112. {
  113. $buff = "";
  114. foreach ($array as $k => $v)
  115. {
  116. if($v != "" && !is_array($v)){
  117. $buff .= $k . "=" . $v . "&";
  118. }
  119. }
  120. $buff = trim($buff, "&");
  121. return $buff;
  122. }
  123. /**
  124. * Notes:效验签名
  125. * @param array $array
  126. * @return int
  127. * User: yym
  128. * Date: 2022/9/2
  129. */
  130. public static function ValidSign(array $array){
  131. $sign = $array['sign'];
  132. unset($array['sign']);
  133. ksort($array);
  134. $bufSignSrc = static::ToUrlParams($array);
  135. //$public_key = static::_getConfig('public_key');
  136. $public_key = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCm9OV6zH5DYH/ZnAVYHscEELdCNfNTHGuBv1nYYEY9FrOzE0/4kLl9f7Y9dkWHlc2ocDwbrFSm0Vqz0q2rJPxXUYBCQl5yW3jzuKSXif7q1yOwkFVtJXvuhf5WRy+1X5FOFoMvS7538No0RpnLzmNi3ktmiqmhpcY/1pmt20FHQQIDAQAB';
  137. $public_key = chunk_split($public_key , 64, "\n");
  138. $key = "-----BEGIN PUBLIC KEY-----\n$public_key-----END PUBLIC KEY-----\n";
  139. $result= openssl_verify($bufSignSrc,base64_decode($sign), $key );
  140. return $result;
  141. }
  142. /**
  143. * @desc: 获取配置文件
  144. * @param string $key
  145. * @return array|string
  146. */
  147. private static function _getConfig(string $key = '')
  148. {
  149. $config = config('app.tonglian_payment');
  150. if (empty($config)) {
  151. throw new \Exception('payment配置文件不存在');
  152. }
  153. if(!empty($key))
  154. {
  155. return $config[$key] ?? '';
  156. }
  157. return $config;
  158. }
  159. /**
  160. * Notes:发送请求
  161. * @param $url
  162. * @param $params
  163. * @return bool|string
  164. * User: yym
  165. * Date: 2022/9/2
  166. */
  167. public static function request($url,$params){
  168. $ch = curl_init();
  169. $this_header = array("content-type: application/x-www-form-urlencoded;charset=UTF-8");
  170. curl_setopt($ch,CURLOPT_HTTPHEADER,$this_header);
  171. curl_setopt($ch, CURLOPT_URL, $url);
  172. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  173. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
  174. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  175. curl_setopt($ch, CURLOPT_POST, 1);
  176. curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
  177. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);//如果不加验证,就设false,商户自行处理
  178. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
  179. $output = curl_exec($ch);
  180. curl_close($ch);
  181. return $output;
  182. }
  183. }