YunPayService.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. namespace addons\YunfastPay\common\services;
  3. use addons\Store\common\services\MerchantService;
  4. use addons\YunfastPay\common\services\trans\TransAbstract;
  5. use addons\YunfastPay\common\utils\KFRequestUtil;
  6. use common\models\order\Order;
  7. use Exception;
  8. use Yii;
  9. use yii\helpers\Json;
  10. /**
  11. * 银典支付service
  12. * @package addons\YunfastPay\common\services
  13. */
  14. class YunPayService extends BaseService
  15. {
  16. /**
  17. * uri
  18. */
  19. const URI = 'yunfast-pay/v1/notify/index';
  20. /**
  21. * @var KFRequestUtil
  22. */
  23. private $kfRequest;
  24. /**
  25. * 机构号(orgCd)
  26. *
  27. * @var string
  28. */
  29. private $orgCd;
  30. /**
  31. * 机构密钥(secret)
  32. *
  33. * @var string
  34. */
  35. private $secret;
  36. /**
  37. * 内管商户号(mchtCd)
  38. *
  39. * @var string
  40. */
  41. private $mchtCd;
  42. /**
  43. * 线上内管商户号(mchtCd)
  44. *
  45. * @var string
  46. */
  47. private $onlineMchtCd;
  48. /**
  49. * @var array
  50. */
  51. protected $config;
  52. /**
  53. * 回调地址
  54. *
  55. * @var string
  56. */
  57. private $notifyUrl;
  58. /**
  59. * @var integer
  60. */
  61. protected $storeId = 0;
  62. /**
  63. * @var array
  64. */
  65. protected $reqData;
  66. /**
  67. * @var boolean
  68. */
  69. protected $isOnline = false;
  70. /**
  71. * 创建支付请求类
  72. *
  73. * @param array $config
  74. * @return KFRequestUtil
  75. */
  76. public function init(array $config = [])
  77. {
  78. $config = !empty($config) ? $config : (new SettingService([
  79. 'mall_id' => $this->mall_id
  80. ]))->getSetting();
  81. // 未配置
  82. if (empty($config['org_cd'])
  83. || empty($config['secret'])
  84. || empty($config['mcht_cd'])
  85. || empty($config['online_mcht_cd'])
  86. ) {
  87. throw new Exception('配置有误');
  88. }
  89. $this->config = $config;
  90. $this->orgCd = $config['org_cd'];
  91. $this->secret = $config['secret'];
  92. $this->mchtCd = $config['mcht_cd'];
  93. $this->onlineMchtCd = $config['online_mcht_cd'];
  94. $this->creatKFRq();
  95. }
  96. /**
  97. * 创建支付请求类
  98. *
  99. * @return void
  100. */
  101. private function creatKFRq()
  102. {
  103. $this->kfRequest = (new KFRequestUtil(
  104. $this->orgCd, $this->secret
  105. ));
  106. }
  107. /**
  108. * 发送请求
  109. *
  110. * @param TransAbstract $trans
  111. * @return array
  112. */
  113. public function sendPayRq(TransAbstract $trans)
  114. {
  115. // 获取回调地址
  116. $this->setNotifyUrl($trans->getNotifyType());
  117. // 添加回调地址
  118. $this->addNotifyUrl($trans);
  119. // 设置配置文件
  120. $trans->setConfig($this->config);
  121. $this->reqData = $trans->getReqData($this->getMchtCd());
  122. $res = Json::decode($this->kfRequest->req($this->reqData));
  123. return $res;
  124. }
  125. /**
  126. * @return array
  127. */
  128. public function getReqData()
  129. {
  130. return $this->reqData;
  131. }
  132. /**
  133. * 获取
  134. *
  135. * @param integer $storeId
  136. * @return string
  137. */
  138. public function getSignUrl(int $storeId)
  139. {
  140. $sign = $this->getSign($storeId);
  141. return sprintf(
  142. 'https://mcht.yunfastpay.com/html#/mcht_protocol?orgCd=%s&chnlCd=sxf&outMchtCd=%s&sign=%s',
  143. $this->orgCd,
  144. $storeId,
  145. $sign
  146. );
  147. }
  148. /**
  149. * 获取签名
  150. *
  151. * @param integer $storeId
  152. * @return string
  153. */
  154. protected function getSign(int $storeId)
  155. {
  156. return md5(
  157. sprintf(
  158. 'orgCd=%s&chnlCd=sxf&outMchtCd=%s&%s',
  159. $this->orgCd,
  160. $storeId,
  161. substr($this->secret, 0, 6)
  162. )
  163. );
  164. }
  165. /**
  166. * 添加回调地址
  167. *
  168. * @param TransAbstract $trans
  169. * @return void
  170. */
  171. public function addNotifyUrl(TransAbstract $trans)
  172. {
  173. if ($trans->getHasNotify()) {
  174. $trans->setNotifyUrl(
  175. $this->notifyUrl
  176. );
  177. }
  178. }
  179. /**
  180. * 设置商户ID
  181. *
  182. * @param integer $id
  183. * @return void
  184. */
  185. public function setStoreId(int $id)
  186. {
  187. $this->storeId = $id;
  188. }
  189. /**
  190. * 设置为是否线上
  191. *
  192. * @param boolean $boolean
  193. * @return void
  194. */
  195. public function setIsOnline(bool $boolean = true)
  196. {
  197. $this->isOnline = $boolean;
  198. }
  199. /**
  200. * 获取回调地址
  201. *
  202. * @return void
  203. */
  204. private function setNotifyUrl($type = 'pay')
  205. {
  206. $host = Yii::$app->services->setting->platform->get('system.api_url');
  207. if (empty($host)) throw new Exception('未设置API地址');
  208. $this->notifyUrl = $host . '/' . static::URI . '?mall_id=' . $this->mall_id . '&type=' . $type;
  209. }
  210. /**
  211. * 获取商户ID
  212. *
  213. * @return int
  214. */
  215. private function getMchtCd()
  216. {
  217. if ($this->storeId == 0) {
  218. // return $this->isOnline
  219. // ? $this->onlineMchtCd
  220. // : $this->mchtCd;
  221. // 未提供店铺ID使用线上
  222. return $this->onlineMchtCd;
  223. }
  224. $mchtCd = (new MerchantService(['mall_id' => $this->mall_id, 'store_id' => $this->storeId]))
  225. ->getMchtCd();
  226. if ($mchtCd == null) throw new \Exception('商户%s未绑定MchtCd');
  227. return $mchtCd;
  228. }
  229. }