ScanGateway.php 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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 ScanGateway 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 InvalidConfigException
  22. * @throws InvalidSignException
  23. */
  24. public function pay($endpoint, array $payload): Collection
  25. {
  26. $payload['method'] = 'alipay.trade.precreate';
  27. $biz_array = json_decode($payload['biz_content'], true);
  28. if ((Alipay::MODE_SERVICE === $this->mode) && (!empty(Support::getInstance()->pid))) {
  29. $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];
  30. }
  31. $payload['biz_content'] = json_encode(array_merge($biz_array, ['product_code' => '']));
  32. $payload['sign'] = Support::generateSign($payload);
  33. Events::dispatch(new Events\PayStarted('Alipay', 'Scan', $endpoint, $payload));
  34. return Support::requestApi($payload);
  35. }
  36. }