WxPayConfig.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. /**
  3. *
  4. * example目录下为简单的支付样例,仅能用于搭建快速体验微信支付使用
  5. * 样例的作用仅限于指导如何使用sdk,在安全上面仅做了简单处理, 复制使用样例代码时请慎重
  6. * 请勿直接直接使用样例对外提供服务
  7. *
  8. **/
  9. namespace app\common\services\wechat\lib;
  10. use app\common\helpers\Url;
  11. use app\common\models\Store;
  12. use Yunshop\StoreCashier\store\models\StoreFaceSetting;
  13. /**
  14. *
  15. * 该类需要业务自己继承, 该类只是作为deamon使用
  16. * 实际部署时,请务必保管自己的商户密钥,证书等
  17. *
  18. */
  19. class WxPayConfig extends WxPayConfigInterface
  20. {
  21. public $set;
  22. public $sing_type;
  23. public function __construct()
  24. {
  25. $main_set = \Setting::get('shop.wechat_set');
  26. //独立设置
  27. if ($main_set['is_independent']) {
  28. $sub_set = StoreFaceSetting::where('store_id', request()->store_id)->first();
  29. $main_set['sub_appid'] = $sub_set->wx_sub_appid;
  30. $main_set['sub_mini_appid'] = $sub_set->wx_sub_mini_appid;
  31. $main_set['sub_mchid'] = $sub_set->wx_sub_mchid;
  32. if ($main_set['profit_sharing']) {
  33. $main_set['profit_sharing'] = !$sub_set->no_profit_sharing ?: 0;
  34. }
  35. }
  36. $this->set = $main_set;
  37. }
  38. public function getIndependent()
  39. {
  40. if (!$this->set['is_independent'] && $this->set['sub_appid']) {
  41. return true;
  42. }
  43. return false;
  44. }
  45. //=======【基本信息设置】=====================================
  46. /**
  47. *
  48. * 微信公众号信息配置
  49. *
  50. * APPID:绑定支付的APPID(必须配置,开户邮件中可查看)
  51. *
  52. * MCHID:商户号(必须配置,开户邮件中可查看)
  53. *
  54. */
  55. public function GetAppId()
  56. {
  57. if (\YunShop::request()->type == 2) {
  58. return $this->set['mini_appid'];
  59. }
  60. return $this->set['appid'];
  61. }
  62. public function GetSubAppId()
  63. {
  64. if (\YunShop::request()->type == 2 && !$this->set['is_independent'] && $this->set['sub_mini_appid']) {
  65. return $this->set['sub_mini_appid'];
  66. }
  67. return $this->set['sub_appid'];
  68. }
  69. public function GetMerchantId()
  70. {
  71. return $this->set['mchid'];
  72. }
  73. public function GetSubMerchantId()
  74. {
  75. return $this->set['sub_mchid'];
  76. }
  77. /**
  78. * 是否开启分账
  79. * @return string
  80. */
  81. public function GetProfitSharing()
  82. {
  83. return $this->set['profit_sharing'] ? 'Y' : 'N';
  84. }
  85. public function GetMchName()
  86. {
  87. return $this->set['mch_name'];
  88. }
  89. //=======【支付相关配置:支付成功回调地址/签名方式】===================================
  90. /**
  91. *
  92. * 签名和验证签名方式, 支持md5和sha256方式
  93. **/
  94. public function GetNotifyUrl()
  95. {
  96. return Url::shopSchemeUrl('payment/wechatscan/notifyUrl.php');;
  97. }
  98. public function GetSignType()
  99. {
  100. return $this->set['sign_type'] ?: "HMAC-SHA256";
  101. }
  102. public function SetSignType($sign_type)
  103. {
  104. $this->set['sign_type'] = $sign_type;
  105. }
  106. //=======【curl代理设置】===================================
  107. /**
  108. * TODO:这里设置代理机器,只有需要代理的时候才设置,不需要代理,请设置为0.0.0.0和0
  109. * 本例程通过curl使用HTTP POST方法,此处可修改代理服务器,
  110. * 默认CURL_PROXY_HOST=0.0.0.0和CURL_PROXY_PORT=0,此时不开启代理(如有需要才设置)
  111. * @var unknown_type
  112. */
  113. public function GetProxy(&$proxyHost, &$proxyPort)
  114. {
  115. $proxyHost = "0.0.0.0";
  116. $proxyPort = 0;
  117. }
  118. //=======【上报信息配置】===================================
  119. /**
  120. * TODO:接口调用上报等级,默认紧错误上报(注意:上报超时间为【1s】,上报无论成败【永不抛出异常】,
  121. * 不会影响接口调用流程),开启上报之后,方便微信监控请求调用的质量,建议至少开启错误上报。
  122. * 上报等级,0.关闭上报; 1.仅错误出错上报; 2.全量上报
  123. * @return int
  124. */
  125. public function GetReportLevenl()
  126. {
  127. return 0;
  128. }
  129. //=======【商户密钥信息-需要业务方继承】===================================
  130. /*
  131. * KEY:商户支付密钥,参考开户邮件设置(必须配置,登录商户平台自行设置), 请妥善保管, 避免密钥泄露
  132. * 设置地址:https://pay.weixin.qq.com/index.php/account/api_cert
  133. *
  134. * APPSECRET:公众帐号secert(仅JSAPI支付的时候需要配置, 登录公众平台,进入开发者中心可设置), 请妥善保管, 避免密钥泄露
  135. * 获取地址:https://mp.weixin.qq.com/advanced/advanced?action=dev&t=advanced/dev&token=2005451881&lang=zh_CN
  136. * @var string
  137. */
  138. public function GetKey()
  139. {
  140. return $this->set['apisecret'];
  141. }
  142. public function GetAppSecret()
  143. {
  144. return $this->set['secret'];
  145. }
  146. //=======【证书路径设置-需要业务方继承】=====================================
  147. /**
  148. * TODO:设置商户证书路径
  149. * 证书路径,注意应该填写绝对路径(仅退款、撤销订单时需要,可登录商户平台下载,
  150. * API证书下载地址:https://pay.weixin.qq.com/index.php/account/api_cert,下载之前需要安装商户操作证书)
  151. * 注意:
  152. * 1.证书文件不能放在web服务器虚拟目录,应放在有访问权限控制的目录中,防止被他人下载;
  153. * 2.建议将证书文件名改为复杂且不容易猜测的文件名;
  154. * 3.商户服务器要做好病毒和木马防护工作,不被非法侵入者窃取证书文件。
  155. * @var path
  156. * @var path
  157. */
  158. public function GetSSLCertPath(&$sslCertPath, &$sslKeyPath)
  159. {
  160. $sslCertPath = $this->set['weixin_cert'];
  161. $sslKeyPath = $this->set['weixin_key'];
  162. }
  163. public function getCertPath()
  164. {
  165. return $this->set['weixin_cert'];
  166. }
  167. public function getKeyPath()
  168. {
  169. return $this->set['weixin_key'];
  170. }
  171. public $MaxQueryRetry = 12;//最大查询重试次数
  172. public $QueryDuration = 5;//查询间隔
  173. }