Skip to content

Commit

Permalink
支持微信支付 .p12 格式证书
Browse files Browse the repository at this point in the history
  • Loading branch information
messense committed Jul 28, 2020
1 parent 9722d72 commit 46b40a5
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
38 changes: 37 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ optionaldict = ">=0.1.0"
python-dateutil = ">=2.5.2"
cryptography = "^2.8"
dataclasses = { version = "^0.7", python = "~3.6" }
requests-pkcs12 = "^1.7"

[tool.poetry.dev-dependencies]
pytest = "^5.4.1"
Expand Down
13 changes: 11 additions & 2 deletions wechatpy/pay/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,19 @@ def __init__(
self.sub_mch_id = sub_mch_id
self.mch_cert = mch_cert
self.mch_key = mch_key
self._using_pkcs12_cert = False
self.timeout = timeout
self.sandbox = sandbox
self._sandbox_api_key = None
self._http = requests.Session()
if mch_cert and mch_cert.endswith(".p12"):
from requests_pkcs12 import Pkcs12Adapter

# 商户 .p12 格式证书,证书密码默认为商户 ID
self._http.mount(
self.API_BASE_URL, Pkcs12Adapter(pkcs12_filename=self.mch_cert, pkcs12_password=self.mch_id)
)
self._using_pkcs12_cert = True

def _fetch_sandbox_api_key(self):
nonce_str = random_string(32)
Expand Down Expand Up @@ -131,8 +140,8 @@ def _request(self, method, url_or_endpoint, **kwargs):
body = body.encode("utf-8")
kwargs["data"] = body

# 商户证书
if self.mch_cert and self.mch_key:
# 商户 PEM 证书
if not self._using_pkcs12_cert and self.mch_cert and self.mch_key:
kwargs["cert"] = (self.mch_cert, self.mch_key)

kwargs["timeout"] = kwargs.get("timeout", self.timeout)
Expand Down

0 comments on commit 46b40a5

Please sign in to comment.