Skip to content

Commit 197613c

Browse files
[WIP] Refactoring code.
1 parent 774dafc commit 197613c

27 files changed

+165
-536
lines changed

src/AllInOneGateway.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
use Omnipay\MoMo\Message\AllInOne\RefundRequest;
1212
use Omnipay\MoMo\Message\AllInOne\PurchaseRequest;
1313
use Omnipay\MoMo\Message\AllInOne\QueryRefundRequest;
14+
use Omnipay\MoMo\Message\AllInOne\NotificationRequest;
1415
use Omnipay\MoMo\Message\AllInOne\QueryTransactionRequest;
1516
use Omnipay\MoMo\Message\AllInOne\CompletePurchaseRequest;
16-
use Omnipay\MoMo\Message\AllInOne\CompletePurchaseNotifyRequest;
1717

1818
/**
1919
* @author Vuong Minh <[email protected]>
@@ -43,14 +43,14 @@ public function completePurchase(array $options = []): CompletePurchaseRequest
4343
}
4444

4545
/**
46-
* Create complete purchase notify request.
46+
* Create complete purchase notification request.
4747
*
4848
* @param array $options
49-
* @return \Omnipay\Common\Message\RequestInterface|CompletePurchaseNotifyRequest
49+
* @return \Omnipay\Common\Message\RequestInterface|NotificationRequest
5050
*/
51-
public function completePurchaseNotify(array $options = []): CompletePurchaseNotifyRequest
51+
public function notification(array $options = []): NotificationRequest
5252
{
53-
return $this->createRequest(CompletePurchaseNotifyRequest::class, $options);
53+
return $this->createRequest(NotificationRequest::class, $options);
5454
}
5555

5656
/**

src/Concerns/Parameters.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,4 @@ public function setPartnerCode(string $code): void
3232
{
3333
$this->setParameter('partnerCode', $code);
3434
}
35-
36-
/**
37-
* Phương thức trừu tượng thiết lập param.
38-
*
39-
* @param $key
40-
* @param $value
41-
* @return mixed
42-
*/
43-
abstract public function setParameter($key, $value);
4435
}

src/Message/AbstractIncomingRequest.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,13 @@
1515
*/
1616
abstract class AbstractIncomingRequest extends AbstractRequest
1717
{
18-
use Concerns\IncomingRequestParameters;
19-
2018
/**
2119
* {@inheritdoc}
2220
* @throws \Omnipay\Common\Exception\InvalidRequestException
2321
*/
2422
public function getData(): array
2523
{
26-
$this->validate(array_keys($parameters = $this->getParameters()));
24+
$this->validate(array_keys($parameters = $this->getIncomingParameters()));
2725

2826
return $parameters;
2927
}
@@ -33,8 +31,17 @@ public function getData(): array
3331
*/
3432
public function initialize(array $parameters = []): self
3533
{
34+
parent::initialize();
35+
3636
$this->parameters->replace($this->getIncomingParameters());
3737

3838
return $this;
3939
}
40+
41+
/**
42+
* Trả về danh sách parameters từ MoMo gửi sang.
43+
*
44+
* @return array
45+
*/
46+
abstract protected function getIncomingParameters(): array;
4047
}

src/Message/AbstractResponse.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,14 @@
1515
*/
1616
abstract class AbstractResponse extends BaseAbstractResponse
1717
{
18+
/**
19+
* 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.
20+
*
21+
* @param string $name
22+
* @return |null
23+
*/
24+
public function __get($name)
25+
{
26+
return $this->data[$name] ?? null;
27+
}
1828
}

src/Message/AllInOne/AbstractIncomingRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@
1515
*/
1616
abstract class AbstractIncomingRequest extends BaseAbstractIncomingRequest
1717
{
18-
use Concerns\RequestParameters;
18+
use Concerns\IncomingRequestParameters;
1919
}

src/Message/AllInOne/AbstractRequest.php

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
namespace Omnipay\MoMo\Message\AllInOne;
99

