AopCertClient.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Name: 芸众商城系统
  5. * Author: 广州市芸众信息科技有限公司
  6. * Profile: 广州市芸众信息科技有限公司位于国际商贸中心的广州,专注于移动电子商务生态系统打造,拥有芸众社交电商系统、区块链数字资产管理系统、供应链管理系统、电子合同等产品/服务。官网 :www.yunzmall.com www.yunzshop.com
  7. * Date: 2021/6/30
  8. * Time: 10:17
  9. */
  10. namespace app\common\services\alipay\sdk;
  11. use Alipay\EasySDK\Kernel\Factory;
  12. class AopCertClient
  13. {
  14. protected $config;
  15. public function __construct()
  16. {
  17. $this->_initConfig();
  18. }
  19. private function _initConfig()
  20. {
  21. $options = new \Alipay\EasySDK\Kernel\Config();
  22. $options->protocol = 'https';
  23. //$options->gatewayHost = 'openapi.alipaydev.com'; //沙箱测试
  24. $options->gatewayHost = 'openapi.alipay.com';
  25. $options->signType = 'RSA2';
  26. $this->config = $options;
  27. }
  28. public function execute($method, $bizParams,$textParams = [])
  29. {
  30. //设置支付宝参数(全局只需设置一次)
  31. Factory::setOptions($this->config);
  32. try {
  33. //支付宝请求接口Client
  34. $aop = Factory::util()->generic();
  35. // 发起API调用
  36. //1、接口名称 2、接口公共参数附加 3、请求参数 biz_content
  37. $result = $aop->execute($method,$textParams,$bizParams);
  38. //3. 处理响应或异常
  39. if (!empty($result->code) && $result->code == 10000) {
  40. return ['code'=> true,
  41. 'data'=> $result->toMap(),
  42. 'msg'=> '成功'
  43. ];
  44. }
  45. return ['code'=> false,
  46. 'data'=> $result->toMap(),
  47. 'msg'=> "调用失败,原因:". $result->msg.",".$result->subMsg
  48. ];
  49. } catch (\Exception $e) {
  50. return ['code'=> false, 'msg'=> $e->getMessage()];
  51. }
  52. }
  53. public function getConfig($key = null)
  54. {
  55. if ($key == null) {
  56. return $this->config;
  57. }
  58. return $this->config->$key;
  59. }
  60. public function setConfigValue($key,$value)
  61. {
  62. $this->config->{$key} = $value;
  63. }
  64. /**
  65. * @param $config
  66. */
  67. public function setConfig(array $config)
  68. {
  69. if (!empty($config) && is_array($config)) {
  70. foreach ($config as $k => $v) {
  71. $this->config->{$k} = $v;
  72. }
  73. }
  74. // $options = new Config();
  75. // $options->protocol = 'https';
  76. // $options->gatewayHost = 'openapi.alipay.com';
  77. // $options->signType = 'RSA2';
  78. //
  79. // $options->appId = '<-- 请填写您的AppId,例如:2019022663440152 -->';
  80. //
  81. // // 为避免私钥随源码泄露,推荐从文件中读取私钥字符串而不是写入源码中
  82. // $options->merchantPrivateKey = '<-- 请填写您的应用私钥,例如:MIIEvQIBADANB ... ... -->';
  83. //
  84. // $options->alipayCertPath = '<-- 请填写您的支付宝公钥证书文件路径,例如:/foo/alipayCertPublicKey_RSA2.crt -->';
  85. // $options->alipayRootCertPath = '<-- 请填写您的支付宝根证书文件路径,例如:/foo/alipayRootCert.crt" -->';
  86. // $options->merchantCertPath = '<-- 请填写您的应用公钥证书文件路径,例如:/foo/appCertPublicKey_2019051064521003.crt -->';
  87. //
  88. // //注:如果采用非证书模式,则无需赋值上面的三个证书路径,改为赋值如下的支付宝公钥字符串即可
  89. // // $options->alipayPublicKey = '<-- 请填写您的支付宝公钥,例如:MIIBIjANBg... -->';
  90. //
  91. // //可设置异步通知接收服务地址(可选)
  92. // $options->notifyUrl = "<-- 请填写您的支付类接口异步通知接收服务地址,例如:https://www.test.com/callback -->";
  93. //
  94. // //可设置AES密钥,调用AES加解密相关接口时需要(可选)
  95. // $options->encryptKey = "<-- 请填写您的AES密钥,例如:aa4BtZ4tspm2wnXLb1ThQA== -->";
  96. }
  97. }