WebGateway.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace Yansongda\Pay\Gateways\Alipay;
  3. use Symfony\Component\HttpFoundation\RedirectResponse;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Yansongda\Pay\Events;
  6. use Yansongda\Pay\Exceptions\InvalidArgumentException;
  7. use Yansongda\Pay\Exceptions\InvalidConfigException;
  8. use Yansongda\Pay\Gateways\Alipay;
  9. class WebGateway extends Gateway
  10. {
  11. /**
  12. * Pay an order.
  13. *
  14. * @author yansongda <me@yansongda.cn>
  15. *
  16. * @param string $endpoint
  17. *
  18. * @throws InvalidConfigException
  19. * @throws InvalidArgumentException
  20. */
  21. public function pay($endpoint, array $payload): Response
  22. {
  23. $biz_array = json_decode($payload['biz_content'], true);
  24. $biz_array['product_code'] = $this->getProductCode();
  25. $method = $biz_array['http_method'] ?? 'POST';
  26. unset($biz_array['http_method']);
  27. if ((Alipay::MODE_SERVICE === $this->mode) && (!empty(Support::getInstance()->pid))) {
  28. $biz_array['extend_params'] = is_array($biz_array['extend_params']) ? array_merge(['sys_service_provider_id' => Support::getInstance()->pid], $biz_array['extend_params']) : ['sys_service_provider_id' => Support::getInstance()->pid];
  29. }
  30. $payload['method'] = $this->getMethod();
  31. $payload['biz_content'] = json_encode($biz_array);
  32. $payload['sign'] = Support::generateSign($payload);
  33. Events::dispatch(new Events\PayStarted('Alipay', 'Web/Wap', $endpoint, $payload));
  34. return $this->buildPayHtml($endpoint, $payload, $method);
  35. }
  36. /**
  37. * Find.
  38. *
  39. * @author yansongda <me@yansongda.cn>
  40. *
  41. * @param $order
  42. */
  43. public function find($order): array
  44. {
  45. return [
  46. 'method' => 'alipay.trade.query',
  47. 'biz_content' => json_encode(is_array($order) ? $order : ['out_trade_no' => $order]),
  48. ];
  49. }
  50. /**
  51. * Build Html response.
  52. *
  53. * @author yansongda <me@yansongda.cn>
  54. *
  55. * @param string $endpoint
  56. * @param array $payload
  57. * @param string $method
  58. */
  59. protected function buildPayHtml($endpoint, $payload, $method = 'POST'): Response
  60. {
  61. if ('GET' === strtoupper($method)) {
  62. return new RedirectResponse($endpoint.'&'.http_build_query($payload));
  63. }
  64. $sHtml = "<form id='alipay_submit' name='alipay_submit' action='".$endpoint."' method='".$method."'>";
  65. foreach ($payload as $key => $val) {
  66. $val = str_replace("'", '&apos;', $val);
  67. $sHtml .= "<input type='hidden' name='".$key."' value='".$val."'/>";
  68. }
  69. $sHtml .= "<input type='submit' value='ok' style='display:none;'></form>";
  70. $sHtml .= "<script>document.forms['alipay_submit'].submit();</script>";
  71. return new Response($sHtml);
  72. }
  73. /**
  74. * Get method config.
  75. *
  76. * @author yansongda <me@yansongda.cn>
  77. */
  78. protected function getMethod(): string
  79. {
  80. return 'alipay.trade.page.pay';
  81. }
  82. /**
  83. * Get productCode config.
  84. *
  85. * @author yansongda <me@yansongda.cn>
  86. */
  87. protected function getProductCode(): string
  88. {
  89. return 'FAST_INSTANT_TRADE_PAY';
  90. }
  91. }