Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring payment #972

Merged
merged 3 commits into from
Oct 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/Kernel/Support/AES.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,20 @@ public static function encrypt(string $text, string $key, string $iv, int $optio
}

/**
* @param string $cipherText
* @param string $key
* @param string $iv
* @param int $option
* @param string $cipherText
* @param string $key
* @param string $iv
* @param int $option
* @param string|null $method
*
* @return string
*/
public static function decrypt(string $cipherText, string $key, string $iv, int $option = OPENSSL_RAW_DATA): string
public static function decrypt(string $cipherText, string $key, string $iv, int $option = OPENSSL_RAW_DATA, $method = null): string
{
self::validateKey($key);
self::validateIv($iv);

return openssl_decrypt($cipherText, self::getMode($key), $key, $option, $iv);
return openssl_decrypt($cipherText, $method ?: self::getMode($key), $key, $option, $iv);
}

/**
Expand Down Expand Up @@ -77,7 +78,7 @@ public static function validateKey(string $key)
*/
public static function validateIv(string $iv)
{
if (strlen($iv) !== 16) {
if (!empty($iv) && strlen($iv) !== 16) {
throw new \InvalidArgumentException('IV length must be 16 bytes.');
}
}
Expand Down
104 changes: 84 additions & 20 deletions src/Payment/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,25 @@

use EasyWeChat\BasicService;
use EasyWeChat\Kernel\ServiceContainer;
use EasyWeChat\Kernel\Support;
use EasyWeChat\OfficialAccount;

/**
* Class Application.
*
* @property \EasyWeChat\OfficialAccount\Auth\AccessToken $access_token
* @property \EasyWeChat\BasicService\Url\Client $url
* @property \EasyWeChat\Payment\Coupon\Client $coupon
* @property \EasyWeChat\Payment\Redpack\Client $redpack
* @property \EasyWeChat\Payment\Transfer\Client $transfer
* @property \EasyWeChat\Payment\Jssdk\Client $jssdk
* @property \EasyWeChat\Payment\Merchant $merchant
* @property \EasyWeChat\Payment\Client $payment
* @property \EasyWeChat\Payment\Bill\Client $bill
* @property \EasyWeChat\Payment\Jssdk\Client $jssdk
* @property \EasyWeChat\Payment\Order\Client $order
* @property \EasyWeChat\Payment\Refund\Client $refund
* @property \EasyWeChat\Payment\Coupon\Client $coupon
* @property \EasyWeChat\Payment\Reverse\Client $reverse
* @property \EasyWeChat\Payment\Redpack\Client $redpack
* @property \EasyWeChat\BasicService\Url\Client $url
* @property \EasyWeChat\Payment\Transfer\Client $transfer
* @property \EasyWeChat\OfficialAccount\Auth\AccessToken $access_token
*
* @method \EasyWeChat\Payment\Client sandboxMode(bool $enabled = false)
* @method string scheme(string $productId)
* @method mixed pay(\EasyWeChat\Payment\Order $order)
* @method mixed prepare(\EasyWeChat\Payment\Order $order)
* @method mixed query(string $orderNo)
* @method mixed queryByTransactionId(string $transactionId)
* @method mixed close(string $tradeNo)
* @method mixed reverse(string $orderNo)
* @method mixed reverseByTransactionId(string $transactionId)
* @method mixed handleNotify(callable $callback, \Symfony\Component\HttpFoundation\Request $request = null)
* @method mixed pay(array $attributes)
* @method mixed authCodeToOpenId(string $authCode)
*/
class Application extends ServiceContainer
{
Expand All @@ -46,7 +41,16 @@ class Application extends ServiceContainer
protected $providers = [
OfficialAccount\Auth\ServiceProvider::class,
BasicService\Url\ServiceProvider::class,
ServiceProvider::class,
Base\ServiceProvider::class,
Bill\ServiceProvider::class,
Coupon\ServiceProvider::class,
Jssdk\ServiceProvider::class,
Order\ServiceProvider::class,
Redpack\ServiceProvider::class,
Refund\ServiceProvider::class,
Reverse\ServiceProvider::class,
Sandbox\ServiceProvider::class,
Transfer\ServiceProvider::class,
];

/**
Expand All @@ -58,6 +62,66 @@ class Application extends ServiceContainer
],
];

/**
* Build payment scheme for product.
*
* @param string $productId
*
* @return string
*/
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);
}

