Payment.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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. $checkMoneyConfig = Yii::$app->store->get($appName.'_payment', $payment_type);
  237. if ($checkMoneyConfig != Yii::$app->store->enable) {
  238. continue;
  239. }
  240. $label = $info['label'];
  241. $imageUrl = '';
  242. if (is_array($info['image'])) {
  243. list($iUrl, $l) = $info['image'];
  244. if ($iUrl) {
  245. $imageUrl = Yii::$service->image->getImgUrl($iUrl, $l);
  246. }
  247. }
  248. $supplement = $info['supplement'];
  249. $arr[$payment_type] = [
  250. 'label' => $label,
  251. 'imageUrl' => $imageUrl,
  252. 'supplement' => $supplement,
  253. ];
  254. }
  255. }
  256. return $arr;
  257. }
  258. /**
  259. * @param $payment_method | String , 支付方式
  260. * @return bool 判断传递的支付方式,是否在配置中存在,如果存在返回true。
  261. */
  262. protected function actionIfIsCorrectStandard($payment_method)
  263. {
  264. $paymentConfig = $this->paymentConfig;
  265. $standard = isset($paymentConfig['standard']) ? $paymentConfig['standard'] : '';
  266. if (isset($standard[$payment_method]) && !empty($standard[$payment_method])) {
  267. return true;
  268. } else {
  269. return false;
  270. }
  271. }
  272. public function getStandardNvpUrl($payment_method = '')
  273. {
  274. if (!$payment_method) {
  275. $payment_method = $this->getPaymentMethod();
  276. }
  277. if ($payment_method) {
  278. $paymentConfig = $this->paymentConfig;
  279. if (isset($paymentConfig['standard'][$payment_method]['nvp_url'])) {
  280. if (!empty($paymentConfig['standard'][$payment_method]['nvp_url'])) {
  281. return $paymentConfig['standard'][$payment_method]['nvp_url'];
  282. }
  283. }
  284. }
  285. }
  286. /**
  287. * @param $payment_method | String 支付方式。
  288. * @return 返回支付方式的label
  289. */
  290. public function getPaymentLabelByMethod($payment_method = '')
  291. {
  292. $payment_method_label = $this->getStandardLabel($payment_method);
  293. if (!$payment_method_label) {
  294. $payment_method_label = $this->getExpressLabel($payment_method);
  295. }
  296. if ($payment_method_label) {
  297. return $payment_method_label;
  298. } else {
  299. return $payment_method;
  300. }
  301. }
  302. /**
  303. * @param $payment_method | String 支付方式。
  304. * @return 返回进行数据交互的express的label。
  305. */
  306. public function getStandardLabel($payment_method = '')
  307. {
  308. if (!$payment_method) {
  309. $payment_method = $this->getPaymentMethod();
  310. }
  311. if ($payment_method) {
  312. $paymentConfig = $this->paymentConfig;
  313. if (isset($paymentConfig['standard'][$payment_method]['label'])) {
  314. if (!empty($paymentConfig['standard'][$payment_method]['label'])) {
  315. return $paymentConfig['standard'][$payment_method]['label'];
  316. }
  317. }
  318. }
  319. }
  320. /**
  321. * @param $payment_method | String 支付方式。
  322. * @return 返回进行数据交互的express的signature。
  323. */
  324. public function getStandardIpnUrl($payment_method = '')
  325. {
  326. if (!$payment_method) {
  327. $payment_method = $this->getPaymentMethod();
  328. }
  329. if ($payment_method) {
  330. $paymentConfig = $this->paymentConfig;
  331. if (isset($paymentConfig['standard'][$payment_method]['ipn_url'])) {
  332. if (!empty($paymentConfig['standard'][$payment_method]['ipn_url'])) {
  333. return $this->getUrl($paymentConfig['standard'][$payment_method]['ipn_url']);
  334. }
  335. }
  336. }
  337. }
  338. /**
  339. * @param $payment_method | String 支付方式。
  340. * @return 返回进行数据交互的express的signature。
  341. */
  342. public function getStandardReturnUrl($payment_method = '')
  343. {
  344. if (!$payment_method) {
  345. $payment_method = $this->getPaymentMethod();
  346. }
  347. if ($payment_method) {
  348. $paymentConfig = $this->paymentConfig;
  349. if (isset($paymentConfig['standard'][$payment_method]['return_url'])) {
  350. if (!empty($paymentConfig['standard'][$payment_method]['return_url'])) {
  351. return $this->getUrl($paymentConfig['standard'][$payment_method]['return_url']);
  352. }
  353. }
  354. }
  355. }
  356. //###################
  357. //# Express 部分 ##
  358. //###################
  359. /**
  360. * @param $payment_method | String 支付方式。
  361. * @return 返回获取token的url地址。
  362. */
  363. public function getExpressNvpUrl($payment_method = '')
  364. {
  365. if (!$payment_method) {
  366. $payment_method = $this->getPaymentMethod();
  367. }
  368. if ($payment_method) {
  369. $paymentConfig = $this->paymentConfig;
  370. if (isset($paymentConfig['express'][$payment_method]['nvp_url'])) {
  371. if (!empty($paymentConfig['express'][$payment_method]['nvp_url'])) {
  372. return $paymentConfig['express'][$payment_method]['nvp_url'];
  373. }
  374. }
  375. }
  376. }
  377. /**
  378. * @param $payment_method | String 支付方式。
  379. * @return 返回进行数据交互的express的api地址。
  380. */
  381. public function getExpressWebscrUrl($payment_method = '')
  382. {
  383. if (!$payment_method) {
  384. $payment_method = $this->getPaymentMethod();
  385. }
  386. if ($payment_method) {
  387. $paymentConfig = $this->paymentConfig;
  388. if (isset($paymentConfig['express'][$payment_method]['webscr_url'])) {
  389. if (!empty($paymentConfig['express'][$payment_method]['webscr_url'])) {
  390. return $paymentConfig['express'][$payment_method]['webscr_url'];
  391. }
  392. }
  393. }
  394. }
  395. /**
  396. * @param $payment_method | String 支付方式。
  397. * @return 返回进行数据交互的express的account。
  398. */
  399. public function getExpressAccount($payment_method = '')
  400. {
  401. if (!$payment_method) {
  402. $payment_method = $this->getPaymentMethod();
  403. }
  404. if ($payment_method) {
  405. $paymentConfig = $this->paymentConfig;
  406. if (isset($paymentConfig['express'][$payment_method]['account'])) {
  407. if (!empty($paymentConfig['express'][$payment_method]['account'])) {
  408. return $paymentConfig['express'][$payment_method]['account'];
  409. }
  410. }
  411. }
  412. }
  413. /**
  414. * @param $payment_method | String 支付方式。
  415. * @return 返回进行数据交互的express的password。
  416. */
  417. public function getExpressPassword($payment_method = '')
  418. {
  419. if (!$payment_method) {
  420. $payment_method = $this->getPaymentMethod();
  421. }
  422. if ($payment_method) {
  423. $paymentConfig = $this->paymentConfig;
  424. if (isset($paymentConfig['express'][$payment_method]['password'])) {
  425. if (!empty($paymentConfig['express'][$payment_method]['password'])) {
  426. return $paymentConfig['express'][$payment_method]['password'];
  427. }
  428. }
  429. }
  430. }
  431. /**
  432. * @param $payment_method | String 支付方式。
  433. * @return 返回进行数据交互的express的signature。
  434. */
  435. public function getExpressSignature($payment_method = '')
  436. {
  437. if (!$payment_method) {
  438. $payment_method = $this->getPaymentMethod();
  439. }
  440. if ($payment_method) {
  441. $paymentConfig = $this->paymentConfig;
  442. if (isset($paymentConfig['express'][$payment_method]['signature'])) {
  443. if (!empty($paymentConfig['express'][$payment_method]['signature'])) {
  444. return $paymentConfig['express'][$payment_method]['signature'];
  445. }
  446. }
  447. }
  448. }
  449. /**
  450. * @param $payment_method | String 支付方式。
  451. * @return 返回进行数据交互的express的label。
  452. */
  453. public function getExpressLabel($payment_method = '')
  454. {
  455. if (!$payment_method) {
  456. $payment_method = $this->getPaymentMethod();
  457. }
  458. if ($payment_method) {
  459. $paymentConfig = $this->paymentConfig;
  460. if (isset($paymentConfig['express'][$payment_method]['label'])) {
  461. if (!empty($paymentConfig['express'][$payment_method]['label'])) {
  462. return $paymentConfig['express'][$payment_method]['label'];
  463. }
  464. }
  465. }
  466. }
  467. /**
  468. * @param $payment_method | String 支付方式。
  469. * @return 返回进行数据交互的express的signature。
  470. */
  471. public function getExpressReturnUrl($payment_method = '')
  472. {
  473. if (!$payment_method) {
  474. $payment_method = $this->getPaymentMethod();
  475. }
  476. if ($payment_method) {
  477. $paymentConfig = $this->paymentConfig;
  478. if (isset($paymentConfig['express'][$payment_method]['return_url'])) {
  479. if (!empty($paymentConfig['express'][$payment_method]['return_url'])) {
  480. return $this->getUrl($paymentConfig['express'][$payment_method]['return_url']);
  481. }
  482. }
  483. }
  484. }
  485. /**
  486. * @param $payment_method | String 支付方式。
  487. * @return 返回进行数据交互的express的signature。
  488. */
  489. public function getExpressCancelUrl($payment_method = '')
  490. {
  491. if (!$payment_method) {
  492. $payment_method = $this->getPaymentMethod();
  493. }
  494. if ($payment_method) {
  495. $paymentConfig = $this->paymentConfig;
  496. if (isset($paymentConfig['express'][$payment_method]['cancel_url'])) {
  497. if (!empty($paymentConfig['express'][$payment_method]['cancel_url'])) {
  498. return $this->getUrl($paymentConfig['express'][$payment_method]['cancel_url']);
  499. }
  500. }
  501. }
  502. }
  503. /**
  504. * @param $payment_method | String 支付方式。
  505. * @return 第三方支付成功后,返回到网站的url
  506. * #从配置信息中获取
  507. */
  508. public function getExpressSuccessRedirectUrl($payment_method = '')
  509. {
  510. if (!$payment_method) {
  511. $payment_method = $this->getPaymentMethod();
  512. }
  513. if ($payment_method) {
  514. $paymentConfig = $this->paymentConfig;
  515. if (isset($paymentConfig['express'][$payment_method]['success_redirect_url'])) {
  516. if (!empty($paymentConfig['express'][$payment_method]['success_redirect_url'])) {
  517. return $this->getUrl($paymentConfig['express'][$payment_method]['success_redirect_url']);
  518. }
  519. }
  520. }
  521. }
  522. /**
  523. * @param $payment_method | String 支付方式。
  524. * @return 返回进行数据交互的express的signature。
  525. */
  526. public function getExpressIpnUrl($payment_method = '')
  527. {
  528. if (!$payment_method) {
  529. $payment_method = $this->getPaymentMethod();
  530. }
  531. if ($payment_method) {
  532. $paymentConfig = $this->paymentConfig;
  533. if (isset($paymentConfig['express'][$payment_method]['ipn_url'])) {
  534. if (!empty($paymentConfig['express'][$payment_method]['ipn_url'])) {
  535. return $this->getUrl($paymentConfig['express'][$payment_method]['ipn_url']);
  536. }
  537. }
  538. }
  539. }
  540. }