Tools.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  2. namespace JiaLeo\Payment\Wechatpay;
  3. use JiaLeo\Payment\Common\PaymentException;
  4. class Tools extends BaseWechatpay
  5. {
  6. public $errorCode; //错误代码
  7. public $errorCodeDes; //错误描述
  8. /**
  9. * 获取RSA公钥
  10. * @return bool|mixed
  11. * @throws PaymentException
  12. */
  13. public function getPublicKey()
  14. {
  15. $url = 'https://fraud.mch.weixin.qq.com/risk/getpublickey';
  16. $data = array(
  17. 'mch_id' => $this->config['mchid'],
  18. 'nonce_str' => $this->getNonceStr(),
  19. 'sign_type' => 'MD5'
  20. );
  21. //签名
  22. $data['sign'] = $this->makeSign($data);
  23. //转换成xml
  24. $xml = $this->toXml($data);
  25. $result = $this->postXmlCurl($url, $xml, $this->config['sslcert_path'], $this->config['sslkey_path']);
  26. $get_result = $this->fromXml($result);
  27. try {
  28. if (!isset($get_result['return_code']) || $get_result['return_code'] != 'SUCCESS') {
  29. throw new PaymentException('调起获取RSA公钥接口失败!错误信息:' . isset($get_result['return_msg']) ? $get_result['return_msg'] : $get_result);
  30. }
  31. if ($get_result['result_code'] != 'SUCCESS') {
  32. throw new PaymentException('调起获取RSA公钥接口失败!错误信息:' . isset($get_result['err_code_des']) ? $get_result['err_code_des'] : $get_result);
  33. }
  34. if (empty($get_result['pub_key'])) {
  35. throw new PaymentException('获取pub_key字段错误!');
  36. }
  37. } catch (\Exception $e) {
  38. $this->errorCode = $get_result['err_code'];
  39. $this->errorCodeDes = $get_result['err_code_des'];
  40. return false;
  41. }
  42. return $get_result;
  43. }
  44. }