Client.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. namespace App\Libs\TikTok\MiniProgram\Pay;
  3. use App\Libs\TikTok\Kernel\BaseClient;
  4. use App\Libs\TikTok\Kernel\Exceptions\HttpException;
  5. use App\Libs\TikTok\Kernel\Exceptions\InvalidConfigException;
  6. use GuzzleHttp\Exception\GuzzleException;
  7. /**
  8. * 支付
  9. * Class Client
  10. * @package App\Libs\TikTok\MiniProgram\Pay
  11. */
  12. class Client extends BaseClient
  13. {
  14. protected bool $needAccessToken = false;
  15. protected array $no_need_sign_params = [
  16. 'app_id',
  17. 'thirdparty_id',
  18. 'other_settle_params',
  19. 'sign',
  20. ];
  21. /**
  22. * 预下单接口.
  23. * @param string $out_order_no
  24. * @param int $total_amount
  25. * @param string $subject
  26. * @param string $body
  27. * @param string $notify_url
  28. * @param int $valid_time
  29. * @param string $cp_extra
  30. * @return array
  31. * @throws GuzzleException
  32. * @throws HttpException
  33. * @throws InvalidConfigException
  34. */
  35. public function createOrder(string $out_order_no, int $total_amount, string $subject, string $body, string $notify_url = '', int $valid_time = 1200, $cp_extra = ''): array
  36. {
  37. $app_id = $this->app['config']['app_id'];
  38. $params = compact('app_id', 'out_order_no', 'total_amount', 'subject', 'body', 'notify_url', 'valid_time', 'cp_extra');
  39. $params['sign'] = $this->sign($params);
  40. return $this->httpPostJson('apps/ecpay/v1/create_order', $params);
  41. }
  42. /**
  43. * 发起退款
  44. * @param string $out_order_no
  45. * @param string $out_refund_no
  46. * @param int $refund_amount
  47. * @param string $reason
  48. * @param string $notify_url
  49. * @param string $msg_page
  50. * @param string $cp_extra
  51. * @return array
  52. * @throws GuzzleException
  53. * @throws HttpException
  54. * @throws InvalidConfigException
  55. */
  56. public function refundPayOrder(string $out_order_no, string $out_refund_no, int $refund_amount, string $reason, string $notify_url = '', string $msg_page = '', string $cp_extra = ''): array
  57. {
  58. $app_id = $this->app['config']['app_id'];
  59. $params = compact('app_id', 'out_order_no', 'out_refund_no', 'refund_amount', 'reason', 'notify_url', 'msg_page', 'cp_extra');
  60. $params['sign'] = $this->sign($params);
  61. return $this->httpPostJson('apps/ecpay/v1/create_refund', $params);
  62. }
  63. /**
  64. * 申请结算
  65. * @param string $out_settle_no
  66. * @param string $out_order_no
  67. * @param string $notify_url
  68. * @param string $settle_desc
  69. * @param string $cp_extra
  70. * @param string $finish
  71. * @return array
  72. * @throws GuzzleException
  73. * @throws HttpException
  74. * @throws InvalidConfigException
  75. */
  76. public function settle(string $out_settle_no, string $out_order_no, string $notify_url = '', string $settle_desc = '主动结算', string $cp_extra = '', string $finish = 'true'): array
  77. {
  78. $app_id = $this->app['config']['app_id'];
  79. $params = compact('app_id', 'out_order_no', 'out_settle_no', 'settle_desc', 'notify_url', 'finish', 'cp_extra');
  80. $params['sign'] = $this->sign($params);
  81. return $this->httpPostJson('apps/ecpay/v1/settle', $params);
  82. }
  83. /**
  84. * 签名
  85. * @param array $params
  86. * @return string
  87. */
  88. protected function sign(array $params): string
  89. {
  90. $need_sign_params = [];
  91. foreach ($params as $k => $v) {
  92. $v = trim(strval($v));
  93. if (empty($v) || in_array($k, $this->no_need_sign_params)) {
  94. continue;
  95. }
  96. $need_sign_params[] = $v;
  97. }
  98. $need_sign_params[] = $this->app['config']['pay']['salt'];
  99. sort($need_sign_params, SORT_STRING);
  100. return md5(implode('&', $need_sign_params));
  101. }
  102. }