PosGateway.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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 PosGateway extends Gateway
  11. {
  12. /**
  13. * Pay an order.
  14. *
  15. * @author yansongda <me@yansongda.cn>
  16. *
  17. * @param string $endpoint
  18. *
  19. * @throws InvalidArgumentException
  20. * @throws GatewayException
  21. * @throws InvalidConfigException
  22. * @throws InvalidSignException
  23. */
  24. public function pay($endpoint, array $payload): Collection
  25. {
  26. $payload['method'] = 'alipay.trade.pay';
  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(
  32. $biz_array,
  33. [
  34. 'product_code' => 'FACE_TO_FACE_PAYMENT',
  35. 'scene' => 'bar_code',
  36. ]
  37. ));
  38. $payload['sign'] = Support::generateSign($payload);
  39. Events::dispatch(new Events\PayStarted('Alipay', 'Pos', $endpoint, $payload));
  40. return Support::requestApi($payload);
  41. }
  42. }