Transfer.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace addons\ddrive\library;
  3. class Transfer
  4. {
  5. /**
  6. * 关于微信企业付款的说明
  7. * 1.微信企业付款要求必传证书,需要到https://pay.weixin.qq.com 账户中心->账户设置->API安全->下载证书,证书路径在第207行和210行修改
  8. * 2.错误码参照 :https://pay.weixin.qq.com/wiki/doc/api/tools/mch_pay.php?chapter=14_2
  9. */
  10. /**
  11. * 企业付款
  12. * @param string $platform openid所属平台,media_platform:公众号,mini_program:小程序
  13. * @param string $openid 调用【网页授权获取用户信息】接口获取到用户在该公众号下的Openid
  14. * @param float $totalFee 收款总费用 单位元
  15. * @param string $outTradeNo 唯一的订单号
  16. * @param string $desc 企业付款操作说明信息
  17. * @param string $trueName 用户真实姓名,不传递则不校验
  18. * @return boolean
  19. */
  20. public static function pay($platform, $openid, $totalFee, $outTradeNo, $desc = "付款", $trueName = "")
  21. {
  22. $epay = get_addon_info('epay');
  23. if (!$epay) {
  24. throw new \Exception("请安装[微信支付宝整合-epay]插件");
  25. }
  26. $config = get_addon_config('epay')['wechat'];
  27. if ($platform == 'media_platform') {
  28. $appid = $config['app_id'];
  29. }
  30. if ($platform == 'mini_program') {
  31. $appid = $config['miniapp_id'];
  32. }
  33. $unified = array(
  34. 'mch_appid' => $appid,
  35. 'mchid' => $config['mch_id'],
  36. 'nonce_str' => self::createNonceStr(),
  37. 'openid' => $openid,
  38. 'check_name' => 'NO_CHECK', //校验用户姓名选项。NO_CHECK:不校验真实姓名,FORCE_CHECK:强校验真实姓名
  39. 'partner_trade_no' => $outTradeNo,
  40. 'spbill_create_ip' => '127.0.0.1',
  41. 'amount' => intval($totalFee * 100), //单位 转为分
  42. 'desc' => $desc,
  43. );
  44. if ($trueName) {
  45. $unified['check_name'] = 'FORCE_CHECK';
  46. $unified['re_user_name'] = 'trueName';
  47. }
  48. $unified['sign'] = self::getSign($unified, $config['key']);
  49. $responseXml = \fast\Http::post('https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers', self::arrayToXml($unified), [
  50. CURLOPT_SSLCERTTYPE => 'PEM',
  51. CURLOPT_SSLCERT => ADDON_PATH . $config['cert_client'],
  52. CURLOPT_SSLKEYTYPE => 'PEM',
  53. CURLOPT_SSLKEY => ADDON_PATH . $config['cert_key'],
  54. ]);
  55. $unifiedOrder = simplexml_load_string($responseXml, 'SimpleXMLElement', LIBXML_NOCDATA);
  56. if ($unifiedOrder === false) {
  57. throw new \Exception("parse xml error");
  58. }
  59. if ($unifiedOrder->return_code != 'SUCCESS') {
  60. throw new \Exception($unifiedOrder->return_msg);
  61. }
  62. if ($unifiedOrder->result_code != 'SUCCESS') {
  63. throw new \Exception($unifiedOrder->err_code_des);
  64. }
  65. return true;
  66. }
  67. public static function createNonceStr($length = 16)
  68. {
  69. $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
  70. $str = '';
  71. for ($i = 0; $i < $length; $i++) {
  72. $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
  73. }
  74. return $str;
  75. }
  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. }
  86. $xml .= "</xml>";
  87. return $xml;
  88. }
  89. public static function getSign($params, $key)
  90. {
  91. ksort($params, SORT_STRING);
  92. $unSignParaString = self::formatQueryParaMap($params, false);
  93. $signStr = strtoupper(md5($unSignParaString . "&key=" . $key));
  94. return $signStr;
  95. }
  96. protected static function formatQueryParaMap($paraMap, $urlEncode = false)
  97. {
  98. $buff = "";
  99. ksort($paraMap);
  100. foreach ($paraMap as $k => $v) {
  101. if (null != $v && "null" != $v) {
  102. if ($urlEncode) {
  103. $v = urlencode($v);
  104. }
  105. $buff .= $k . "=" . $v . "&";
  106. }
  107. }
  108. $reqPar = '';
  109. if (strlen($buff) > 0) {
  110. $reqPar = substr($buff, 0, strlen($buff) - 1);
  111. }
  112. return $reqPar;
  113. }
  114. }