AlipayAbstract.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. namespace common\expand\alipay;
  3. use Alipay\EasySDK\Kernel\Config;
  4. use Alipay\EasySDK\Util\Generic\Models\AlipayOpenApiGenericResponse;
  5. use common\enums\StatusEnum;
  6. use common\models\user\User;
  7. use common\models\user\UserInfo;
  8. use Yii;
  9. use yii\helpers\Json;
  10. abstract class AlipayAbstract
  11. {
  12. protected $data = [];
  13. protected $config = [];
  14. /**
  15. * 支付宝method
  16. * @var string
  17. */
  18. protected $alipay_method;
  19. /**
  20. * @var AlipayFactory
  21. */
  22. protected $alipayFactory;
  23. public function __construct(array $data)
  24. {
  25. $this->data = $data;
  26. $this->config = \Yii::$app->services->setting->mall->get($data['mall_id'], 'alipay_mini');
  27. $this->alipayFactory = AlipayFactory::setOptions($this->getOptions());
  28. }
  29. /**
  30. * @return Config
  31. */
  32. private function getOptions(): Config
  33. {
  34. $options = new Config();
  35. $options->protocol = 'https';
  36. $options->gatewayHost = 'openapi.alipay.com';
  37. // $options->gatewayHost = 'openapi.alipaydev.com'; // 测试环境
  38. $options->signType = 'RSA2';
  39. $options->appId = $this->config['app_id'];
  40. // 为避免私钥随源码泄露,推荐从文件中读取私钥字符串而不是写入源码中
  41. $options->merchantPrivateKey = $this->config['merchantPrivateKey'];
  42. if (!empty($this->config['is_use_cert'])) { // 如果启用证书模式
  43. $options->alipayCertPath = dirname(Yii::$app->basePath).'/'.$this->config['alipayCertPath'];
  44. $options->alipayRootCertPath = dirname(Yii::$app->basePath).'/'.$this->config['alipayRootCertPath'];
  45. $options->merchantCertPath = dirname(Yii::$app->basePath).'/'.$this->config['merchantCertPath'];
  46. } else {
  47. //注:如果采用非证书模式,则无需赋值上面的三个证书路径,改为赋值如下的支付宝公钥字符串即可
  48. $options->alipayPublicKey = file_get_contents(dirname(Yii::$app->basePath).'/'.$this->config['alipayCertPath']);
  49. }
  50. //可设置异步通知接收服务地址(可选)
  51. $options->notifyUrl = '';
  52. //可设置AES密钥,调用AES加解密相关接口时需要(可选)
  53. $options->encryptKey = $this->config['encryptKey'];
  54. return $options;
  55. }
  56. /**
  57. * 获取支付宝用户id
  58. * @return int|string
  59. * @throws \Exception
  60. */
  61. protected function getAlipayOpenIdByUserId()
  62. {
  63. $identity = UserInfo::find()->where(['user_id' => $this->data['user_id']])
  64. ->andWhere(['platform' => User::PLATFORM_MP_ALI])
  65. ->select('openid')->scalar();
  66. if (empty($identity)) throw new \Exception("当前用户未绑定支付宝: {$this->data['user_id']}");
  67. return $identity;
  68. }
  69. /**
  70. * @param AlipayOpenApiGenericResponse $response
  71. * @return mixed|null
  72. */
  73. protected function formatResponse(AlipayOpenApiGenericResponse $response)
  74. {
  75. $httpBody = Json::decode($response->httpBody);
  76. if (!empty($this->alipay_method)) {
  77. return $httpBody[str_replace('.', '_', "{$this->alipay_method}_response")];
  78. }
  79. return $httpBody;
  80. }
  81. }