ByteDancePay.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. namespace common\components\payment;
  3. use linslin\yii2\curl\Curl;
  4. use yii\helpers\Json;
  5. use yii\web\UnprocessableEntityHttpException;
  6. /**
  7. * 字节跳动小程序
  8. *
  9. * Class ByteDancePay
  10. * @package common\components\payment
  11. */
  12. class ByteDancePay
  13. {
  14. const URL = 'https://developer.toutiao.com/';
  15. /**
  16. * UnionPay constructor.
  17. */
  18. public function __construct($config)
  19. {
  20. $this->config = $config;
  21. }
  22. /**
  23. * 创建支付订单
  24. *
  25. * @param array $params
  26. * @return bool|mixed|string
  27. * @throws \Exception
  28. */
  29. public function create(array $params)
  30. {
  31. $params['app_id'] = $this->config['app_id'];
  32. $params['valid_time'] = time() + 2 * 3600 * 24;
  33. $params['sign'] = $this->getSign($params);
  34. return $this->httpPost('api/apps/ecpay/v1/create_order', $params);
  35. }
  36. /**
  37. * 查询
  38. *
  39. * @param int $out_order_no 退款单号
  40. * @param string $thirdparty_id
  41. * @return bool|mixed|string
  42. * @throws \Exception
  43. */
  44. public function query($out_order_no, $thirdparty_id = '')
  45. {
  46. $params = [];
  47. $params['app_id'] = $this->config['app_id'];
  48. $params['out_order_no'] = $out_order_no;
  49. $params['thirdparty_id'] = $thirdparty_id;
  50. $params['sign'] = $this->getSign($params);
  51. return $this->httpPost('api/apps/ecpay/v1/query_order', $params);
  52. }
  53. /**
  54. * 退款
  55. *
  56. * @param $params
  57. * @return bool|mixed|string
  58. * @throws \Exception
  59. */
  60. public function refund(array $params)
  61. {
  62. $params['app_id'] = $this->config['app_id'];
  63. $params['sign'] = $this->getSign($params);
  64. return $this->httpPost('api/apps/ecpay/v1/create_refund', $params);
  65. }
  66. /**
  67. * 退款查询
  68. *
  69. * @param int $out_refund_no 退款单号
  70. * @param string $thirdparty_id
  71. * @return bool|mixed|string
  72. * @throws \Exception
  73. */
  74. public function queryRefund($out_refund_no, $thirdparty_id = '')
  75. {
  76. $params['app_id'] = $this->config['app_id'];
  77. $params['out_order_no'] = $out_refund_no;
  78. $params['thirdparty_id'] = $thirdparty_id;
  79. $params['sign'] = $this->getSign($params);
  80. return $this->httpPost('api/apps/ecpay/v1/query_refund', $params);
  81. }
  82. /**
  83. * 签名校验
  84. *
  85. * @param $params
  86. * @return bool
  87. */
  88. public function isPaid($params)
  89. {
  90. if ($params['type'] !== 'payment') {
  91. return false;
  92. }
  93. $sign = $params["msg_signature"];
  94. unset($params["msg_signature"]);
  95. unset($params["type"]);
  96. $params['token'] = trim($this->config['app_token']);
  97. sort($params, 2);
  98. $signStr = trim(implode('', $params));
  99. return sha1($signStr) === $sign;
  100. }
  101. /**
  102. * 计算签名
  103. *
  104. * @param $params
  105. * @return string
  106. */
  107. protected function getSign($params)
  108. {
  109. unset($params["sign"]);
  110. unset($params["app_id"]);
  111. unset($params["thirdparty_id"]);
  112. $paramArray = [];
  113. foreach ($params as $param) {
  114. $paramArray[] = trim($param);
  115. }
  116. $paramArray[] = trim($this->config['app_salt']);
  117. sort($paramArray, 2);
  118. $signStr = trim(implode('&', $paramArray));
  119. return md5($signStr);
  120. }
  121. /**
  122. * @param $url
  123. * @param array $params
  124. * @return bool|mixed|string
  125. * @throws \Exception
  126. */
  127. protected function httpPost($url, $body = [])
  128. {
  129. $curl = new Curl();
  130. $result = $curl->setRawPostData(Json::encode($body))->post(self::URL . $url);
  131. $result = Json::decode($result);
  132. if ($result['err_no'] == 0) {
  133. return $result['data'] ?? '';
  134. }
  135. throw new UnprocessableEntityHttpException($result['err_no'] . ':' . $result['err_tips']);
  136. }
  137. }
粤ICP备19079148号