Client.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /*
  3. * This file is part of the overtrue/wechat.
  4. *
  5. * (c) overtrue <i@overtrue.me>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace EasyWeChat\Payment\Transfer;
  11. use EasyWeChat\Kernel\Exceptions\RuntimeException;
  12. use EasyWeChat\Payment\Kernel\BaseClient;
  13. use function EasyWeChat\Kernel\Support\get_server_ip;
  14. use function EasyWeChat\Kernel\Support\rsa_public_encrypt;
  15. /**
  16. * Class Client.
  17. *
  18. * @author AC <alexever@gmail.com>
  19. */
  20. class Client extends BaseClient
  21. {
  22. /**
  23. * Query MerchantPay to balance.
  24. *
  25. * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
  26. *
  27. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  28. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  29. * @throws \GuzzleHttp\Exception\GuzzleException
  30. */
  31. public function queryBalanceOrder(string $partnerTradeNo)
  32. {
  33. $params = [
  34. 'appid' => $this->app['config']->app_id,
  35. 'mch_id' => $this->app['config']->mch_id,
  36. 'partner_trade_no' => $partnerTradeNo,
  37. ];
  38. return $this->safeRequest('mmpaymkttransfers/gettransferinfo', $params);
  39. }
  40. /**
  41. * Send MerchantPay to balance.
  42. *
  43. * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
  44. *
  45. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  46. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  47. * @throws \GuzzleHttp\Exception\GuzzleException
  48. */
  49. public function toBalance(array $params)
  50. {
  51. $base = [
  52. 'mch_id' => null,
  53. 'mchid' => $this->app['config']->mch_id,
  54. 'mch_appid' => $this->app['config']->app_id,
  55. ];
  56. if (empty($params['spbill_create_ip'])) {
  57. $params['spbill_create_ip'] = get_server_ip();
  58. }
  59. return $this->safeRequest('mmpaymkttransfers/promotion/transfers', array_merge($base, $params));
  60. }
  61. /**
  62. * Query MerchantPay order to BankCard.
  63. *
  64. * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
  65. *
  66. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  67. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  68. * @throws \GuzzleHttp\Exception\GuzzleException
  69. */
  70. public function queryBankCardOrder(string $partnerTradeNo)
  71. {
  72. $params = [
  73. 'mch_id' => $this->app['config']->mch_id,
  74. 'partner_trade_no' => $partnerTradeNo,
  75. ];
  76. return $this->safeRequest('mmpaysptrans/query_bank', $params);
  77. }
  78. /**
  79. * Send MerchantPay to BankCard.
  80. *
  81. * @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
  82. *
  83. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  84. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  85. * @throws \EasyWeChat\Kernel\Exceptions\RuntimeException
  86. * @throws \GuzzleHttp\Exception\GuzzleException
  87. */
  88. public function toBankCard(array $params)
  89. {
  90. foreach (['bank_code', 'partner_trade_no', 'enc_bank_no', 'enc_true_name', 'amount'] as $key) {
  91. if (empty($params[$key])) {
  92. throw new RuntimeException(\sprintf('"%s" is required.', $key));
  93. }
  94. }
  95. $publicKey = file_get_contents($this->app['config']->get('rsa_public_key_path'));
  96. $params['enc_bank_no'] = rsa_public_encrypt($params['enc_bank_no'], $publicKey);
  97. $params['enc_true_name'] = rsa_public_encrypt($params['enc_true_name'], $publicKey);
  98. return $this->safeRequest('mmpaysptrans/pay_bank', $params);
  99. }
  100. }