RoutineService.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace service;
  3. use think\Url;
  4. use service\HttpService;
  5. /**
  6. * 小程序支付
  7. * Created by PhpStorm.
  8. * User: Administrator
  9. * Date: 2018/5/29 0029
  10. * Time: 上午 10:10
  11. */
  12. class RoutineService{
  13. /**
  14. * @param $openid $openid 用户openid
  15. * @param $fee $fee 金额
  16. * @param $out_trade_no $out_trade_no 订单号
  17. * @param $body $body 提示
  18. * @return mixed
  19. */
  20. public static function payRoutine($openid,$out_trade_no,$fee,$attach,$body){
  21. $payment = SystemConfigService::more(['site_url','routine_appId','routine_appsecret','pay_routine_mchid','pay_routine_client_cert','pay_routine_client_key','pay_routine_key','pay_weixin_open']);
  22. $config = array(
  23. 'appid' => $payment['routine_appId'],
  24. 'mch_id' => $payment['pay_routine_mchid'],
  25. 'key' => $payment['pay_routine_key'],
  26. );
  27. $unified = array(
  28. 'appid' => $config['appid'],
  29. 'attach' => $attach, //商家数据包,原样返回,如果填写中文,请注意转换为utf-8
  30. 'body' => $body,
  31. 'mch_id' => $config['mch_id'],
  32. 'nonce_str' => self::nonce_str(),//随机字符串
  33. 'notify_url' => $payment['site_url'].Url::build('routine/Routine/notify'),
  34. 'openid' => $openid,
  35. 'out_trade_no' => $out_trade_no,
  36. 'spbill_create_ip' => self::get_server_ip()?:'127.0.0.1',//终端的ip
  37. 'total_fee' => $fee*100, //单位 转为分
  38. 'trade_type' => 'JSAPI'//交易类型 默认
  39. );
  40. $unified['sign'] = self::getSign($unified, $config['key']);//签名
  41. $responseXml = HttpService::postRequest('https://api.mch.weixin.qq.com/pay/unifiedorder', self::arrayToXml($unified));
  42. $unifiedOrder = simplexml_load_string($responseXml, 'SimpleXMLElement', LIBXML_NOCDATA);
  43. if ($unifiedOrder === false) {
  44. die('parse xml error');
  45. }
  46. if ($unifiedOrder->return_code != 'SUCCESS') {
  47. die($unifiedOrder->return_msg);
  48. }
  49. if ($unifiedOrder->result_code != 'SUCCESS') {
  50. die($unifiedOrder->err_code);
  51. }
  52. $time = time();
  53. $arr = array(
  54. "appId" => $unifiedOrder->appid,
  55. "timeStamp" => "$time",
  56. "nonceStr" => self::nonce_str(),//随机字符串
  57. "package" => "prepay_id=" . $unifiedOrder->prepay_id,
  58. "signType" => 'MD5',
  59. );
  60. $arr['paySign'] = self::getSign($arr, $config['key']);
  61. return $arr;
  62. }
  63. //随机32位字符串
  64. public static function nonce_str(){
  65. $result = '';
  66. $str = 'QWERTYUIOPASDFGHJKLZXVBNMqwertyuioplkjhgfdsamnbvcxz';
  67. for ($i=0;$i<32;$i++){
  68. $result .= $str[rand(0,48)];
  69. }
  70. return $result;
  71. }
  72. //数组转XML
  73. public static function arrayToXml($arr)
  74. {
  75. $xml = "<xml>";
  76. foreach ($arr as $key => $val) {
  77. if (is_numeric($val)) {
  78. $xml .= "<" . $key . ">" . $val . "</" . $key . ">";
  79. } else
  80. $xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
  81. }
  82. $xml .= "</xml>";
  83. return $xml;
  84. }
  85. /**
  86. * 获取签名
  87. */
  88. public static function getSign($params, $key)
  89. {
  90. ksort($params, SORT_STRING);
  91. $unSignParaString = self::formatQueryParaMap($params, false);
  92. $signStr = strtoupper(md5($unSignParaString . "&key=" . $key));
  93. return $signStr;
  94. }
  95. protected static function formatQueryParaMap($paraMap, $urlEncode = false)
  96. {
  97. $buff = "";
  98. ksort($paraMap);
  99. foreach ($paraMap as $k => $v) {
  100. if (null != $v && "null" != $v) {
  101. if ($urlEncode) {
  102. $v = urlencode($v);
  103. }
  104. $buff .= $k . "=" . $v . "&";
  105. }
  106. }
  107. $reqPar = '';
  108. if (strlen($buff) > 0) {
  109. $reqPar = substr($buff, 0, strlen($buff) - 1);
  110. }
  111. return $reqPar;
  112. }
  113. //获取IP
  114. public static function get_server_ip() {
  115. if (isset($_SERVER)) {
  116. if($_SERVER['SERVER_ADDR']) {
  117. $server_ip = $_SERVER['SERVER_ADDR'];
  118. }
  119. else {
  120. $server_ip = $_SERVER['LOCAL_ADDR'];
  121. }
  122. }
  123. else {
  124. $server_ip = getenv('SERVER_ADDR');
  125. }
  126. return $server_ip;
  127. }
  128. }