TomatoPay.php 1.6 KB

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