MiniGateway.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. <?php
  2. namespace Yansongda\Pay\Gateways\Alipay;
  3. use Yansongda\Pay\Events;
  4. use Yansongda\Pay\Exceptions\GatewayException;
  5. use Yansongda\Pay\Exceptions\InvalidArgumentException;
  6. use Yansongda\Pay\Exceptions\InvalidConfigException;
  7. use Yansongda\Pay\Exceptions\InvalidSignException;
  8. use Yansongda\Pay\Gateways\Alipay;
  9. use Yansongda\Supports\Collection;
  10. class MiniGateway extends Gateway
  11. {
  12. /**
  13. * Pay an order.
  14. *
  15. * @author xiaozan <i@xiaozan.me>
  16. *
  17. * @param string $endpoint
  18. *
  19. * @throws GatewayException
  20. * @throws InvalidArgumentException
  21. * @throws InvalidConfigException
  22. * @throws InvalidSignException
  23. *
  24. * @see https://docs.alipay.com/mini/introduce/pay
  25. */
  26. public function pay($endpoint, array $payload): Collection
  27. {
  28. $biz_array = json_decode($payload['biz_content'], true);
  29. if (empty($biz_array['buyer_id'])) {
  30. throw new InvalidArgumentException('buyer_id required');
  31. }
  32. if ((Alipay::MODE_SERVICE === $this->mode) && (!empty(Support::getInstance()->pid))) {
  33. $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];
  34. }
  35. $payload['biz_content'] = json_encode($biz_array);
  36. $payload['method'] = 'alipay.trade.create';
  37. $payload['sign'] = Support::generateSign($payload);
  38. Events::dispatch(new Events\PayStarted('Alipay', 'Mini', $endpoint, $payload));
  39. return Support::requestApi($payload);
  40. }
  41. }