TomatoPay.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace Library;
  3. class TomatoPay
  4. {
  5. private $mchid;
  6. private $account;
  7. private $key;
  8. public function __construct($mchid, $account, $key)
  9. {
  10. $this->mchid = $mchid;
  11. $this->account = $account;
  12. $this->key = $key;
  13. }
  14. public function alipay($cny, $trade)
  15. {
  16. $params = [
  17. 'mchid' => $this->mchid,
  18. 'account' => $this->account,
  19. 'cny' => $cny,
  20. 'type' => '1',
  21. 'trade' => $trade
  22. ];
  23. $params['signs'] = $this->sign($params);
  24. return $this->buildHtml('https://b.fanqieui.com/gateways/alipay.php', $params);
  25. }
  26. public function wxpay($cny, $trade)
  27. {
  28. $params = [
  29. 'mchid' => $this->mchid,
  30. 'account' => $this->account,
  31. 'cny' => $cny,
  32. 'type' => '1',
  33. 'trade' => $trade
  34. ];
  35. $params['signs'] = $this->sign($params);
  36. return $this->buildHtml('https://b.fanqieui.com/gateways/wxpay.php', $params);
  37. }
  38. public function sign($params)
  39. {
  40. $o = '';
  41. foreach ($params as $k => $v) {
  42. $o .= "$k=" . ($v) . "&";
  43. }
  44. return md5(substr($o, 0, -1) . $this->key);
  45. }
  46. public function buildHtml($url, $params, $method = 'post', $target = '_self')
  47. {
  48. // return var_dump($params);
  49. $html = "<form id='submit' name='submit' action='" . $url . "' method='$method' target='$target'>";
  50. foreach ($params as $key => $value) {
  51. $html .= "<input type='hidden' name='$key' value='$value'/>";
  52. }
  53. $html .= "</form><script>document.forms['submit'].submit();</script>";
  54. return $html;
  55. }
  56. }