| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423 | <?phpnamespace Yansongda\Pay\Gateways;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\InvalidConfigException;use Yansongda\Pay\Exceptions\InvalidGatewayException;use Yansongda\Pay\Exceptions\InvalidSignException;use Yansongda\Pay\Gateways\Alipay\Support;use Yansongda\Supports\Collection;use Yansongda\Supports\Config;use Yansongda\Supports\Str;/** * @method Response   app(array $config)      APP 支付 * @method Collection pos(array $config)      刷卡支付 * @method Collection scan(array $config)     扫码支付 * @method Collection transfer(array $config) 帐户转账 * @method Response   wap(array $config)      手机网站支付 * @method Response   web(array $config)      电脑支付 * @method Collection mini(array $config)     小程序支付 */class Alipay implements GatewayApplicationInterface{    /**     * Const mode_normal.     */    const MODE_NORMAL = 'normal';    /**     * Const mode_dev.     */    const MODE_DEV = 'dev';    /**     * Const mode_service.     */    const MODE_SERVICE = 'service';    /**     * Const url.     */    const URL = [        self::MODE_NORMAL => 'https://openapi.alipay.com/gateway.do?charset=utf-8',        self::MODE_SERVICE => 'https://openapi.alipay.com/gateway.do?charset=utf-8',        self::MODE_DEV => 'https://openapi-sandbox.dl.alipaydev.com/gateway.do?charset=utf-8',    ];    /**     * Alipay payload.     *     * @var array     */    protected $payload;    /**     * Alipay gateway.     *     * @var string     */    protected $gateway;    /**     * extends.     *     * @var array     */    protected $extends;    /**     * Bootstrap.     *     * @author yansongda <me@yansongda.cn>     *     * @throws \Exception     */    public function __construct(Config $config)    {        $this->gateway = Support::create($config)->getBaseUri();        $this->payload = [            'app_id' => $config->get('app_id'),            'method' => '',            'format' => 'JSON',            'charset' => 'utf-8',            'sign_type' => 'RSA2',            'version' => '1.0',            'return_url' => $config->get('return_url'),            'notify_url' => $config->get('notify_url'),            'timestamp' => date('Y-m-d H:i:s'),            'sign' => '',            'biz_content' => '',            'app_auth_token' => $config->get('app_auth_token'),        ];        if ($config->get('app_cert_public_key') && $config->get('alipay_root_cert')) {            $this->payload['app_cert_sn'] = Support::getCertSN($config->get('app_cert_public_key'));            $this->payload['alipay_root_cert_sn'] = Support::getRootCertSN($config->get('alipay_root_cert'));        }    }    /**     * Magic pay.     *     * @author yansongda <me@yansongda.cn>     *     * @param string $method     * @param array  $params     *     * @throws GatewayException     * @throws InvalidArgumentException     * @throws InvalidConfigException     * @throws InvalidGatewayException     * @throws InvalidSignException     *     * @return Response|Collection     */    public function __call($method, $params)    {        if (isset($this->extends[$method])) {            return $this->makeExtend($method, ...$params);        }        return $this->pay($method, ...$params);    }    /**     * Pay an order.     *     * @author yansongda <me@yansongda.cn>     *     * @param string $gateway     * @param array  $params     *     * @throws InvalidGatewayException     *     * @return Response|Collection     */    public function pay($gateway, $params = [])    {        Events::dispatch(new Events\PayStarting('Alipay', $gateway, $params));        $this->payload['return_url'] = $params['return_url'] ?? $this->payload['return_url'];        $this->payload['notify_url'] = $params['notify_url'] ?? $this->payload['notify_url'];        unset($params['return_url'], $params['notify_url']);        $this->payload['biz_content'] = json_encode($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");    }    /**     * Verify sign.     *     * @author yansongda <me@yansongda.cn>     *     * @param array|null $data     *     * @throws InvalidSignException     * @throws InvalidConfigException     */    public function verify($data = null, bool $refund = false): Collection    {        if (is_null($data)) {            $request = Request::createFromGlobals();            $data = $request->request->count() > 0 ? $request->request->all() : $request->query->all();        }        if (isset($data['fund_bill_list'])) {            $data['fund_bill_list'] = htmlspecialchars_decode($data['fund_bill_list']);        }        Events::dispatch(new Events\RequestReceived('Alipay', '', $data));        if (Support::verifySign($data)) {            return new Collection($data);        }        Events::dispatch(new Events\SignFailed('Alipay', '', $data));        throw new InvalidSignException('Alipay Sign Verify FAILED', $data);    }    /**     * Query an order.     *     * @author yansongda <me@yansongda.cn>     *     * @param string|array $order     *     * @throws GatewayException     * @throws InvalidConfigException     * @throws InvalidSignException     */    public function find($order, string $type = 'wap'): Collection    {        $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['method'] = $config['method'];        $this->payload['biz_content'] = $config['biz_content'];        $this->payload['sign'] = Support::generateSign($this->payload);        Events::dispatch(new Events\MethodCalled('Alipay', 'Find', $this->gateway, $this->payload));        return Support::requestApi($this->payload);    }    /**     * Refund an order.     *     * @author yansongda <me@yansongda.cn>     *     * @throws GatewayException     * @throws InvalidConfigException     * @throws InvalidSignException     */    public function refund(array $order): Collection    {        $this->payload['method'] = 'alipay.trade.refund';        $this->payload['biz_content'] = json_encode($order);        $this->payload['sign'] = Support::generateSign($this->payload);        Events::dispatch(new Events\MethodCalled('Alipay', 'Refund', $this->gateway, $this->payload));        return Support::requestApi($this->payload);    }    /**     * Cancel an order.     *     * @author yansongda <me@yansongda.cn>     *     * @param array|string $order     *     * @throws GatewayException     * @throws InvalidConfigException     * @throws InvalidSignException     */    public function cancel($order): Collection    {        $this->payload['method'] = 'alipay.trade.cancel';        $this->payload['biz_content'] = json_encode(is_array($order) ? $order : ['out_trade_no' => $order]);        $this->payload['sign'] = Support::generateSign($this->payload);        Events::dispatch(new Events\MethodCalled('Alipay', 'Cancel', $this->gateway, $this->payload));        return Support::requestApi($this->payload);    }    /**     * Close an order.     *     * @param string|array $order     *     * @author yansongda <me@yansongda.cn>     *     * @throws GatewayException     * @throws InvalidConfigException     * @throws InvalidSignException     */    public function close($order): Collection    {        $this->payload['method'] = 'alipay.trade.close';        $this->payload['biz_content'] = json_encode(is_array($order) ? $order : ['out_trade_no' => $order]);        $this->payload['sign'] = Support::generateSign($this->payload);        Events::dispatch(new Events\MethodCalled('Alipay', 'Close', $this->gateway, $this->payload));        return Support::requestApi($this->payload);    }    /**     * Download bill.     *     * @author yansongda <me@yansongda.cn>     *     * @param string|array $bill     *     * @throws GatewayException     * @throws InvalidConfigException     * @throws InvalidSignException     */    public function download($bill): string    {        $this->payload['method'] = 'alipay.data.dataservice.bill.downloadurl.query';        $this->payload['biz_content'] = json_encode(is_array($bill) ? $bill : ['bill_type' => 'trade', 'bill_date' => $bill]);        $this->payload['sign'] = Support::generateSign($this->payload);        Events::dispatch(new Events\MethodCalled('Alipay', 'Download', $this->gateway, $this->payload));        $result = Support::requestApi($this->payload);        return ($result instanceof Collection) ? $result->get('bill_download_url') : '';    }    /**     * Reply success to alipay.     *     * @author yansongda <me@yansongda.cn>     */    public function success(): Response    {        Events::dispatch(new Events\MethodCalled('Alipay', 'Success', $this->gateway));        return new Response('success');    }    /**     * extend.     *     * @author yansongda <me@yansongda.cn>     *     * @throws GatewayException     * @throws InvalidConfigException     * @throws InvalidSignException     * @throws InvalidArgumentException     */    public function extend(string $method, callable $function, bool $now = true): ?Collection    {        if (!$now && !method_exists($this, $method)) {            $this->extends[$method] = $function;            return null;        }        $customize = $function($this->payload);        if (!is_array($customize) && !($customize instanceof Collection)) {            throw new InvalidArgumentException('Return Type Must Be Array Or Collection');        }        Events::dispatch(new Events\MethodCalled('Alipay', 'extend', $this->gateway, $customize));        if (is_array($customize)) {            $this->payload = $customize;            $this->payload['sign'] = Support::generateSign($this->payload);            return Support::requestApi($this->payload);        }        return $customize;    }    /**     * Make pay gateway.     *     * @author yansongda <me@yansongda.cn>     *     * @throws InvalidGatewayException     *     * @return Response|Collection     */    protected function makePay(string $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");    }    /**     * makeExtend.     *     * @author yansongda <me@yansongda.cn>     *     * @throws GatewayException     * @throws InvalidArgumentException     * @throws InvalidConfigException     * @throws InvalidSignException     */    protected function makeExtend(string $method, array ...$params): Collection    {        $params = count($params) >= 1 ? $params[0] : $params;        $function = $this->extends[$method];        $customize = $function($this->payload, $params);        if (!is_array($customize) && !($customize instanceof Collection)) {            throw new InvalidArgumentException('Return Type Must Be Array Or Collection');        }        Events::dispatch(new Events\MethodCalled(            'Alipay',            'extend - '.$method,            $this->gateway,            is_array($customize) ? $customize : $customize->toArray()        ));        if (is_array($customize)) {            $this->payload = $customize;            $this->payload['sign'] = Support::generateSign($this->payload);            return Support::requestApi($this->payload);        }        return $customize;    }}
 |