<?php
/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace EasyWeChat\Payment;
use Closure;
use EasyWeChat\BasicService;
use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
use EasyWeChat\Kernel\ServiceContainer;
use EasyWeChat\Kernel\Support;
use EasyWeChat\OfficialAccount;
class Application extends ServiceContainer
{
protected $providers = [
OfficialAccount\Auth\ServiceProvider::class,
BasicService\Url\ServiceProvider::class,
Base\ServiceProvider::class,
Bill\ServiceProvider::class,
Coupon\ServiceProvider::class,
Jssdk\ServiceProvider::class,
Merchant\ServiceProvider::class,
Order\ServiceProvider::class,
Redpack\ServiceProvider::class,
Refund\ServiceProvider::class,
Reverse\ServiceProvider::class,
Sandbox\ServiceProvider::class,
Transfer\ServiceProvider::class,
Security\ServiceProvider::class,
];
protected $defaultConfig = [
'http' => [
'base_uri' => 'https://api.mch.weixin.qq.com/',
],
];
public function scheme(string $productId): string
{
$params = [
'appid' => $this['config']->app_id,
'mch_id' => $this['config']->mch_id,
'time_stamp' => time(),
'nonce_str' => uniqid(),
'product_id' => $productId,
];
$params['sign'] = Support\generate_sign($params, $this['config']->key);
return 'weixin://wxpay/bizpayurl?'.http_build_query($params);
}
public function handlePaidNotify(Closure $closure)
{
return (new Notify\Paid($this))->handle($closure);
}
public function handleRefundedNotify(Closure $closure)
{
return (new Notify\Refunded($this))->handle($closure);
}
public function handleScannedNotify(Closure $closure)
{
return (new Notify\Scanned($this))->handle($closure);
}
public function setSubMerchant(string $mchId, string $appId = null)
{
$this['config']->set('sub_mch_id', $mchId);
$this['config']->set('sub_appid', $appId);
return $this;
}
public function inSandbox(): bool
{
return (bool) $this['config']->get('sandbox');
}
public function getKey(string $endpoint = null)
{
if ('sandboxnew/pay/getsignkey' === $endpoint) {
return $this['config']->key;
}
$key = $this->inSandbox() ? $this['sandbox']->getKey() : $this['config']->key;
if (32 !== strlen($key)) {
throw new InvalidArgumentException(sprintf("'%s' should be 32 chars length.", $key));
}
return $key;
}
public function __call($name, $arguments)
{
return call_user_func_array([$this['base'], $name], $arguments);
}
}