alipay_public_key; $key = $this->pregReplaceAll('/---.*---/', '', $key); $key = trim($key); $key = str_replace("\n", '', $key); $key = str_replace("\r\n", '', $key); $key = str_replace("\r", '', $key); $key = wordwrap($key, 64, "\r\n", true); if (mb_stripos($key, $begin_str) === false) { $key = $begin_str . "\r\n" . $key; } if (mb_stripos($key, $end_str) === false) { $key = $key . "\r\n" . $end_str; } $this->alipay_public_key = $key; }], ['app_private_key', function ($attr) { // key自动添加 -----BEGIN RSA PRIVATE KEY-----&&-----END RSA PRIVATE KEY-----&&换行符 $begin_str = '-----BEGIN RSA PRIVATE KEY-----'; $end_str = '-----END RSA PRIVATE KEY-----'; $key = $this->app_private_key; $key = $this->pregReplaceAll('/---.*---/', '', $key); $key = trim($key); $key = str_replace("\n", '', $key); $key = str_replace("\r\n", '', $key); $key = str_replace("\r", '', $key); $key = wordwrap($key, 64, "\r\n", true); if (mb_stripos($key, $begin_str) === false) { $key = $begin_str . "\r\n" . $key; } if (mb_stripos($key, $end_str) === false) { $key = $key . "\r\n" . $end_str; } $this->app_private_key = $key; }], ]; } public function attributeLabels() { return [ 'app_id' => '小程序AppID', 'alipay_public_key' => '支付宝公钥', 'app_public_key' => '应用公钥', 'app_private_key' => '应用私钥', 'cs_tnt_inst_id' => '云客服TntInstId', 'cs_scene' => '云客服Scene', ]; } public function save() { if (!$this->validate()) { return $this->getErrorResponse(); } $data = $this->attributes; unset($data['store_id']); Option::set(self::OPTION_KEY, $data, $this->store_id); return [ 'code' => 0, 'msg' => '保存成功。', ]; } /** * 根据 Store Id 获取其配置实例 * * @param string|int $storeId * @return static */ public static function get($storeId) { $instance = new static(); $instance->store_id = $storeId; $data = Option::get(self::OPTION_KEY, $storeId); if ($data != null) { $instance->attributes = (array)$data; } return $instance; } /** * 返回支付宝 AopClient * * @return \Alipay\AopClient */ public function getClient() { if ($this->app_id == null) { throw new \InvalidArgumentException('支付宝小程序 appid 为空,请检查是否配置支付宝小程序'); } try { $kp = AlipayKeyPair::create($this->app_private_key, $this->alipay_public_key); } catch (AlipayException $ex) { throw new \InvalidArgumentException('支付宝小程序密钥异常,请检查是否配置支付宝小程序'); } $requester = new AlipayCurlRequester([ CURLOPT_CONNECTTIMEOUT => 5, CURLOPT_TIMEOUT => 10, ]); return new \Alipay\AopClient($this->app_id, $kp, null, $requester); } private function pregReplaceAll($find, $replacement, $s) { while (preg_match($find, $s)) { $s = preg_replace($find, $replacement, $s); } return $s; } }