QianBaoService.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. namespace App\Services\Payment;
  3. use GuzzleHttp\Client;
  4. use GuzzleHttp\Exception\RequestException;
  5. use GuzzleHttp\Psr7\Response;
  6. use App\Services\BaseService;
  7. class QianBaoService extends BaseService
  8. {
  9. // SanJin payment service methods would go here
  10. const REQUEST_URL = 'https://api.qbdf13.com/';
  11. // 提现通道
  12. const ALIPAY_TO_CARD = 'DF001'; // 支付宝转卡
  13. const ALIPAY_TO_ALIPAY = 'DF002'; // 支付宝转支付宝
  14. const PROVISION_TO_CARD = 'DF003'; // 备付金转卡
  15. const PROVISION_TO_ALIPAY = 'DF004'; // 备付金转支付宝
  16. const NUMBER_RMB = 'DF005'; // 数字人民币
  17. // 支付通道
  18. const PAY_ZFB1 = 'ZFB001';
  19. public static $PAY_CHANNEL = [
  20. 'ZFB001' => [10,100]
  21. ];
  22. public static function withdrawChannel()
  23. {
  24. return [
  25. self::ALIPAY_TO_CARD => lang('银行卡'),
  26. self::ALIPAY_TO_ALIPAY => lang('支付宝'),
  27. self::NUMBER_RMB => lang('数字人民币'),
  28. ];
  29. }
  30. // 获取异步的通知地址
  31. public static function getNotifyUrl()
  32. {
  33. $host = config('app.url');
  34. return $host.'/api/pay/notify';
  35. }
  36. // 获取商户ID
  37. public static function getMerchantId()
  38. {
  39. return config('app.tree_payment_merchant_id');
  40. }
  41. // 获取商户ID
  42. public static function getSecret()
  43. {
  44. return config('app.tree_payment_secret');
  45. }
  46. /**
  47. * @description: 获取请求客户端
  48. * @return {*}
  49. */
  50. public static function getClient(): Client
  51. {
  52. return new Client([
  53. 'base_uri' => self::REQUEST_URL,
  54. 'timeout' => 5.0,
  55. ]);
  56. }
  57. /**
  58. * @description: 查询商户余额
  59. * @return {array}
  60. * {array.success - 是否成功 true|false}
  61. * {array.msg - 返回错误消息}
  62. * {array.code - 返回状态码:200成功,其他失败}
  63. * {array.timestamp - 时间戳 13位}
  64. * {array.data - 商户余额}
  65. */
  66. public static function findBalance()
  67. {
  68. $merchant_id = self::getMerchantId();
  69. $secret = self::getSecret();
  70. $sign = md5($merchant_id . $secret);
  71. $data = [
  72. 'merchantNum' => $merchant_id,
  73. 'sign' => $sign,
  74. ];
  75. $client = self::getClient();
  76. $response = $client->get('api/findBalance', [
  77. 'query' => $data,
  78. ]);
  79. $body = $response->getBody();
  80. return json_decode($body->getContents(), true);
  81. }
  82. /**
  83. * @description: 查询代付订单信息
  84. * @param {*} $order_no 代付订单
  85. * @return {array} $result
  86. */
  87. public static function findPayoutOrderInfo($order_no)
  88. {
  89. $merchant_id = self::getMerchantId();
  90. $secret = self::getSecret();
  91. $data = [];
  92. $data['merchantNum'] = $merchant_id;
  93. $data['orderNo'] = $order_no;
  94. $sign = md5($merchant_id.$order_no.$secret);
  95. $data['sign'] = $sign;
  96. $client = self::getClient();
  97. $response = $client->get('api/findPayoutOrderInfo', [
  98. 'query' => $data,
  99. ]);
  100. $body = $response->getBody();
  101. return json_decode($body->getContents(), true);
  102. }
  103. /**
  104. * @description: 查询代收订单信息
  105. * @param {*} $order_no 代收订单
  106. * @return {array} $result
  107. */
  108. public static function findPayOrderInfo($order_no)
  109. {
  110. $merchant_id = self::getMerchantId();
  111. $secret = self::getSecret();
  112. $data = [];
  113. $data['merchantNum'] = $merchant_id;
  114. $data['orderNo'] = $order_no;
  115. $sign = md5($merchant_id.$order_no.$secret);
  116. $data['sign'] = $sign;
  117. $client = self::getClient();
  118. $response = $client->get('api/findPayOrderInfo', [
  119. 'query' => $data,
  120. ]);
  121. $body = $response->getBody();
  122. return json_decode($body->getContents(), true);
  123. }
  124. // 代收
  125. public static function pay($amount, $order_no, $payType = self::PAY_ZFB1)
  126. {
  127. $data = [];
  128. $merchant_id = self::getMerchantId();
  129. $secret = self::getSecret();
  130. $notify_url = self::getNotifyUrl();
  131. $data['merchantNum'] = $merchant_id;
  132. $data['amount'] = $amount;
  133. $data['orderNo'] = $order_no;
  134. $data['notifyUrl'] = $notify_url;
  135. $data['payType'] = $payType;
  136. // $data['returnUrl'] = $return_url;
  137. $signStr = $merchant_id . $order_no . $amount . $secret;
  138. $sign = md5($signStr);
  139. $data['sign'] = $sign;
  140. $client = self::getClient();
  141. $response = $client->post('api/pay', [
  142. 'form_params' => $data,
  143. 'headers' => [
  144. 'Content-Type' => 'application/x-www-form-urlencoded',
  145. ]
  146. ]);
  147. $body = $response->getBody();
  148. $result = json_decode($body->getContents(), true);
  149. return $result;
  150. }
  151. // 代付
  152. public static function payout($amount, $order_no, $bank_name, $account, $card_no, $payType = self::ALIPAY_TO_ALIPAY)
  153. {
  154. $data = [];
  155. $merchant_id = self::getMerchantId();
  156. $secret = self::getSecret();
  157. $notify_url = self::getNotifyUrl();
  158. $data['merchantNum'] = $merchant_id;
  159. $data['amount'] = $amount;
  160. $data['orderNo'] = $order_no;
  161. $data['notifyUrl'] = $notify_url;
  162. $data['payType'] = $payType;
  163. switch($payType){
  164. case self::ALIPAY_TO_ALIPAY:
  165. $bank_name = '支付宝';
  166. break;
  167. case self::NUMBER_RMB:
  168. $bank_name = '数字人民币';
  169. break;
  170. }
  171. $signStr = $merchant_id . $order_no . $amount . $secret;
  172. $sign = md5($signStr);
  173. $data['sign'] = $sign;
  174. $data['bankName'] = $bank_name;
  175. $data['account'] = $account;
  176. $data['cardNumber'] = $card_no;
  177. $client = self::getClient();
  178. $response = $client->post('api/payout', [
  179. 'form_params' => $data,
  180. 'headers' => [
  181. 'Content-Type' => 'application/x-www-form-urlencoded',
  182. ]
  183. ]);
  184. $body = $response->getBody();
  185. $result = json_decode($body->getContents(), true);
  186. return $result;
  187. }
  188. /**
  189. * @description: 异步通知签名加密
  190. * @param {*} $state
  191. * @param {*} $order_no
  192. * @param {*} $amount
  193. * @return {*}
  194. */
  195. public static function verifyNotifySign($state,$order_no,$amount)
  196. {
  197. $merchant_id = self::getMerchantId();
  198. $secret = self::getSecret();
  199. $sign = md5($state.$merchant_id.$order_no.$amount.$secret);
  200. return $sign;
  201. }
  202. public static function getWhere(array $search = []): array
  203. {
  204. return [];
  205. }
  206. }