Payment.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. <?php
  2. /*
  3. * FecShop file.
  4. *
  5. * @link http://www.fecshop.com/
  6. * @copyright Copyright (c) 2016 FecShop Software LLC
  7. * @license http://www.fecshop.com/license/
  8. */
  9. namespace fecshop\services;
  10. use Yii;
  11. /**
  12. * Payment services.
  13. *
  14. * @property \fecshop\services\payment\Alipay $alipay alipay sub-service of url
  15. * @property \fecshop\services\payment\Paypal $paypal paypal sub-service of url
  16. * @property \fecshop\services\payment\Wxpay $wxpay wxpay sub-service of url
  17. *
  18. * @author Terry Zhao <2358269014@qq.com>
  19. * @since 1.0
  20. */
  21. class Payment extends Service
  22. {
  23. public $paymentConfig;
  24. /**
  25. * Array
  26. * 不需要释放库存的支付方式。譬如货到付款,在系统中
  27. * pending订单,如果一段时间未付款,会释放产品库存,但是货到付款类型的订单不会释放,
  28. * 如果需要释放产品库存,客服在后台取消订单即可释放产品库存。
  29. */
  30. public $noRelasePaymentMethod;
  31. public $env_sanbox = 'sanbox';
  32. public $env_product = 'product';
  33. protected $_currentPaymentMethod;
  34. public function init()
  35. {
  36. parent::init();
  37. }
  38. /**
  39. * @param $payment_method | string
  40. * 设置当前的支付方式
  41. */
  42. public function setPaymentMethod($payment_method)
  43. {
  44. $this->_currentPaymentMethod = $payment_method;
  45. }
  46. /**
  47. * @return $payment_method | string
  48. * 得到当前的支付方式
  49. */
  50. public function getPaymentMethod()
  51. {
  52. return $this->_currentPaymentMethod;
  53. }
  54. /**
  55. * @return 得到支付方式,以及对应的label,譬如:
  56. * [
  57. * 'check_money' => 'Check / Money Order',
  58. * 'alipay_standard' => '支付宝支付',
  59. * ] #从配置信息中获取
  60. */
  61. public function getPaymentLabels(){
  62. $arr = [];
  63. $paymentConfig = $this->paymentConfig;
  64. if (is_array($paymentConfig['standard'])) {
  65. foreach ($paymentConfig['standard'] as $payment_method => $one) {
  66. if (isset($one['label']) && !empty($one['label']) && $payment_method) {
  67. $arr[$payment_method] = $one['label'];
  68. }
  69. }
  70. }
  71. return $arr;
  72. }
  73. /**
  74. * @param $payment_method | String 支付方式。
  75. * @return 返回提交订单信息跳转到的第三方支付url,也就是第三方支付的url。
  76. * #从配置信息中获取
  77. */
  78. public function getStandardStartUrl($payment_method = '', $type = '')
  79. {
  80. if (!$payment_method) {
  81. $payment_method = $this->getPaymentMethod();
  82. }
  83. if ($payment_method) {
  84. $paymentConfig = $this->paymentConfig;
  85. if (isset($paymentConfig['standard'][$payment_method]['start_url'])) {
  86. if (!empty($paymentConfig['standard'][$payment_method]['start_url'])) {
  87. if ($type == 'appserver') {
  88. return $this->getAppServerUrl($paymentConfig['standard'][$payment_method]['start_url']);
  89. } else {
  90. return $this->getUrl($paymentConfig['standard'][$payment_method]['start_url']);
  91. }
  92. }
  93. }
  94. }
  95. }
  96. public function getAppServerUrl($url)
  97. {
  98. $url = str_replace('@homeUrl', '', $url);
  99. return trim($url);
  100. }
  101. /**
  102. * @param $url | String url的字符串
  103. * @return string 根据传递的字符串格式,得到相应的url
  104. */
  105. protected function getUrl($url)
  106. {
  107. $homeUrl = Yii::$service->url->homeUrl();
  108. $url = str_replace('@homeUrl', $homeUrl, $url);
  109. return trim($url);
  110. }
  111. /**
  112. * @param $payment_method | String 支付方式。
  113. * @return 第三方支付成功后,返回到网站的url
  114. * #从配置信息中获取
  115. */
  116. public function getStandardSuccessRedirectUrl($payment_method = '')
  117. {
  118. if (!$payment_method) {
  119. $payment_method = $this->getPaymentMethod();
  120. }
  121. if ($payment_method) {
  122. $paymentConfig = $this->paymentConfig;
  123. if (isset($paymentConfig['standard'][$payment_method]['success_redirect_url'])) {
  124. if (!empty($paymentConfig['standard'][$payment_method]['success_redirect_url'])) {
  125. return $this->getUrl($paymentConfig['standard'][$payment_method]['success_redirect_url']);
  126. }
  127. }
  128. }
  129. }
  130. /**
  131. * @param $payment_method | String 支付方式。
  132. * @return string 支付取消的url。
  133. * #从配置信息中获取
  134. */
  135. public function getStandardCancelUrl($payment_method = '')
  136. {
  137. if (!$payment_method) {
  138. $payment_method = $this->getPaymentMethod();
  139. }
  140. if ($payment_method) {
  141. $paymentConfig = $this->paymentConfig;
  142. if (isset($paymentConfig['standard'][$payment_method]['cancel_url'])) {
  143. if (!empty($paymentConfig['standard'][$payment_method]['cancel_url'])) {
  144. return $this->getUrl($paymentConfig['standard'][$payment_method]['cancel_url']);
  145. }
  146. }
  147. }
  148. }
  149. /**
  150. * @param $payment_method | String 支付方式。
  151. * @return string 用户名
  152. * #从配置信息中获取
  153. */
  154. public function getStandardAccount($payment_method = '')
  155. {
  156. if (!$payment_method) {
  157. $payment_method = $this->getPaymentMethod();
  158. }
  159. if ($payment_method) {
  160. $paymentConfig = $this->paymentConfig;
  161. if (isset($paymentConfig['standard'][$payment_method]['account'])) {
  162. if (!empty($paymentConfig['standard'][$payment_method]['account'])) {
  163. return $paymentConfig['standard'][$payment_method]['account'];
  164. }
  165. }
  166. }
  167. }
  168. /**
  169. * @param $payment_method | String 支付方式。
  170. * @return string Password
  171. * #从配置信息中获取
  172. */
  173. public function getStandardPassword($payment_method = '')
  174. {
  175. if (!$payment_method) {
  176. $payment_method = $this->getPaymentMethod();
  177. }
  178. if ($payment_method) {
  179. $paymentConfig = $this->paymentConfig;
  180. if (isset($paymentConfig['standard'][$payment_method]['password'])) {
  181. if (!empty($paymentConfig['standard'][$payment_method]['password'])) {
  182. return $paymentConfig['standard'][$payment_method]['password'];
  183. }
  184. }
  185. }
  186. }
  187. /**
  188. * @param $payment_method | String 支付方式。
  189. * @return string Signature
  190. * #从配置信息中获取
  191. */
  192. public function getStandardSignature($payment_method = '')
  193. {
  194. if (!$payment_method) {
  195. $payment_method = $this->getPaymentMethod();
  196. }
  197. if ($payment_method) {
  198. $paymentConfig = $this->paymentConfig;
  199. if (isset($paymentConfig['standard'][$payment_method]['signature'])) {
  200. if (!empty($paymentConfig['standard'][$payment_method]['signature'])) {
  201. return $paymentConfig['standard'][$payment_method]['signature'];
  202. }
  203. }
  204. }
  205. }
  206. /**
  207. * @param $payment_method | String 支付方式。
  208. * @return 返回进行数据交互的express的api地址。
  209. */
  210. public function getStandardWebscrUrl($payment_method = '')
  211. {
  212. if (!$payment_method) {
  213. $payment_method = $this->getPaymentMethod();
  214. }
  215. if ($payment_method) {
  216. $paymentConfig = $this->paymentConfig;
  217. if (isset($paymentConfig['standard'][$payment_method]['webscr_url'])) {
  218. if (!empty($paymentConfig['standard'][$payment_method]['webscr_url'])) {
  219. return $paymentConfig['standard'][$payment_method]['webscr_url'];
  220. }
  221. }
  222. }
  223. }
  224. /**
  225. * @return array 得到所有支付的数组,数组含有三个字段。
  226. */
  227. public function getStandardPaymentArr()
  228. {
  229. $arr = [];
  230. $appName = Yii::$service->helper->getAppName();
  231. if (
  232. isset($this->paymentConfig['standard']) &&
  233. is_array($this->paymentConfig['standard'])
  234. ) {
  235. foreach ($this->paymentConfig['standard'] as $payment_type => $info) {
  236. // 查看配置是否是激活状态
  237. $checkMoneyConfig = Yii::$app->store->get($appName.'_payment', $payment_type);
  238. if ($checkMoneyConfig != Yii::$app->store->enable) {
  239. continue;
  240. }
  241. $label = $info['label'];
  242. $imageUrl = '';
  243. if (is_array($info['image'])) {
  244. list($iUrl, $l) = $info['image'];
  245. if ($iUrl) {
  246. $imageUrl = Yii::$service->image->getImgUrl($iUrl, $l);
  247. }
  248. }
  249. $supplement = $info['supplement'];
  250. $arr[$payment_type] = [
  251. 'label' => $label,
  252. 'imageUrl' => $imageUrl,
  253. 'supplement' => $supplement,
  254. ];
  255. }
  256. }
  257. return $arr;
  258. }
  259. /**
  260. * @param $payment_method | String , 支付方式
  261. * @return bool 判断传递的支付方式,是否在配置中存在,如果存在返回true。
  262. */
  263. protected function actionIfIsCorrectStandard($payment_method)
  264. {
  265. $paymentConfig = $this->paymentConfig;
  266. $standard = isset($paymentConfig['standard']) ? $paymentConfig['standard'] : '';
  267. if (isset($standard[$payment_method]) && !empty($standard[$payment_method])) {
  268. return true;
  269. } else {
  270. return false;
  271. }
  272. }
  273. public function getStandardNvpUrl($payment_method = '')
  274. {
  275. if (!$payment_method) {
  276. $payment_method = $this->getPaymentMethod();
  277. }
  278. if ($payment_method) {
  279. $paymentConfig = $this->paymentConfig;
  280. if (isset($paymentConfig['standard'][$payment_method]['nvp_url'])) {
  281. if (!empty($paymentConfig['standard'][$payment_method]['nvp_url'])) {
  282. return $paymentConfig['standard'][$payment_method]['nvp_url'];
  283. }
  284. }
  285. }
  286. }
  287. /**
  288. * @param $payment_method | String 支付方式。
  289. * @return 返回支付方式的label
  290. */
  291. public function getPaymentLabelByMethod($payment_method = '')
  292. {
  293. $payment_method_label = $this->getStandardLabel($payment_method);
  294. if (!$payment_method_label) {
  295. $payment_method_label = $this->getExpressLabel($payment_method);
  296. }
  297. if ($payment_method_label) {
  298. return $payment_method_label;
  299. } else {
  300. return $payment_method;
  301. }
  302. }
  303. /**
  304. * @param $payment_method | String 支付方式。
  305. * @return 返回进行数据交互的express的label。
  306. */
  307. public function getStandardLabel($payment_method = '')
  308. {
  309. if (!$payment_method) {
  310. $payment_method = $this->getPaymentMethod();
  311. }
  312. if ($payment_method) {
  313. $paymentConfig = $this->paymentConfig;
  314. if (isset($paymentConfig['standard'][$payment_method]['label'])) {
  315. if (!empty($paymentConfig['standard'][$payment_method]['label'])) {
  316. return $paymentConfig['standard'][$payment_method]['label'];
  317. }
  318. }
  319. }
  320. }
  321. /**
  322. * @param $payment_method | String 支付方式。
  323. * @return 返回进行数据交互的express的signature。
  324. */
  325. public function getStandardIpnUrl($payment_method = '')
  326. {
  327. if (!$payment_method) {
  328. $payment_method = $this->getPaymentMethod();
  329. }
  330. if ($payment_method) {
  331. $paymentConfig = $this->paymentConfig;
  332. if (isset($paymentConfig['standard'][$payment_method]['ipn_url'])) {
  333. if (!empty($paymentConfig['standard'][$payment_method]['ipn_url'])) {
  334. return $this->getUrl($paymentConfig['standard'][$payment_method]['ipn_url']);
  335. }
  336. }
  337. }
  338. }
  339. /**
  340. * @param $payment_method | String 支付方式。
  341. * @return 返回进行数据交互的express的signature。
  342. */
  343. public function getStandardReturnUrl($payment_method = '')
  344. {
  345. if (!$payment_method) {
  346. $payment_method = $this->getPaymentMethod();
  347. }
  348. if ($payment_method) {
  349. $paymentConfig = $this->paymentConfig;
  350. if (isset($paymentConfig['standard'][$payment_method]['return_url'])) {
  351. if (!empty($paymentConfig['standard'][$payment_method]['return_url'])) {
  352. return $this->getUrl($paymentConfig['standard'][$payment_method]['return_url']);
  353. }
  354. }
  355. }
  356. }
  357. //###################
  358. //# Express 部分 ##
  359. //###################
  360. /**
  361. * @param $payment_method | String 支付方式。
  362. * @return 返回获取token的url地址。
  363. */
  364. public function getExpressNvpUrl($payment_method = '')
  365. {
  366. if (!$payment_method) {
  367. $payment_method = $this->getPaymentMethod();
  368. }
  369. if ($payment_method) {
  370. $paymentConfig = $this->paymentConfig;
  371. if (isset($paymentConfig['express'][$payment_method]['nvp_url'])) {
  372. if (!empty($paymentConfig['express'][$payment_method]['nvp_url'])) {
  373. return $paymentConfig['express'][$payment_method]['nvp_url'];
  374. }
  375. }
  376. }
  377. }
  378. /**
  379. * @param $payment_method | String 支付方式。
  380. * @return 返回进行数据交互的express的api地址。
  381. */
  382. public function getExpressWebscrUrl($payment_method = '')
  383. {
  384. if (!$payment_method) {
  385. $payment_method = $this->getPaymentMethod();
  386. }
  387. if ($payment_method) {
  388. $paymentConfig = $this->paymentConfig;
  389. if (isset($paymentConfig['express'][$payment_method]['webscr_url'])) {
  390. if (!empty($paymentConfig['express'][$payment_method]['webscr_url'])) {
  391. return $paymentConfig['express'][$payment_method]['webscr_url'];
  392. }
  393. }
  394. }
  395. }
  396. /**
  397. * @param $payment_method | String 支付方式。
  398. * @return 返回进行数据交互的express的account。
  399. */
  400. public function getExpressAccount($payment_method = '')
  401. {
  402. if (!$payment_method) {
  403. $payment_method = $this->getPaymentMethod();
  404. }
  405. if ($payment_method) {
  406. $paymentConfig = $this->paymentConfig;
  407. if (isset($paymentConfig['express'][$payment_method]['account'])) {
  408. if (!empty($paymentConfig['express'][$payment_method]['account'])) {
  409. return $paymentConfig['express'][$payment_method]['account'];
  410. }
  411. }
  412. }
  413. }
  414. /**
  415. * @param $payment_method | String 支付方式。
  416. * @return 返回进行数据交互的express的password。
  417. */
  418. public function getExpressPassword($payment_method = '')
  419. {
  420. if (!$payment_method) {
  421. $payment_method = $this->getPaymentMethod();
  422. }
  423. if ($payment_method) {
  424. $paymentConfig = $this->paymentConfig;
  425. if (isset($paymentConfig['express'][$payment_method]['password'])) {
  426. if (!empty($paymentConfig['express'][$payment_method]['password'])) {
  427. return $paymentConfig['express'][$payment_method]['password'];
  428. }
  429. }
  430. }
  431. }
  432. /**
  433. * @param $payment_method | String 支付方式。
  434. * @return 返回进行数据交互的express的signature。
  435. */
  436. public function getExpressSignature($payment_method = '')
  437. {
  438. if (!$payment_method) {
  439. $payment_method = $this->getPaymentMethod();
  440. }
  441. if ($payment_method) {
  442. $paymentConfig = $this->paymentConfig;
  443. if (isset($paymentConfig['express'][$payment_method]['signature'])) {
  444. if (!empty($paymentConfig['express'][$payment_method]['signature'])) {
  445. return $paymentConfig['express'][$payment_method]['signature'];
  446. }
  447. }
  448. }
  449. }
  450. /**
  451. * @param $payment_method | String 支付方式。
  452. * @return 返回进行数据交互的express的label。
  453. */
  454. public function getExpressLabel($payment_method = '')
  455. {
  456. if (!$payment_method) {
  457. $payment_method = $this->getPaymentMethod();
  458. }
  459. if ($payment_method) {
  460. $paymentConfig = $this->paymentConfig;
  461. if (isset($paymentConfig['express'][$payment_method]['label'])) {
  462. if (!empty($paymentConfig['express'][$payment_method]['label'])) {
  463. return $paymentConfig['express'][$payment_method]['label'];
  464. }
  465. }
  466. }
  467. }
  468. /**
  469. * @param $payment_method | String 支付方式。
  470. * @return 返回进行数据交互的express的signature。
  471. */
  472. public function getExpressReturnUrl($payment_method = '')
  473. {
  474. if (!$payment_method) {
  475. $payment_method = $this->getPaymentMethod();
  476. }
  477. if ($payment_method) {
  478. $paymentConfig = $this->paymentConfig;
  479. if (isset($paymentConfig['express'][$payment_method]['return_url'])) {
  480. if (!empty($paymentConfig['express'][$payment_method]['return_url'])) {
  481. return $this->getUrl($paymentConfig['express'][$payment_method]['return_url']);
  482. }
  483. }
  484. }
  485. }
  486. /**
  487. * @param $payment_method | String 支付方式。
  488. * @return 返回进行数据交互的express的signature。
  489. */
  490. public function getExpressCancelUrl($payment_method = '')
  491. {
  492. if (!$payment_method) {
  493. $payment_method = $this->getPaymentMethod();
  494. }
  495. if ($payment_method) {
  496. $paymentConfig = $this->paymentConfig;
  497. if (isset($paymentConfig['express'][$payment_method]['cancel_url'])) {
  498. if (!empty($paymentConfig['express'][$payment_method]['cancel_url'])) {
  499. return $this->getUrl($paymentConfig['express'][$payment_method]['cancel_url']);
  500. }
  501. }
  502. }
  503. }
  504. /**
  505. * @param $payment_method | String 支付方式。
  506. * @return 第三方支付成功后,返回到网站的url
  507. * #从配置信息中获取
  508. */
  509. public function getExpressSuccessRedirectUrl($payment_method = '')
  510. {
  511. if (!$payment_method) {
  512. $payment_method = $this->getPaymentMethod();
  513. }
  514. if ($payment_method) {
  515. $paymentConfig = $this->paymentConfig;
  516. if (isset($paymentConfig['express'][$payment_method]['success_redirect_url'])) {
  517. if (!empty($paymentConfig['express'][$payment_method]['success_redirect_url'])) {
  518. return $this->getUrl($paymentConfig['express'][$payment_method]['success_redirect_url']);
  519. }
  520. }
  521. }
  522. }
  523. /**
  524. * @param $payment_method | String 支付方式。
  525. * @return 返回进行数据交互的express的signature。
  526. */
  527. public function getExpressIpnUrl($payment_method = '')
  528. {
  529. if (!$payment_method) {
  530. $payment_method = $this->getPaymentMethod();
  531. }
  532. if ($payment_method) {
  533. $paymentConfig = $this->paymentConfig;
  534. if (isset($paymentConfig['express'][$payment_method]['ipn_url'])) {
  535. if (!empty($paymentConfig['express'][$payment_method]['ipn_url'])) {
  536. return $this->getUrl($paymentConfig['express'][$payment_method]['ipn_url']);
  537. }
  538. }
  539. }
  540. }
  541. }