WebGateway.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. namespace Yansongda\Pay\Gateways\Wechat;
  3. use Yansongda\Pay\Exceptions\InvalidArgumentException;
  4. class WebGateway extends Wechat
  5. {
  6. /**
  7. * get trade type config.
  8. *
  9. * @author yansongda <me@yansongda.cn>
  10. *
  11. * @return string
  12. */
  13. protected function getTradeType()
  14. {
  15. return 'NATIVE';
  16. }
  17. /**
  18. * pay a order.
  19. *
  20. * @author yansongda <me@yansongda.cn>
  21. *
  22. * @param array $config_biz
  23. *
  24. * @return string
  25. */
  26. public function pay(array $config_biz = [])
  27. {
  28. if (is_null($this->user_config->get('app_id'))) {
  29. throw new InvalidArgumentException('Missing Config -- [app_id]');
  30. }
  31. $code_url = $this->preOrder($config_biz)['code_url'];
  32. $params = [
  33. 'body' => $config_biz['body'],
  34. 'code_url' => $code_url,
  35. 'out_trade_no' => $config_biz['out_trade_no'],
  36. 'return_url' => $this->user_config->get('return_url'),
  37. 'total_fee' => $config_biz['total_fee'],
  38. ];
  39. $params['sign'] = md5(implode('', $params) . $this->user_config->get('app_id'));
  40. $endpoint = addon_url("epay/api/wechat");
  41. return $this->buildPayHtml($endpoint, $params);
  42. }
  43. /**
  44. * build pay html.
  45. *
  46. * @author yansongda <me@yansongda.cn>
  47. *
  48. * @return string
  49. */
  50. protected function buildPayHtml($endpoint, $params)
  51. {
  52. $sHtml = "<form id='alipaysubmit' name='wechatsubmit' action='" . $endpoint . "' method='POST'>";
  53. foreach ($params as $key => $val) {
  54. $val = str_replace("'", '&apos;', $val);
  55. $sHtml .= "<input type='hidden' name='" . $key . "' value='" . $val . "'/>";
  56. }
  57. $sHtml .= "<input type='submit' value='ok' style='display:none;'></form>";
  58. $sHtml .= "<script>document.forms['wechatsubmit'].submit();</script>";
  59. return $sHtml;
  60. }
  61. }