| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263 |
- <?php
- namespace addons\YunfastPay\common\services;
- use addons\Store\common\services\MerchantService;
- use addons\YunfastPay\common\services\trans\TransAbstract;
- use addons\YunfastPay\common\utils\KFRequestUtil;
- use common\models\order\Order;
- use Exception;
- use Yii;
- use yii\helpers\Json;
- /**
- * 银典支付service
- * @package addons\YunfastPay\common\services
- */
- class YunPayService extends BaseService
- {
- /**
- * uri
- */
- const URI = 'yunfast-pay/v1/notify/index';
- /**
- * @var KFRequestUtil
- */
- private $kfRequest;
- /**
- * 机构号(orgCd)
- *
- * @var string
- */
- private $orgCd;
- /**
- * 机构密钥(secret)
- *
- * @var string
- */
- private $secret;
- /**
- * 内管商户号(mchtCd)
- *
- * @var string
- */
- private $mchtCd;
- /**
- * 线上内管商户号(mchtCd)
- *
- * @var string
- */
- private $onlineMchtCd;
- /**
- * @var array
- */
- protected $config;
- /**
- * 回调地址
- *
- * @var string
- */
- private $notifyUrl;
- /**
- * @var integer
- */
- protected $storeId = 0;
- /**
- * @var array
- */
- protected $reqData;
- /**
- * @var boolean
- */
- protected $isOnline = false;
- /**
- * 创建支付请求类
- *
- * @param array $config
- * @return KFRequestUtil
- */
- public function init(array $config = [])
- {
- $config = !empty($config) ? $config : (new SettingService([
- 'mall_id' => $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;
- }
- }
|