Skip to content

Commit

Permalink
Use closure.
Browse files Browse the repository at this point in the history
  • Loading branch information
mingyoung committed Oct 23, 2017
1 parent 5f21692 commit ac8ceff
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 30 deletions.
19 changes: 10 additions & 9 deletions src/Payment/Application.php
Expand Up @@ -11,6 +11,7 @@

namespace EasyWeChat\Payment;

use Closure;
use EasyWeChat\BasicService;
use EasyWeChat\Kernel\ServiceContainer;
use EasyWeChat\Kernel\Support;
Expand Down Expand Up @@ -85,33 +86,33 @@ public function scheme(string $productId): string
}

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

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

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

/**
Expand Down
5 changes: 3 additions & 2 deletions src/Payment/Notify/Handler.php
Expand Up @@ -11,6 +11,7 @@

namespace EasyWeChat\Payment\Notify;

use Closure;
use EasyWeChat\Kernel\Exceptions\Exception;
use EasyWeChat\Kernel\Support;
use EasyWeChat\Kernel\Support\XML;
Expand Down Expand Up @@ -68,11 +69,11 @@ public function __construct($app)
/**
* Handle incoming notify.
*
* @param callable $callback
* @param \Closure $closure
*
* @return \Symfony\Component\HttpFoundation\Response
*/
abstract public function handle(callable $callback): Response;
abstract public function handle(Closure $closure);

/**
* @param string $message
Expand Down
8 changes: 4 additions & 4 deletions src/Payment/Notify/Paid.php
Expand Up @@ -11,19 +11,19 @@

namespace EasyWeChat\Payment\Notify;

use Symfony\Component\HttpFoundation\Response;
use Closure;

class Paid extends Handler
{
/**
* @param callable $callback
* @param \Closure $closure
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function handle(callable $callback): Response
public function handle(Closure $closure)
{
$this->strict(
call_user_func_array($callback, [$this->getMessage(), [$this, 'fail']])
$closure->bindTo($this)->__invoke($this->getMessage(), [$this, 'fail'])
);

return $this->toResponse();
Expand Down
15 changes: 10 additions & 5 deletions src/Payment/Notify/Refunded.php
Expand Up @@ -11,28 +11,33 @@

namespace EasyWeChat\Payment\Notify;

use Closure;
use EasyWeChat\Kernel\Support\XML;
use Symfony\Component\HttpFoundation\Response;

class Refunded extends Handler
{
protected $check = false;

/**
* @param callable $callback
* @param \Closure $closure
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function handle(callable $callback): Response
public function handle(Closure $closure)
{
$this->strict(
call_user_func_array($callback, [$this->getMessage(), [$this, 'fail']])
$closure->bindTo($this)->__invoke($this->getMessage(), [$this, 'fail'])
);

return $this->toResponse();
}

public function decryptedInfo()
/**
* Decrypt the `req_info` from request message.
*
* @return array
*/
public function reqInfo()
{
return XML::parse($this->decryptMessage('req_info'));
}
Expand Down
12 changes: 4 additions & 8 deletions src/Payment/Notify/Scanned.php
Expand Up @@ -11,7 +11,7 @@

namespace EasyWeChat\Payment\Notify;

use Symfony\Component\HttpFoundation\Response;
use Closure;

class Scanned extends Handler
{
Expand All @@ -24,24 +24,20 @@ class Scanned extends Handler

/**
* @param string $message
*
* @return $this
*/
public function alert(string $message)
{
$this->alert = $message;

return $this;
}

/**
* @param callable $callback
* @param \Closure $closure
*
* @return \Symfony\Component\HttpFoundation\Response
*/
public function handle(callable $callback): Response
public function handle(Closure $closure)
{
$result = call_user_func_array($callback, [$this->getMessage(), [$this, 'fail'], [$this, 'alert']]);
$result = $closure->bindTo($this)->__invoke($this->getMessage(), [$this, 'fail'], [$this, 'alert']);

$attributes = [
'result_code' => is_null($this->alert) && is_null($this->fail) ? static::SUCCESS : static::FAIL,
Expand Down
5 changes: 3 additions & 2 deletions tests/Payment/Notify/PaidTest.php
Expand Up @@ -38,8 +38,9 @@ public function testPaidNotify()

$notify = new Paid($app);

$response = $notify->handle(function ($message) {
$this->assertSame([
$that = $this;
$response = $notify->handle(function ($message) use ($that) {
$that->assertSame([
'foo' => 'bar',
'sign' => '280F9CB28E99DC917792FCC7AC1B88C4',
], $message);
Expand Down

0 comments on commit ac8ceff

Please sign in to comment.