PayService.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Niucloud-admin 企业快速开发的saas管理平台
  4. // +----------------------------------------------------------------------
  5. // | 官方网址:https://www.niucloud.com
  6. // +----------------------------------------------------------------------
  7. // | niucloud团队 版权所有 开源版本可自由商用
  8. // +----------------------------------------------------------------------
  9. // | Author: Niucloud Team
  10. // +----------------------------------------------------------------------
  11. namespace app\service\api\pay;
  12. use app\dict\common\ChannelDict;
  13. use app\service\core\member\CoreMemberService;
  14. use app\service\core\pay\CorePayService;
  15. use core\base\BaseApiService;
  16. use think\db\exception\DataNotFoundException;
  17. use think\db\exception\DbException;
  18. use think\db\exception\ModelNotFoundException;
  19. /**
  20. * 支付业务
  21. */
  22. class PayService extends BaseApiService
  23. {
  24. public $core_pay_service;
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. $this->core_pay_service = new CorePayService();
  29. }
  30. /**
  31. * 去支付
  32. * @param string $type
  33. * @param string $trade_type
  34. * @param int $trade_id
  35. * @param string $return_url
  36. * @param string $quit_url
  37. * @param string $buyer_id
  38. * @return mixed
  39. * @throws DataNotFoundException
  40. * @throws DbException
  41. * @throws ModelNotFoundException
  42. */
  43. public function pay(string $type, string $trade_type, int $trade_id, string $return_url = '', string $quit_url = '', string $buyer_id = '', string $voucher = '', string $openid = ''){
  44. $member = (new CoreMemberService())->getInfoByMemberId($this->member_id);
  45. switch ($this->channel) {
  46. case ChannelDict::WECHAT://公众号
  47. $openid = $openid ? $openid : $member['wx_openid'] ?? '';
  48. break;
  49. case ChannelDict::WEAPP://微信小程序
  50. $openid = $openid ? $openid : $member['weapp_openid'] ?? '';
  51. break;
  52. }
  53. return $this->core_pay_service->pay($trade_type, $trade_id, $type, $this->channel, $openid, $return_url, $quit_url, $buyer_id, $voucher);
  54. }
  55. /**
  56. * 关闭支付
  57. * @param string $type
  58. * @param string $out_trade_no
  59. * @return null
  60. */
  61. public function close(string $type, string $out_trade_no){
  62. return $this->core_pay_service->close($out_trade_no);
  63. }
  64. /**
  65. * 支付异步通知
  66. * @param string $channel
  67. * @param string $type
  68. * @param string $action
  69. * @return void|null
  70. */
  71. public function notify(string $channel, string $type, string $action){
  72. return $this->core_pay_service->notify($channel, $type, $action);
  73. }
  74. /**
  75. * 通过交易流水号查询支付信息以及支付方式
  76. * @param $out_trade_no
  77. * @return array
  78. */
  79. public function getInfoByOutTradeNo($out_trade_no){
  80. return $this->core_pay_service->getInfoByOutTradeNo($out_trade_no, $this->channel);
  81. }
  82. public function getInfoByTrade(string $trade_type, int $trade_id){
  83. return $this->core_pay_service->getInfoByTrade($trade_type, $trade_id, $this->channel);
  84. }
  85. /**
  86. * 获取支付方法
  87. * @param string $trade_type
  88. * @return array|array[]
  89. * @throws DataNotFoundException
  90. * @throws DbException
  91. * @throws ModelNotFoundException
  92. */
  93. public function getPayTypeByTrade(string $trade_type){
  94. return $this->core_pay_service->getPayTypeByTrade($trade_type, $this->channel);
  95. }
  96. }