123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366 |
- <?php
- namespace Yansongda\Pay\Gateways;
- use Exception;
- use Symfony\Component\HttpFoundation\RedirectResponse;
- use Symfony\Component\HttpFoundation\Request;
- use Symfony\Component\HttpFoundation\Response;
- use Yansongda\Pay\Contracts\GatewayApplicationInterface;
- use Yansongda\Pay\Contracts\GatewayInterface;
- use Yansongda\Pay\Events;
- use Yansongda\Pay\Exceptions\GatewayException;
- use Yansongda\Pay\Exceptions\InvalidArgumentException;
- use Yansongda\Pay\Exceptions\InvalidGatewayException;
- use Yansongda\Pay\Exceptions\InvalidSignException;
- use Yansongda\Pay\Gateways\Wechat\Support;
- use Yansongda\Pay\Log;
- use Yansongda\Supports\Collection;
- use Yansongda\Supports\Config;
- use Yansongda\Supports\Str;
- class Wechat implements GatewayApplicationInterface
- {
-
- const MODE_NORMAL = 'normal';
-
- const MODE_DEV = 'dev';
-
- const MODE_HK = 'hk';
-
- const MODE_US = 'us';
-
- const MODE_SERVICE = 'service';
-
- const URL = [
- self::MODE_NORMAL => 'https://api.mch.weixin.qq.com/',
- self::MODE_DEV => 'https://api.mch.weixin.qq.com/xdc/apiv2sandbox/',
- self::MODE_HK => 'https://apihk.mch.weixin.qq.com/',
- self::MODE_SERVICE => 'https://api.mch.weixin.qq.com/',
- self::MODE_US => 'https://apius.mch.weixin.qq.com/',
- ];
-
- protected $payload;
-
- protected $gateway;
-
- public function __construct(Config $config)
- {
- $this->gateway = Support::create($config)->getBaseUri();
- $this->payload = [
- 'appid' => $config->get('app_id', ''),
- 'mch_id' => $config->get('mch_id', ''),
- 'nonce_str' => Str::random(),
- 'notify_url' => $config->get('notify_url', ''),
- 'sign' => '',
- 'trade_type' => '',
- 'spbill_create_ip' => Request::createFromGlobals()->getClientIp(),
- ];
- if ($config->get('mode', self::MODE_NORMAL) === static::MODE_SERVICE) {
- $this->payload = array_merge($this->payload, [
- 'sub_mch_id' => $config->get('sub_mch_id'),
- 'sub_appid' => $config->get('sub_app_id', ''),
- ]);
- }
- }
-
- public function __call($method, $params)
- {
- return self::pay($method, ...$params);
- }
-
- public function pay($gateway, $params = [])
- {
- Events::dispatch(new Events\PayStarting('Wechat', $gateway, $params));
- $this->payload = array_merge($this->payload, $params);
- $gateway = get_class($this).'\\'.Str::studly($gateway).'Gateway';
- if (class_exists($gateway)) {
- return $this->makePay($gateway);
- }
- throw new InvalidGatewayException("Pay Gateway [{$gateway}] Not Exists");
- }
-
- public function verify($content = null, bool $refund = false): Collection
- {
- $content = $content ?? Request::createFromGlobals()->getContent();
- Events::dispatch(new Events\RequestReceived('Wechat', '', [$content]));
- $data = Support::fromXml($content);
- if ($refund) {
- $decrypt_data = Support::decryptRefundContents($data['req_info']);
- $data = array_merge(Support::fromXml($decrypt_data), $data);
- }
- Log::debug('Resolved The Received Wechat Request Data', $data);
- if ($refund || Support::generateSign($data) === $data['sign']) {
- return new Collection($data);
- }
- Events::dispatch(new Events\SignFailed('Wechat', '', $data));
- throw new InvalidSignException('Wechat Sign Verify FAILED', $data);
- }
-
- public function find($order, string $type = 'wap'): Collection
- {
- if ('wap' != $type) {
- unset($this->payload['spbill_create_ip']);
- }
- $gateway = get_class($this).'\\'.Str::studly($type).'Gateway';
- if (!class_exists($gateway) || !is_callable([new $gateway(), 'find'])) {
- throw new GatewayException("{$gateway} Done Not Exist Or Done Not Has FIND Method");
- }
- $config = call_user_func([new $gateway(), 'find'], $order);
- $this->payload = Support::filterPayload($this->payload, $config['order']);
- Events::dispatch(new Events\MethodCalled('Wechat', 'Find', $this->gateway, $this->payload));
- return Support::requestApi(
- $config['endpoint'],
- $this->payload,
- $config['cert']
- );
- }
-
- public function refund(array $order): Collection
- {
- $this->payload = Support::filterPayload($this->payload, $order, true);
- Events::dispatch(new Events\MethodCalled('Wechat', 'Refund', $this->gateway, $this->payload));
- return Support::requestApi(
- 'secapi/pay/refund',
- $this->payload,
- true
- );
- }
-
- public function cancel($order): Collection
- {
- unset($this->payload['spbill_create_ip']);
- $this->payload = Support::filterPayload($this->payload, $order);
- Events::dispatch(new Events\MethodCalled('Wechat', 'Cancel', $this->gateway, $this->payload));
- return Support::requestApi(
- 'secapi/pay/reverse',
- $this->payload,
- true
- );
- }
-
- public function close($order): Collection
- {
- unset($this->payload['spbill_create_ip']);
- $this->payload = Support::filterPayload($this->payload, $order);
- Events::dispatch(new Events\MethodCalled('Wechat', 'Close', $this->gateway, $this->payload));
- return Support::requestApi('pay/closeorder', $this->payload);
- }
-
- public function success(): Response
- {
- Events::dispatch(new Events\MethodCalled('Wechat', 'Success', $this->gateway));
- return new Response(
- Support::toXml(['return_code' => 'SUCCESS', 'return_msg' => 'OK']),
- 200,
- ['Content-Type' => 'application/xml']
- );
- }
-
- public function download(array $params): string
- {
- unset($this->payload['spbill_create_ip']);
- $this->payload = Support::filterPayload($this->payload, $params, true);
- Events::dispatch(new Events\MethodCalled('Wechat', 'Download', $this->gateway, $this->payload));
- $result = Support::getInstance()->post(
- 'pay/downloadbill',
- Support::getInstance()->toXml($this->payload)
- );
- if (is_array($result)) {
- throw new GatewayException('Get Wechat API Error: '.$result['return_msg'], $result);
- }
- return $result;
- }
-
- protected function makePay($gateway)
- {
- $app = new $gateway();
- if ($app instanceof GatewayInterface) {
- return $app->pay($this->gateway, array_filter($this->payload, function ($value) {
- return '' !== $value && !is_null($value);
- }));
- }
- throw new InvalidGatewayException("Pay Gateway [{$gateway}] Must Be An Instance Of GatewayInterface");
- }
- }
|