Skip to content

Commit

Permalink
Merge pull request yansongda#289 from iwzh/master
Browse files Browse the repository at this point in the history
支付宝支付服务商模式
  • Loading branch information
yansongda authored Oct 13, 2019
2 parents 644a661 + 3036f2c commit 68d189b
Show file tree
Hide file tree
Showing 8 changed files with 110 additions and 22 deletions.
5 changes: 5 additions & 0 deletions src/Gateways/Alipay.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ class Alipay implements GatewayApplicationInterface
*/
const MODE_DEV = 'dev';

/**
* Const mode_service.
*/
const MODE_SERVICE = 'service';

/**
* Const url.
*/
Expand Down
17 changes: 11 additions & 6 deletions src/Gateways/Alipay/AppGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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));
Expand Down
41 changes: 41 additions & 0 deletions src/Gateways/Alipay/Gateway.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Yansongda\Pay\Gateways\Alipay;

use Yansongda\Pay\Contracts\GatewayInterface;
use Yansongda\Pay\Exceptions\InvalidArgumentException;
use Yansongda\Supports\Collection;

abstract class Gateway implements GatewayInterface
{
/**
* Mode.
*
* @var string
*/
protected $mode;

/**
* Bootstrap.
*
* @author yansongda <[email protected]>
*
* @throws InvalidArgumentException
*/
public function __construct()
{
$this->mode = Support::getInstance()->mode;
}

/**
* Pay an order.
*
* @author yansongda <[email protected]>
*
* @param string $endpoint
* @param array $payload
*
* @return Collection
*/
abstract public function pay($endpoint, array $payload);
}
12 changes: 8 additions & 4 deletions src/Gateways/Alipay/MiniGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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);

Expand Down
12 changes: 9 additions & 3 deletions src/Gateways/Alipay/PosGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -19,6 +20,7 @@ class PosGateway implements GatewayInterface
* @param string $endpoint
* @param array $payload
*
* @throws InvalidArgumentException
* @throws GatewayException
* @throws InvalidConfigException
* @throws InvalidSignException
Expand All @@ -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',
Expand Down
15 changes: 9 additions & 6 deletions src/Gateways/Alipay/ScanGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -20,6 +21,7 @@ class ScanGateway implements GatewayInterface
* @param array $payload
*
* @throws GatewayException
* @throws InvalidArgumentException
* @throws InvalidConfigException
* @throws InvalidSignException
*
Expand All @@ -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));
Expand Down
20 changes: 20 additions & 0 deletions src/Gateways/Alipay/Support.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
{
Expand Down Expand Up @@ -96,6 +98,24 @@ public static function create(Config $config)
return self::$instance;
}

/**
* getInstance.
*
* @author yansongda <[email protected]>
*
* @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.
*
Expand Down
10 changes: 7 additions & 3 deletions src/Gateways/Alipay/WebGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -19,6 +20,7 @@ class WebGateway implements GatewayInterface
* @param array $payload
*
* @throws InvalidConfigException
* @throws InvalidArgumentException
*
* @return Response
*/
Expand All @@ -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);
Expand Down

0 comments on commit 68d189b

Please sign in to comment.