RoutineService.php 4.5 KB

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