RedpackGateway.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <?php
  2. namespace Yansongda\Pay\Gateways\Wechat;
  3. use Symfony\Component\HttpFoundation\Request;
  4. use Yansongda\Pay\Events;
  5. use Yansongda\Pay\Exceptions\GatewayException;
  6. use Yansongda\Pay\Exceptions\InvalidArgumentException;
  7. use Yansongda\Pay\Exceptions\InvalidSignException;
  8. use Yansongda\Pay\Gateways\Wechat;
  9. use Yansongda\Supports\Collection;
  10. class RedpackGateway extends Gateway
  11. {
  12. /**
  13. * Pay an order.
  14. *
  15. * @author yansongda <me@yansongda.cn>
  16. *
  17. * @param string $endpoint
  18. *
  19. * @throws GatewayException
  20. * @throws InvalidArgumentException
  21. * @throws InvalidSignException
  22. */
  23. public function pay($endpoint, array $payload): Collection
  24. {
  25. $payload['wxappid'] = $payload['appid'];
  26. if ('cli' !== php_sapi_name()) {
  27. $payload['client_ip'] = Request::createFromGlobals()->server->get('SERVER_ADDR');
  28. }
  29. if (Wechat::MODE_SERVICE === $this->mode) {
  30. $payload['msgappid'] = $payload['appid'];
  31. }
  32. unset($payload['appid'], $payload['trade_type'],
  33. $payload['notify_url'], $payload['spbill_create_ip']);
  34. $payload['sign'] = Support::generateSign($payload);
  35. Events::dispatch(new Events\PayStarted('Wechat', 'Redpack', $endpoint, $payload));
  36. return Support::requestApi(
  37. 'mmpaymkttransfers/sendredpack',
  38. $payload,
  39. true
  40. );
  41. }
  42. /**
  43. * Get trade type config.
  44. *
  45. * @author yansongda <me@yansongda.cn>
  46. */
  47. protected function getTradeType(): string
  48. {
  49. return '';
  50. }
  51. }