NotificationRequest   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 143
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 143
ccs 0
cts 92
cp 0
rs 10
wmc 18

6 Methods

Rating   Name   Duplication   Size   Complexity  
A confirmTransaction() 0 9 2
C getNotifyParams() 0 33 8
A fetchTransaction() 0 7 1
B getReturnParams() 0 38 4
A getData() 0 16 2
A sendData() 0 3 1
1
<?php
2
3
namespace Omnipay\MyCard\Message;
4
5
6
use Omnipay\MyCard\Exception\DefaultException;
7
8
class NotificationRequest extends AbstractRequest
9
{
10
11
    private $ip = [
12
        '220.130.127.125',  // MyCard正式服务器IP
13
        '218.32.37.148'     // MyCard测试服务器IP
14
    ];
15
16
17
    public function getData()
18
    {
19
        if ($this->httpRequest->get('DATA')) {
20
            $this->getNotifyParams();
21
            $type = 'notify';
22
        }
23
        else {
24
            $this->getReturnParams();
25
            $type = 'return';
26
        }
27
        return [
28
            'code'          => $this->getParameter('code'),
29
            'message'       => $this->getParameter('message'),
30
            'transactionId' => $this->getTransactionId(),
31
            'type'          => $type,
32
            'notifyData'    => $this->getParameter('raw')
33
        ];
34
    }
35
36
37
    public function sendData($data)
38
    {
39
        return $this->response = new NotificationResponse($this, $data);
40
    }
41
42
43
    /**
44
     * 客户端Return
45
     * Docs: Version1.9.1#3.2.4 - 回傳參數說明
46
     */
47
    private function getReturnParams()
48
    {
49
        $ReturnCode = $this->httpRequest->get('ReturnCode');        // 1 为成功, 其他则为失败. 注意: ReturnCode 为1并不代表交易成功,正确交易结果请参考PayResult
50
        $ReturnMsg = $this->httpRequest->get('ReturnMsg');
51
        $PayResult = $this->httpRequest->get('PayResult');          // 交易结果代码 交易成功为 3; 交易失败为 0
52
        $FacTradeSeq = $this->httpRequest->get('FacTradeSeq');      // 厂商交易序号
53
        // $PaymentType = $this->httpRequest->get('PaymentType');      // 付费方式
54
        // $Amount = $this->httpRequest->get('Amount');
55
        // $Currency = $this->httpRequest->get('Currency');
56
        // $MyCardType = $this->httpRequest->get('MyCardType');        // 通路代码 PaymentType = INGAME 时才有值
57
        // $PromoCode = $this->httpRequest->get('PromoCode');          // 活动代码
58
        // $Hash = $this->httpRequest->get('Hash');                    // 验证码
59
        // 1.PaymentType=INGAME时,传MyCard卡片号码; 2.PaymentType=COSTPOINT时,传会员扣点交易序号,格式为MMS开头+数; 3.其余PaymentType为Billing小额付款交易,传Billing交易序号
60
        // $MyCardTradeNo = $this->httpRequest->get('MyCardTradeNo');
61
62
63
        // 检查
64
        if ($ReturnCode != 1) {
65
            throw new DefaultException($ReturnMsg);
66
        }
67
        if ($PayResult != 3) {
68
            throw new DefaultException($ReturnMsg);
69
        }
70
71
72
        $token = new TokenRequest($this->httpClient, $this->httpRequest);
73
        $token->initialize($this->getParameters());
74
75
        // 签名验证
76
        if ($token->getSign('returnHash') != $this->httpRequest->get('Hash')) {
77
            throw new DefaultException('Sign Error');
78
        }
79
80
81
        $this->setTransactionId($FacTradeSeq);
82
        $this->setParameter('code', $ReturnCode);
83
        $this->setParameter('message', $ReturnMsg);
84
        $this->setParameter('raw', $this->httpRequest->request->all() + $this->httpRequest->query->all());
85
    }
86
87
88
    /**
89
     * 服务端Notify
90
     * Docs: Version1.9.1#3.6 - MyCard主動通知CP廠商交易成功
91
     * 格式 DATA={"ReturnCode":"1","ReturnMsg":"QueryOK","FacServiceId":"MyCardSDK","TotalNum":2,"FacTradeSeq":["FacTradeSeq0001","FacTradeSeq0002"]}
92
     */
93
    private function getNotifyParams()
94
    {
95
        if (!in_array($this->httpRequest->getClientIp(), $this->ip)) {
96
            throw new DefaultException('IP Is Not Allowed');
97
        }
98
        $data = $this->httpRequest->get('DATA');
99
        try {
100
            $data = json_decode($data, true);
101
        } catch (DefaultException $e) {
102
            throw $e;
103
        }
104
105
        // 检查参数
106
        if (empty($data['ReturnCode'])) {
107
            throw new DefaultException('Missing MyCard ReturnCode');
108
        }
109
        if (empty($data['ReturnMsg'])) {
110
            throw new DefaultException('Missing MyCard ReturnMsg');
111
        }
112
        if (empty($data['FacServiceId'])) {
113
            throw new DefaultException('Missing MyCard FacServiceId');
114
        }
115
        if ($this->getAppId() != $data['FacServiceId']) {
116
            throw new DefaultException('Factory Service Id Not Matched');
117
        }
118
        if ($data['ReturnCode'] != 1) {
119
            throw new DefaultException($data['ReturnMsg']);
120
        }
121
122
        $this->setTransactionId(array_pop($data['FacTradeSeq'])); // TODO :: 仅处理一条记录
123
        $this->setParameter('code', $data['ReturnCode']);
124
        $this->setParameter('message', $data['ReturnMsg']);
125
        $this->setParameter('raw', $data);
126
    }
127
128
129
    // 参考 \Omnipay\MyCard\Message\FetchRequest
130
    public function fetchTransaction($token = '')
131
    {
132
        $this->response = null;
133
        $this->setToken($token);
134
        $fetchRequest = new FetchRequest($this->httpClient, $this->httpRequest);
135
        $fetchRequest->initialize($this->getParameters());
136
        return $fetchRequest->send();
137
    }
138
139
140
    // docs: 3.4 確認 MyCard 交易,並進行請款(Server to Server)
141
    // 注意: 二次扣款也会失败
142
    public function confirmTransaction()
143
    {
144
        $this->response = null;
145
        if (!$this->getToken()) {
146
            return false;
147
        }
148
        $confirmRequest = new ConfirmRequest($this->httpClient, $this->httpRequest);
149
        $confirmRequest->initialize($this->getParameters());
150
        return $confirmRequest->send();
151
    }
152
153
}