downcert.ps1 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # 读取配置文件
  2. $scriptPath = Split-Path -Parent $MyInvocation.MyCommand.Path
  3. $envPath = Join-Path $scriptPath "../../.env"
  4. # 从.env文件读取配置
  5. $envContent = Get-Content $envPath -Raw
  6. if ($envContent -match 'WECHAT_PAY_KEY=(.+)') {
  7. $apiV3key = $matches[1].Trim()
  8. } else {
  9. $apiV3key = Read-Host "未找到 API V3 密钥,请手动输入"
  10. }
  11. if ($envContent -match 'WECHAT_PAY_MCH_ID=(.+)') {
  12. $mchId = $matches[1].Trim()
  13. } else {
  14. $mchId = Read-Host "未找到商户号,请手动输入"
  15. }
  16. # 从wechat.php配置文件读取证书序列号
  17. $configContent = Get-Content $envPath -Raw
  18. if ($configContent -match 'WECHAT_PAY_SERIAL_NO=(.+)') {
  19. $mchSerialNo = $matches[1].Trim()
  20. } else {
  21. $mchSerialNo = Read-Host "未找到商户证书序列号,请手动输入"
  22. }
  23. # 设置证书相关路径
  24. $certPath = Join-Path $scriptPath "../../storage/app/certs/wechat"
  25. $mchPrivateKeyFilePath = Join-Path $certPath "apiclient_key.pem"
  26. $outputFilePath = Join-Path $certPath "wechatpay_cert.pem"
  27. # 确保证书目录存在
  28. if (-not (Test-Path $certPath)) {
  29. New-Item -ItemType Directory -Path $certPath -Force
  30. }
  31. Write-Host "`n证书将下载到: $outputFilePath"
  32. Write-Host "使用的商户私钥文件路径: $mchPrivateKeyFilePath`n"
  33. # 执行证书下载
  34. try {
  35. java -jar ./script/bin/CertificateDownloader-1.2.0-jar-with-dependencies.jar `
  36. -k $apiV3key `
  37. -m $mchId `
  38. -f $mchPrivateKeyFilePath `
  39. -s $mchSerialNo `
  40. -o $outputFilePath
  41. if ($LASTEXITCODE -eq 0) {
  42. Write-Host "`n证书下载成功!" -ForegroundColor Green
  43. } else {
  44. Write-Host "`n证书下载失败!" -ForegroundColor Red
  45. }
  46. } catch {
  47. Write-Host "`n发生错误: $_" -ForegroundColor Red
  48. }