/**
* @param callable $callback
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function handlePaidNotify(callable $callback)
{
return (new Notify\Paid($this))->handle($callback);
}

/**
* @param callable $callback
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function handleRefundedNotify(callable $callback)
{
return (new Notify\Refunded($this))->handle($callback);
}

/**
* @param callable $callback
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function handleScannedNotify(callable $callback)
{
return (new Notify\Scanned($this))->handle($callback);
}

/**
* @return bool
*/
public function inSandbox(): bool
{
return (bool) $this['config']->get('sandbox');
}

/**
* @param string $name
* @param array $arguments
Expand All @@ -66,6 +130,6 @@ class Application extends ServiceContainer
*/
public function __call($name, $arguments)
{
return call_user_func_array([$this['payment'], $name], $arguments);
return call_user_func_array([$this['base'], $name], $arguments);
}
}
41 changes: 41 additions & 0 deletions src/Payment/Base/Client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace EasyWeChat\Payment\Base;

use EasyWeChat\Payment\Kernel\BaseClient;

class Client extends BaseClient
{
/**
* Pay the order.
*
* @param array $attributes
*
* @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
*/
public function pay(array $attributes)
{
return $this->request('pay/micropay', $attributes);
}

/**
* Get openid by auth code.
*
* @param string $authCode
*
* @return \Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
*/
public function authCodeToOpenId(string $authCode)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

openid 现在已经是一个词了,与 web 类似

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK,稍后改

{
return $this->request('tools/authcodetoopenid', ['auth_code' => $authCode]);
}
}
33 changes: 33 additions & 0 deletions src/Payment/Base/ServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace EasyWeChat\Payment\Base;

use Pimple\Container;
use Pimple\ServiceProviderInterface;

/**
* Class ServiceProvider.
*
* @author overtrue <[email protected]>
*/
class ServiceProvider implements ServiceProviderInterface
{
/**
* {@inheritdoc}.
*/
public function register(Container $app)
{
$app['base'] = function ($app) {
return new Client($app);
};
}
}
42 changes: 42 additions & 0 deletions src/Payment/Bill/Client.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace EasyWeChat\Payment\Bill;

use EasyWeChat\Kernel\Http\StreamResponse;
use EasyWeChat\Payment\Kernel\BaseClient;

class Client extends BaseClient
{
/**
* Download bill history as a table file.
*
* @param string $date
* @param string $type
*
* @return \EasyWeChat\Kernel\Http\StreamResponse|\Psr\Http\Message\ResponseInterface|\EasyWeChat\Kernel\Support\Collection|array|object|string
*/
public function download(string $date, string $type = 'ALL')
{
$params = [
'bill_date' => $date,
'bill_type' => $type,
];

$response = $this->requestRaw('pay/downloadbill', $params);

if (strpos($response->getBody()->getContents(), '<xml>') === 0) {
return $this->resolveResponse($response, $this->app['config']->get('response_type', 'array'));
}

return StreamResponse::buildFromPsrResponse($response);
}
}
33 changes: 33 additions & 0 deletions src/Payment/Bill/ServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace EasyWeChat\Payment\Bill;

use Pimple\Container;
use Pimple\ServiceProviderInterface;

/**
* Class ServiceProvider.
*
* @author overtrue <[email protected]>
*/
class ServiceProvider implements ServiceProviderInterface
{
/**
* {@inheritdoc}.
*/
public function register(Container $app)
{
$app['bill'] = function ($app) {
return new Client($app);
};
}
}
Loading