Payment.php 19 KB

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