10+
use InvalidArgumentException;
11+
use Omnipay\MoMo\Support\Signature;
1012
use Omnipay\MoMo\Message\AbstractRequest as BaseAbstractRequest;
1113

1214
/**
@@ -15,9 +17,15 @@
1517
*/
1618
abstract class AbstractRequest extends BaseAbstractRequest
1719
{
18-
use Concerns\RequestSignature;
1920
use Concerns\RequestParameters;
2021

22+
/**
23+
* Trả về lớp đối tượng phản hồi tương ứng của Request.
24+
*
25+
* @var string
26+
*/
27+
protected $responseClass = Response::class;
28+
2129
/**
2230
* {@inheritdoc}
2331
* @throws \Omnipay\Common\Exception\InvalidRequestException
@@ -41,16 +49,52 @@ public function sendData($data)
4149
$response = $this->httpClient->request('POST', $this->getEndpoint().'/gw_payment/transactionProcessor', [
4250
'Content-Type' => 'application/json; charset=UTF-8',
4351
], json_encode($data));
44-
$responseClass = $this->responseClass();
52+
$responseClass = $this->responseClass;
4553
$responseData = json_decode($response->getBody()->getContents(), true);
4654

4755
return $this->response = new $responseClass($this, $responseData);
4856
}
4957

5058
/**
51-
* Trả về lớp đối tượng phản hồi tương ứng của Request.
59+
* Trả về chữ ký điện tử gửi đến MoMo dựa theo `$requestType` truyền vào.
5260
*
61+
* @param string $requestType
5362
* @return string
5463
*/
55-
abstract protected function responseClass(): string;
64+
protected function generateSignature(string $requestType): string
65+
{
66+
$data = [];
67+
$signature = new Signature($this->getParameter('secretKey'));
68+
69+
foreach ($this->getSignatureParameters($requestType) as $parameter) {
70+
$data[$parameter] = $this->getParameter($parameter);
71+
}
72+
73+
return $signature->generate($data);
74+
}
75+
76+
/**
77+
* Trả về danh sách param dùng để tạo chữ ký số theo `$requestType`.
78+
*
79+
* @param string $requestType
80+
* @return array
81+
*/
82+
protected function getSignatureParameters(string $requestType): array
83+
{
84+
switch ($requestType) {
85+
case 'captureMoMoWallet':
86+
return [
87+
'partnerCode', 'accessKey', 'requestId', 'amount', 'orderId', 'orderInfo', 'returnUrl', 'notifyUrl',
88+
'extraData',
89+
];
90+
case 'transactionStatus':
91+
case 'refundMoMoWallet':
92+
case 'refundStatus':
93+
return [
94+
'partnerCode', 'accessKey', 'requestId', 'orderId', 'requestType',
95+
];
96+
default:
97+
throw new InvalidArgumentException(sprintf('Request type: (%s) is not valid!', $requestType));
98+
}
99+
}
56100
}

src/Message/AllInOne/AbstractResponse.php

Lines changed: 0 additions & 59 deletions
This file was deleted.

src/Message/AllInOne/CompletePurchaseNotifyResponse.php

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/Message/AllInOne/CompletePurchaseRequest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class CompletePurchaseRequest extends AbstractIncomingRequest
1818
/**
1919
* {@inheritdoc}
2020
*/
21-
protected function getIncomingParameterBag(): ParameterBag
21+
protected function getIncomingParametersBag(): ParameterBag
2222
{
2323
return $this->httpRequest->query;
2424
}
@@ -27,8 +27,8 @@ protected function getIncomingParameterBag(): ParameterBag
2727
* {@inheritdoc}
2828
* @throws \Omnipay\Common\Exception\InvalidResponseException
2929
*/
30-
public function sendData($data): CompletePurchaseResponse
30+
public function sendData($data): IncomingResponse
3131
{
32-
return $this->response = new CompletePurchaseResponse($this, $data);
32+
return $this->response = new IncomingResponse($this, $data);
3333
}
3434
}

src/Message/AllInOne/CompletePurchaseResponse.php

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)