diff --git a/src/Gateways/Alipay.php b/src/Gateways/Alipay.php index f38727437..0c55cc28c 100644 --- a/src/Gateways/Alipay.php +++ b/src/Gateways/Alipay.php @@ -38,6 +38,11 @@ class Alipay implements GatewayApplicationInterface */ const MODE_DEV = 'dev'; + /** + * Const mode_service. + */ + const MODE_SERVICE = 'service'; + /** * Const url. */ diff --git a/src/Gateways/Alipay/AppGateway.php b/src/Gateways/Alipay/AppGateway.php index 19b00a41f..7dd45b355 100644 --- a/src/Gateways/Alipay/AppGateway.php +++ b/src/Gateways/Alipay/AppGateway.php @@ -3,11 +3,12 @@ namespace Yansongda\Pay\Gateways\Alipay; use Symfony\Component\HttpFoundation\Response; -use Yansongda\Pay\Contracts\GatewayInterface; use Yansongda\Pay\Events; +use Yansongda\Pay\Exceptions\InvalidArgumentException; use Yansongda\Pay\Exceptions\InvalidConfigException; +use Yansongda\Pay\Gateways\Alipay; -class AppGateway implements GatewayInterface +class AppGateway extends Gateway { /** * Pay an order. @@ -18,16 +19,20 @@ class AppGateway implements GatewayInterface * @param array $payload * * @throws InvalidConfigException + * @throws InvalidArgumentException * * @return Response */ public function pay($endpoint, array $payload): Response { $payload['method'] = 'alipay.trade.app.pay'; - $payload['biz_content'] = json_encode(array_merge( - json_decode($payload['biz_content'], true), - ['product_code' => 'QUICK_MSECURITY_PAY'] - )); + + $biz_array = json_decode($payload['biz_content'], true); + if ((Alipay::MODE_SERVICE === $this->mode) && (!empty(Support::getInstance()->pid))) { + //������ģʽ�ҷ�����pid������Ϊ�� + $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]; + } + $payload['biz_content'] = json_encode(array_merge($biz_array, ['product_code' => 'QUICK_MSECURITY_PAY'])); $payload['sign'] = Support::generateSign($payload); Events::dispatch(new Events\PayStarted('Alipay', 'App', $endpoint, $payload)); diff --git a/src/Gateways/Alipay/Gateway.php b/src/Gateways/Alipay/Gateway.php new file mode 100644 index 000000000..f5ab91184 --- /dev/null +++ b/src/Gateways/Alipay/Gateway.php @@ -0,0 +1,41 @@ + + * + * @throws InvalidArgumentException + */ + public function __construct() + { + $this->mode = Support::getInstance()->mode; + } + + /** + * Pay an order. + * + * @author yansongda + * + * @param string $endpoint + * @param array $payload + * + * @return Collection + */ + abstract public function pay($endpoint, array $payload); +} diff --git a/src/Gateways/Alipay/MiniGateway.php b/src/Gateways/Alipay/MiniGateway.php index 1b4a51cc7..40336b007 100644 --- a/src/Gateways/Alipay/MiniGateway.php +++ b/src/Gateways/Alipay/MiniGateway.php @@ -2,15 +2,15 @@ namespace Yansongda\Pay\Gateways\Alipay; -use Yansongda\Pay\Contracts\GatewayInterface; use Yansongda\Pay\Events; use Yansongda\Pay\Exceptions\GatewayException; use Yansongda\Pay\Exceptions\InvalidArgumentException; use Yansongda\Pay\Exceptions\InvalidConfigException; use Yansongda\Pay\Exceptions\InvalidSignException; +use Yansongda\Pay\Gateways\Alipay; use Yansongda\Supports\Collection; -class MiniGateway implements GatewayInterface +class MiniGateway extends Gateway { /** * Pay an order. @@ -31,10 +31,14 @@ class MiniGateway implements GatewayInterface */ public function pay($endpoint, array $payload): Collection { - if (empty(json_decode($payload['biz_content'], true)['buyer_id'])) { + $biz_array = json_decode($payload['biz_content'], true); + if (empty($biz_array['buyer_id'])) { throw new InvalidArgumentException('buyer_id required'); } - + if ((Alipay::MODE_SERVICE === $this->mode) && (!empty(Support::getInstance()->pid))) { + $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]; + } + $payload['biz_content'] = json_encode($biz_array); $payload['method'] = 'alipay.trade.create'; $payload['sign'] = Support::generateSign($payload); diff --git a/src/Gateways/Alipay/PosGateway.php b/src/Gateways/Alipay/PosGateway.php index ac3118ae8..09ee3fc2e 100644 --- a/src/Gateways/Alipay/PosGateway.php +++ b/src/Gateways/Alipay/PosGateway.php @@ -2,14 +2,15 @@ namespace Yansongda\Pay\Gateways\Alipay; -use Yansongda\Pay\Contracts\GatewayInterface; use Yansongda\Pay\Events; use Yansongda\Pay\Exceptions\GatewayException; +use Yansongda\Pay\Exceptions\InvalidArgumentException; use Yansongda\Pay\Exceptions\InvalidConfigException; use Yansongda\Pay\Exceptions\InvalidSignException; +use Yansongda\Pay\Gateways\Alipay; use Yansongda\Supports\Collection; -class PosGateway implements GatewayInterface +class PosGateway extends Gateway { /** * Pay an order. @@ -19,6 +20,7 @@ class PosGateway implements GatewayInterface * @param string $endpoint * @param array $payload * + * @throws InvalidArgumentException * @throws GatewayException * @throws InvalidConfigException * @throws InvalidSignException @@ -28,8 +30,12 @@ class PosGateway implements GatewayInterface public function pay($endpoint, array $payload): Collection { $payload['method'] = 'alipay.trade.pay'; + $biz_array = json_decode($payload['biz_content'], true); + if ((Alipay::MODE_SERVICE === $this->mode) && (!empty(Support::getInstance()->pid))) { + $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]; + } $payload['biz_content'] = json_encode(array_merge( - json_decode($payload['biz_content'], true), + $biz_array, [ 'product_code' => 'FACE_TO_FACE_PAYMENT', 'scene' => 'bar_code', diff --git a/src/Gateways/Alipay/ScanGateway.php b/src/Gateways/Alipay/ScanGateway.php index 4aa07e692..85dd2e30a 100644 --- a/src/Gateways/Alipay/ScanGateway.php +++ b/src/Gateways/Alipay/ScanGateway.php @@ -2,14 +2,15 @@ namespace Yansongda\Pay\Gateways\Alipay; -use Yansongda\Pay\Contracts\GatewayInterface; use Yansongda\Pay\Events; use Yansongda\Pay\Exceptions\GatewayException; +use Yansongda\Pay\Exceptions\InvalidArgumentException; use Yansongda\Pay\Exceptions\InvalidConfigException; use Yansongda\Pay\Exceptions\InvalidSignException; +use Yansongda\Pay\Gateways\Alipay; use Yansongda\Supports\Collection; -class ScanGateway implements GatewayInterface +class ScanGateway extends Gateway { /** * Pay an order. @@ -20,6 +21,7 @@ class ScanGateway implements GatewayInterface * @param array $payload * * @throws GatewayException + * @throws InvalidArgumentException * @throws InvalidConfigException * @throws InvalidSignException * @@ -28,10 +30,11 @@ class ScanGateway implements GatewayInterface public function pay($endpoint, array $payload): Collection { $payload['method'] = 'alipay.trade.precreate'; - $payload['biz_content'] = json_encode(array_merge( - json_decode($payload['biz_content'], true), - ['product_code' => ''] - )); + $biz_array = json_decode($payload['biz_content'], true); + if ((Alipay::MODE_SERVICE === $this->mode) && (!empty(Support::getInstance()->pid))) { + $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]; + } + $payload['biz_content'] = json_encode(array_merge($biz_array, ['product_code' => ''])); $payload['sign'] = Support::generateSign($payload); Events::dispatch(new Events\PayStarted('Alipay', 'Scan', $endpoint, $payload)); diff --git a/src/Gateways/Alipay/Support.php b/src/Gateways/Alipay/Support.php index a3a67f65a..b911a2e38 100644 --- a/src/Gateways/Alipay/Support.php +++ b/src/Gateways/Alipay/Support.php @@ -4,6 +4,7 @@ use Yansongda\Pay\Events; use Yansongda\Pay\Exceptions\GatewayException; +use Yansongda\Pay\Exceptions\InvalidArgumentException; use Yansongda\Pay\Exceptions\InvalidConfigException; use Yansongda\Pay\Exceptions\InvalidSignException; use Yansongda\Pay\Gateways\Alipay; @@ -23,6 +24,7 @@ * @property array http http options * @property string mode current mode * @property array log log options + * @property string pid ali pid */ class Support { @@ -96,6 +98,24 @@ public static function create(Config $config) return self::$instance; } + /** + * getInstance. + * + * @author yansongda + * + * @throws InvalidArgumentException + * + * @return Support + */ + public static function getInstance() + { + if (is_null(self::$instance)) { + throw new InvalidArgumentException('You Should [Create] First Before Using'); + } + + return self::$instance; + } + /** * clear. * diff --git a/src/Gateways/Alipay/WebGateway.php b/src/Gateways/Alipay/WebGateway.php index 45c4db970..fc12728a4 100644 --- a/src/Gateways/Alipay/WebGateway.php +++ b/src/Gateways/Alipay/WebGateway.php @@ -4,11 +4,12 @@ use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\Response; -use Yansongda\Pay\Contracts\GatewayInterface; use Yansongda\Pay\Events; +use Yansongda\Pay\Exceptions\InvalidArgumentException; use Yansongda\Pay\Exceptions\InvalidConfigException; +use Yansongda\Pay\Gateways\Alipay; -class WebGateway implements GatewayInterface +class WebGateway extends Gateway { /** * Pay an order. @@ -19,6 +20,7 @@ class WebGateway implements GatewayInterface * @param array $payload * * @throws InvalidConfigException + * @throws InvalidArgumentException * * @return Response */ @@ -30,7 +32,9 @@ public function pay($endpoint, array $payload): Response $method = $biz_array['http_method'] ?? 'POST'; unset($biz_array['http_method']); - + if ((Alipay::MODE_SERVICE === $this->mode) && (!empty(Support::getInstance()->pid))) { + $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]; + } $payload['method'] = $this->getMethod(); $payload['biz_content'] = json_encode($biz_array); $payload['sign'] = Support::generateSign($payload);