$this->mall_id ]))->getSetting(); // 未配置 if (empty($config['org_cd']) || empty($config['secret']) || empty($config['mcht_cd']) || empty($config['online_mcht_cd']) ) { throw new Exception('配置有误'); } $this->config = $config; $this->orgCd = $config['org_cd']; $this->secret = $config['secret']; $this->mchtCd = $config['mcht_cd']; $this->onlineMchtCd = $config['online_mcht_cd']; $this->creatKFRq(); } /** * 创建支付请求类 * * @return void */ private function creatKFRq() { $this->kfRequest = (new KFRequestUtil( $this->orgCd, $this->secret )); } /** * 发送请求 * * @param TransAbstract $trans * @return array */ public function sendPayRq(TransAbstract $trans) { // 获取回调地址 $this->setNotifyUrl($trans->getNotifyType()); // 添加回调地址 $this->addNotifyUrl($trans); // 设置配置文件 $trans->setConfig($this->config); $this->reqData = $trans->getReqData($this->getMchtCd()); $res = Json::decode($this->kfRequest->req($this->reqData)); return $res; } /** * @return array */ public function getReqData() { return $this->reqData; } /** * 获取 * * @param integer $storeId * @return string */ public function getSignUrl(int $storeId) { $sign = $this->getSign($storeId); return sprintf( 'https://mcht.yunfastpay.com/html#/mcht_protocol?orgCd=%s&chnlCd=sxf&outMchtCd=%s&sign=%s', $this->orgCd, $storeId, $sign ); } /** * 获取签名 * * @param integer $storeId * @return string */ protected function getSign(int $storeId) { return md5( sprintf( 'orgCd=%s&chnlCd=sxf&outMchtCd=%s&%s', $this->orgCd, $storeId, substr($this->secret, 0, 6) ) ); } /** * 添加回调地址 * * @param TransAbstract $trans * @return void */ public function addNotifyUrl(TransAbstract $trans) { if ($trans->getHasNotify()) { $trans->setNotifyUrl( $this->notifyUrl ); } } /** * 设置商户ID * * @param integer $id * @return void */ public function setStoreId(int $id) { $this->storeId = $id; } /** * 设置为是否线上 * * @param boolean $boolean * @return void */ public function setIsOnline(bool $boolean = true) { $this->isOnline = $boolean; } /** * 获取回调地址 * * @return void */ private function setNotifyUrl($type = 'pay') { $host = Yii::$app->services->setting->platform->get('system.api_url'); if (empty($host)) throw new Exception('未设置API地址'); $this->notifyUrl = $host . '/' . static::URI . '?mall_id=' . $this->mall_id . '&type=' . $type; } /** * 获取商户ID * * @return int */ private function getMchtCd() { if ($this->storeId == 0) { // return $this->isOnline // ? $this->onlineMchtCd // : $this->mchtCd; // 未提供店铺ID使用线上 return $this->onlineMchtCd; } $mchtCd = (new MerchantService(['mall_id' => $this->mall_id, 'store_id' => $this->storeId])) ->getMchtCd(); if ($mchtCd == null) throw new \Exception('商户%s未绑定MchtCd'); return $mchtCd; } }