Skip to content

Commit bc70c7b

Browse files
Added message concerns
1 parent adee6d6 commit bc70c7b

File tree

4 files changed

+169
-0
lines changed

4 files changed

+169
-0
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
/**
3+
* @link https://github.com/phpviet/omnipay-onepay
4+
* @copyright (c) PHP Viet
5+
* @license [MIT](http://www.opensource.org/licenses/MIT)
6+
*/
7+
8+
namespace Omnipay\OnePay\Message\Concerns;
9+
10+
/**
11+
* @author Vuong Minh <[email protected]>
12+
* @since 1.0.0
13+
*/
14+
trait RequestEndpoint
15+
{
16+
/**
17+
* @var string
18+
*/
19+
protected $productionEndpoint;
20+
21+
/**
22+
* @var string
23+
*/
24+
protected $testEndpoint;
25+
26+
/**
27+
* Trả về url kết nối MoMo.
28+
*
29+
* @return string
30+
*/
31+
protected function getEndpoint(): string
32+
{
33+
return $this->getTestMode() ? $this->testEndpoint : $this->productionEndpoint;
34+
}
35+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
/**
3+
* @link https://github.com/phpviet/omnipay-onepay
4+
* @copyright (c) PHP Viet
5+
* @license [MIT](http://www.opensource.org/licenses/MIT)
6+
*/
7+
8+
namespace Omnipay\OnePay\Message\Concerns;
9+
10+
use Omnipay\OnePay\Support\Signature;
11+
12+
/**
13+
* @author Vuong Minh <[email protected]>
14+
* @since 1.0.0
15+
*/
16+
trait RequestSignature
17+
{
18+
/**
19+
* Trả về chữ ký điện tử gửi đến MoMo dựa theo [[getSignatureParameters()]].
20+
*
21+
* @return string
22+
*/
23+
protected function generateSignature(): string
24+
{
25+
$data = [];
26+
$signature = new Signature($this->getParameter('vpc_HashKey'));
27+
28+
foreach ($this->getSignatureParameters() as $parameter) {
29+
$data[$parameter] = $this->getParameter($parameter);
30+
}
31+
32+
return $signature->generate($data);
33+
}
34+
35+
/**
36+
* Trả về danh sách parameters dùng để tạo chữ ký số.
37+
*
38+
* @return array
39+
*/
40+
abstract protected function getSignatureParameters(): array;
41+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* @link https://github.com/phpviet/omnipay-onepay
4+
* @copyright (c) PHP Viet
5+
* @license [MIT](http://www.opensource.org/licenses/MIT)
6+
*/
7+
8+
namespace Omnipay\OnePay\Message\Concerns;
9+
10+
/**
11+
* @author Vuong Minh <[email protected]>
12+
* @since 1.0.0
13+
*/
14+
trait ResponseProperties
15+
{
16+
/**
17+
* Phương thức hổ trợ tạo các thuộc tính của đối tượng từ dữ liệu gửi về từ MoMo.
18+
*
19+
* @param string $name
20+
* @return null|string
21+
*/
22+
public function __get($name)
23+
{
24+
if (isset($this->data[$name])) {
25+
return $this->data[$name];
26+
} else {
27+
trigger_error(sprintf('Undefined property: %s::%s', __CLASS__, '$'.$name), E_USER_NOTICE);
28+
29+
return;
30+
}
31+
}
32+
33+
/**
34+
* Phương thức hổ trợ bảo vệ các thuộc tính của đối tượng từ dữ liệu gửi về từ MoMo.
35+
*
36+
* @param string $name
37+
* @param mixed $value
38+
* @return null|string
39+
*/
40+
public function __set($name, $value)
41+
{
42+
if (isset($this->data[$name])) {
43+
trigger_error(sprintf('Undefined property: %s::%s', __CLASS__, '$'.$name), E_USER_NOTICE);
44+
} else {
45+
$this->$name = $value;
46+
}
47+
}
48+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
/**
3+
* @link https://github.com/phpviet/omnipay-onepay
4+
* @copyright (c) PHP Viet
5+
* @license [MIT](http://www.opensource.org/licenses/MIT)
6+
*/
7+
8+
namespace Omnipay\OnePay\Message\Concerns;
9+
10+
use Omnipay\OnePay\Support\Signature;
11+
use Omnipay\Common\Exception\InvalidResponseException;
12+
13+
/**
14+
* @author Vuong Minh <[email protected]>
15+
* @since 1.0.0
16+
*/
17+
trait ResponseSignatureValidation
18+
{
19+
/**
20+
* Kiểm tra tính hợp lệ của dữ liệu do MoMo phản hồi.
21+
*
22+
* @throws InvalidResponseException
23+
*/
24+
protected function validateSignature(): void
25+
{
26+
$data = [];
27+
$requestParameters = $this->getRequest()->getParameters();
28+
$signature = new Signature($requestParameters['vpc_HashKey']);
29+
30+
foreach ($this->getSignatureParameters() as $parameter) {
31+
$data[$parameter] = $this->data[$parameter];
32+
}
33+
34+
if (! $signature->validate($data, $this->data['vpc_SecureHash'])) {
35+
throw new InvalidResponseException(sprintf('Data signature response from OnePay is invalid!'));
36+
}
37+
}
38+
39+
/**
40+
* Trả về danh sách các param data đã dùng để tạo chữ ký dữ liệu.
41+
*
42+
* @return array
43+
*/
44+
abstract protected function getSignatureParameters(): array;
45+
}

0 commit comments

Comments
 (0)