PayService.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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\dict\pay\PaySceneDict;
  14. use app\model\member\Member;
  15. use app\model\pay\Pay;
  16. use app\model\sys\Poster;
  17. use app\service\core\member\CoreMemberService;
  18. use app\service\core\pay\CorePayService;
  19. use core\base\BaseApiService;
  20. use core\exception\ApiException;
  21. use think\db\exception\DataNotFoundException;
  22. use think\db\exception\DbException;
  23. use think\db\exception\ModelNotFoundException;
  24. /**
  25. * 支付业务
  26. */
  27. class PayService extends BaseApiService
  28. {
  29. public $core_pay_service;
  30. public function __construct()
  31. {
  32. parent::__construct();
  33. $this->core_pay_service = new CorePayService();
  34. }
  35. /**
  36. * 去支付
  37. * @param string $type
  38. * @param string $trade_type
  39. * @param int $trade_id
  40. * @param string $return_url
  41. * @param string $quit_url
  42. * @param string $buyer_id
  43. * @return mixed
  44. * @throws DataNotFoundException
  45. * @throws DbException
  46. * @throws ModelNotFoundException
  47. */
  48. public function pay(string $type, string $trade_type, int $trade_id, string $return_url = '', string $quit_url = '', string $buyer_id = '', string $voucher = '', string $openid = ''){
  49. $member = (new CoreMemberService())->getInfoByMemberId($this->member_id);
  50. switch ($this->channel) {
  51. case ChannelDict::WECHAT://公众号
  52. $openid = $openid ? $openid : $member['wx_openid'] ?? '';
  53. break;
  54. case ChannelDict::WEAPP://微信小程序
  55. $openid = $openid ? $openid : $member['weapp_openid'] ?? '';
  56. break;
  57. }
  58. return $this->core_pay_service->pay($trade_type, $trade_id, $type, $this->channel, $openid, $return_url, $quit_url, $buyer_id, $voucher, $this->member_id);
  59. }
  60. /**
  61. * 关闭支付
  62. * @param string $type
  63. * @param string $out_trade_no
  64. * @return null
  65. */
  66. public function close(string $type, string $out_trade_no){
  67. return $this->core_pay_service->close($out_trade_no);
  68. }
  69. /**
  70. * 支付异步通知
  71. * @param string $channel
  72. * @param string $type
  73. * @param string $action
  74. * @return void|null
  75. */
  76. public function notify(string $channel, string $type, string $action){
  77. return $this->core_pay_service->notify($channel, $type, $action);
  78. }
  79. /**
  80. * 通过交易流水号查询支付信息以及支付方式
  81. * @param $out_trade_no
  82. * @return array
  83. */
  84. public function getInfoByOutTradeNo($out_trade_no){
  85. return $this->core_pay_service->getInfoByOutTradeNo($out_trade_no, $this->channel);
  86. }
  87. public function getInfoByTrade(string $trade_type, int $trade_id, array $data){
  88. return $this->core_pay_service->getInfoByTrade($trade_type, $trade_id, $this->channel, $data['scene']);
  89. }
  90. /**
  91. * 获取找朋友帮忙付支付信息
  92. * @param string $trade_type
  93. * @param int $trade_id
  94. * @return array
  95. */
  96. public function getFriendspayInfoByTrade($trade_type, $trade_id){
  97. $pay_info = $this->core_pay_service->getInfoByTrade($trade_type, $trade_id, $this->channel, PaySceneDict::FRIENDSPAY);
  98. if (!empty($pay_info)) {
  99. //todo 查询订单交易信息,其它插件可实现该钩子
  100. $trade_info = array_values(array_filter(event('PayTradeInfo',[ 'trade_type' => $trade_type, 'trade_id' => $trade_id ])))[0] ?? [];
  101. $pay_info['trade_info'] = $trade_info;
  102. if ($pay_info['from_main_id'] != $this->member_id) {
  103. $pay_info['is_self'] = false;
  104. } else {
  105. $pay_info['is_self'] = true;
  106. }
  107. //海报
  108. $poster = ( new Poster() )->field('id')->where([
  109. [ 'type', '=', 'friendspay' ],
  110. [ 'status', '=', 1 ],
  111. [ 'is_default', '=', 1 ]
  112. ])->findOrEmpty()->toArray();
  113. if (!empty($poster)) {
  114. $pay_info['poster_id'] = $poster['id'];
  115. }
  116. //发起帮付会员信息
  117. $member = ( new Member() )->field('member_id,nickname,headimg')->where([
  118. [ 'member_id', '=', $pay_info['from_main_id'] ]
  119. ])->findOrEmpty()->toArray();
  120. $pay_info['member'] = $member;
  121. }
  122. return $pay_info;
  123. }
  124. /**
  125. * 获取支付方法
  126. * @param string $trade_type
  127. * @return array|array[]
  128. * @throws DataNotFoundException
  129. * @throws DbException
  130. * @throws ModelNotFoundException
  131. */
  132. public function getPayTypeByTrade(string $trade_type){
  133. return $this->core_pay_service->getPayTypeByTrade($trade_type, $this->channel);
  134. }
  135. }