MiniGateway.php
1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
namespace Yansongda\Pay\Gateways\Alipay;
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 extends Gateway
{
/**
* Pay an order.
*
* @author xiaozan <i@xiaozan.me>
*
* @param string $endpoint
*
* @throws GatewayException
* @throws InvalidArgumentException
* @throws InvalidConfigException
* @throws InvalidSignException
*
* @see https://docs.alipay.com/mini/introduce/pay
*/
public function pay($endpoint, array $payload): Collection
{
$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);
Events::dispatch(new Events\PayStarted('Alipay', 'Mini', $endpoint, $payload));
return Support::requestApi($payload);
}
}