TransferGateway.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 TransferGateway 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. if (Wechat::MODE_SERVICE === $this->mode) {
  26. unset($payload['sub_mch_id'], $payload['sub_appid']);
  27. }
  28. $type = Support::getTypeName($payload['type'] ?? '');
  29. $payload['mch_appid'] = Support::getInstance()->getConfig($type, '');
  30. $payload['mchid'] = $payload['mch_id'];
  31. if ('cli' !== php_sapi_name() && !isset($payload['spbill_create_ip'])) {
  32. $payload['spbill_create_ip'] = Request::createFromGlobals()->server->get('SERVER_ADDR');
  33. }
  34. unset($payload['appid'], $payload['mch_id'], $payload['trade_type'],
  35. $payload['notify_url'], $payload['type']);
  36. $payload['sign'] = Support::generateSign($payload);
  37. Events::dispatch(new Events\PayStarted('Wechat', 'Transfer', $endpoint, $payload));
  38. return Support::requestApi(
  39. 'mmpaymkttransfers/promotion/transfers',
  40. $payload,
  41. true
  42. );
  43. }
  44. /**
  45. * Find.
  46. *
  47. * @author yansongda <me@yansongda.cn>
  48. *
  49. * @param $order
  50. */
  51. public function find($order): array
  52. {
  53. return [
  54. 'endpoint' => 'mmpaymkttransfers/gettransferinfo',
  55. 'order' => is_array($order) ? $order : ['partner_trade_no' => $order],
  56. 'cert' => true,
  57. ];
  58. }
  59. /**
  60. * Get trade type config.
  61. *
  62. * @author yansongda <me@yansongda.cn>
  63. */
  64. protected function getTradeType(): string
  65. {
  66. return '';
  67. }
  68. }