NewMerchantMiniPayForm.php 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\modules\client\models\v1;
  8. use app\models\Option;
  9. use app\models\WechatConfig;
  10. use app\utils\Wechat\WechatProfit;
  11. use EasyWeChat\Factory;
  12. class NewMerchantMiniPayForm extends WechatProfit
  13. {
  14. public function getConfig($store_id = 0){
  15. $store_id = $store_id > 0 ? $store_id : get_store_id();
  16. if (is_profit_pay('wx', $store_id)) {
  17. $wechat_config = Option::getSaasWechat();
  18. // 证书
  19. if (!is_dir(\Yii::$app->runtimePath . '/pem')) {
  20. mkdir(\Yii::$app->runtimePath . '/pem');
  21. file_put_contents(\Yii::$app->runtimePath . '/pem/index.html', '');
  22. }
  23. $cert_pem_file = null;
  24. if ($wechat_config['sp_apiclient_cert']) {
  25. $cert_pem_file = \Yii::$app->runtimePath . '/pem/' . md5($wechat_config['sp_apiclient_cert']);
  26. if (!file_exists($cert_pem_file)) {
  27. file_put_contents($cert_pem_file, $wechat_config['sp_apiclient_cert']);
  28. }
  29. }
  30. $key_pem_file = null;
  31. if ($wechat_config['sp_apiclient_key']) {
  32. $key_pem_file = \Yii::$app->runtimePath . '/pem/' . md5($wechat_config['sp_apiclient_key']);
  33. if (!file_exists($key_pem_file)) {
  34. file_put_contents($key_pem_file, $wechat_config['sp_apiclient_key']);
  35. }
  36. }
  37. $config = [
  38. // 必要配置
  39. 'app_id' => $wechat_config['sp_appid'],
  40. 'mch_id' => $wechat_config['sp_mch_id'],
  41. 'key' => $wechat_config['sp_key'], // API 密钥
  42. // 如需使用敏感接口(如退款、发送红包等)需要配置 API 证书路径(登录商户平台下载 API 证书)
  43. 'cert_path' => $cert_pem_file, // XXX: 绝对路径!!!!
  44. 'key_path' => $key_pem_file, // XXX: 绝对路径!!!!
  45. 'notify_url' => '', // 你也可以在下单时单独设置来想覆盖它
  46. ];
  47. } else {
  48. $wechat_config = WechatConfig::findOne(['store_id' => $store_id, 'type' => 1]);
  49. // 证书
  50. if (!is_dir(\Yii::$app->runtimePath . '/pem')) {
  51. mkdir(\Yii::$app->runtimePath . '/pem');
  52. file_put_contents(\Yii::$app->runtimePath . '/pem/index.html', '');
  53. }
  54. $cert_pem_file = null;
  55. if ($wechat_config->cert_pem) {
  56. $cert_pem_file = \Yii::$app->runtimePath . '/pem/' . md5($wechat_config->cert_pem);
  57. if (!file_exists($cert_pem_file)) {
  58. file_put_contents($cert_pem_file, $wechat_config->cert_pem);
  59. }
  60. }
  61. $key_pem_file = null;
  62. if ($wechat_config->key_pem) {
  63. $key_pem_file = \Yii::$app->runtimePath . '/pem/' . md5($wechat_config->key_pem);
  64. if (!file_exists($key_pem_file)) {
  65. file_put_contents($key_pem_file, $wechat_config->key_pem);
  66. }
  67. }
  68. $config = [
  69. 'app_id' => $wechat_config->app_id,
  70. 'secret' => $wechat_config->app_secret,
  71. 'key' => $wechat_config->pay_key,
  72. 'mch_id' => $wechat_config->mch_id,
  73. 'cert_path' => $cert_pem_file,
  74. 'key_path' => $key_pem_file,
  75. 'response_type' => 'array'
  76. ];
  77. }
  78. return Factory::payment($config);
  79. }
  80. }