forked from helei112g/payment
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotifyContext.php
More file actions
83 lines (76 loc) · 2.02 KB
/
NotifyContext.php
File metadata and controls
83 lines (76 loc) · 2.02 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
namespace Payment;
use Payment\Notify\AliNotify;
use Payment\Notify\CmbNotify;
use Payment\Notify\NotifyStrategy;
use Payment\Notify\PayNotifyInterface;
use Payment\Notify\WxNotify;
use Payment\Common\PayException;
/**
* 异步通知的上下文
* @link https://www.gitbook.com/book/helei112g1/payment-sdk/details
* @link https://helei112g.github.io/
*
* Class NotifyContext
* @package Payment
*/
class NotifyContext
{
/**
* 支付的渠道
* @var NotifyStrategy
*/
protected $notify;
/**
* 设置对应的通知渠道
* @param string $channel 通知渠道
* - @see Config
*
* @param array $config 配置文件
* @throws PayException
* @author helei
*/
public function initNotify($channel, array $config)
{
try {
switch ($channel) {
case Config::ALI_CHARGE:
$this->notify = new AliNotify($config);
break;
case Config::WX_CHARGE:
$this->notify = new WxNotify($config);
break;
case Config::CMB_CHARGE:
$this->notify = new CmbNotify($config);
break;
default:
throw new PayException('当前仅支持:ALI_CHARGE WX_CHARGE CMB_CHARGE 常量');
}
} catch (PayException $e) {
throw $e;
}
}
/**
* 返回异步通知的数据
* @return array|false
*/
public function getNotifyData()
{
return $this->notify->getNotifyData();
}
/**
* 通过环境类调用支付异步通知
*
* @param PayNotifyInterface $notify
* @return array
* @throws PayException
* @author helei
*/
public function notify(PayNotifyInterface $notify)
{
if (! $this->notify instanceof NotifyStrategy) {
throw new PayException('请检查初始化是否正确');
}
return $this->notify->handle($notify);
}
}