Skip to content

Commit 2d87fe2

Browse files
Added QRCode gateway
1 parent fbafcb3 commit 2d87fe2

File tree

11 files changed

+231
-48
lines changed

11 files changed

+231
-48
lines changed

src/AllInOneGateway.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class AllInOneGateway extends AbstractGateway
3030
*/
3131
public function getName(): string
3232
{
33-
return 'MoMo';
33+
return 'MoMo AIO';
3434
}
3535

3636
/**

src/AppInAppGateway.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,15 @@ class AppInAppGateway extends AbstractGateway
1919
{
2020
use Concerns\AppInAppParameters;
2121

22+
/**
23+
* {@inheritdoc}
24+
*/
25+
public function getName(): string
26+
{
27+
return 'MoMo AIA';
28+
}
29+
30+
2231
/**
2332
* {@inheritdoc}
2433
*/

src/Concerns/PosParameters.php renamed to src/Concerns/POSParameters.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* @author Vuong Minh <[email protected]>
1212
* @since 1.0.0
1313
*/
14-
trait PosParameters
14+
trait POSParameters
1515
{
1616
use Parameters;
1717

src/Concerns/QRCodeParameters.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
* @link https://github.com/phpviet/omnipay-momo
4+
* @copyright (c) PHP Viet
5+
* @license [MIT](http://www.opensource.org/licenses/MIT)
6+
*/
7+
8+
namespace Omnipay\MoMo\Concerns;
9+
10+
/**
11+
* @author Vuong Minh <[email protected]>
12+
* @since 1.0.0
13+
*/
14+
trait QRCodeParameters
15+
{
16+
use Parameters;
17+
18+
/**
19+
* Thiết lập access key do MoMo cấp.
20+
*
21+
* @param string $key
22+
*/
23+
public function setAccessKey(string $key): void
24+
{
25+
$this->setParameter('accessKey', $key);
26+
}
27+
}

src/Message/Pos/PurchaseRequest.php renamed to src/Message/POS/PurchaseRequest.php

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
* @license [MIT](http://www.opensource.org/licenses/MIT)
66
*/
77

8-
namespace Omnipay\MoMo\Message\Pos;
8+
namespace Omnipay\MoMo\Message\POS;
99

10-
use Omnipay\MoMo\Concerns\PosParameters;
10+
use Omnipay\MoMo\Concerns\POSParameters;
1111
use Omnipay\MoMo\Message\AbstractHashRequest;
1212

1313
/**
@@ -16,7 +16,34 @@
1616
*/
1717
class PurchaseRequest extends AbstractHashRequest
1818
{
19-
use PosParameters;
19+
use POSParameters;
20+
21+
/**
22+
* {@inheritdoc}
23+
* @throws \Omnipay\Common\Exception\InvalidRequestException
24+
*/
25+
public function getData(): array
26+
{
27+
$this->validate('paymentCode');
28+
$this->setParameter('payType', 3);
29+
$parameters = parent::getData();
30+
unset($parameters['paymentCode']);
31+
32+
return $parameters;
33+
}
34+
35+
/**
36+
* {@inheritdoc}
37+
*/
38+
public function sendData($data): PurchaseResponse
39+
{
40+
$response = $this->httpClient->request('POST', $this->getEndpoint().'/pay/pos', [
41+
'Content-Type' => 'application/json; charset=utf-8',
42+
], json_encode($data));
43+
$responseData = $response->getBody()->getContents();
44+
45+
return $this->response = new PurchaseResponse($this, json_decode($responseData, true) ?? []);
46+
}
2047

2148
/**
2249
* Thiết lập mã cửa hàng.
@@ -58,33 +85,6 @@ public function setPaymentCode(string $code): void
5885
$this->setParameter('paymentCode', $code);
5986
}
6087

61-
/**
62-
* {@inheritdoc}
63-
* @throws \Omnipay\Common\Exception\InvalidRequestException
64-
*/
65-
public function getData(): array
66-
{
67-
$this->validate('paymentCode');
68-
$this->setParameter('payType', 3);
69-
$parameters = parent::getData();
70-
unset($parameters['paymentCode']);
71-
72-
return $parameters;
73-
}
74-
75-
/**
76-
* {@inheritdoc}
77-
*/
78-
public function sendData($data): PurchaseResponse
79-
{
80-
$response = $this->httpClient->request('POST', $this->getEndpoint().'/pay/pos', [
81-
'Content-Type' => 'application/json; charset=utf-8',
82-
], json_encode($data));
83-
$responseData = $response->getBody()->getContents();
84-
85-
return $this->response = new PurchaseResponse($this, json_decode($responseData, true) ?? []);
86-
}
87-
8888
/**
8989
* {@inheritdoc}
9090
*/

src/Message/Pos/PurchaseResponse.php renamed to src/Message/POS/PurchaseResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* @license [MIT](http://www.opensource.org/licenses/MIT)
66
*/
77

8-
namespace Omnipay\MoMo\Message\Pos;
8+
namespace Omnipay\MoMo\Message\POS;
99

1010
use Omnipay\MoMo\Message\AbstractResponse;
1111

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-momo
4+
* @copyright (c) PHP Viet
5+
* @license [MIT](http://www.opensource.org/licenses/MIT)
6+
*/
7+
8+
namespace Omnipay\MoMo\Message\QRCode;
9+
10+
use Omnipay\MoMo\Concerns\QRCodeParameters;
11+
use Omnipay\MoMo\Message\AbstractIncomingRequest;
12+
13+
/**
14+
* @author Vuong Minh <[email protected]>
15+
* @since 1.0.0
16+
*/
17+
class NotificationRequest extends AbstractIncomingRequest
18+
{
19+
use QRCodeParameters;
20+
21+
/**
22+
* {@inheritdoc}
23+
* @throws \Omnipay\Common\Exception\InvalidResponseException
24+
*/
25+
public function sendData($data)
26+
{
27+
return $this->response = new NotificationResponse($this, $data);
28+
}
29+
30+
/**
31+
* {@inheritdoc}
32+
*/
33+
protected function getIncomingParameters(): array
34+
{
35+
$data = [];
36+
$requestData = json_decode($this->httpRequest->getContent(), true) ?? [];
37+
$parameters = [
38+
'accessKey', 'amount', 'message', 'momoTransId', 'partnerCode', 'partnerRefId', 'partnerTransId',
39+
'responseTime', 'status', 'storeId', 'transType', 'signature',
40+
];
41+
42+
foreach ($parameters as $parameter) {
43+
$data[$parameter] = $requestData[$parameter] ?? null;
44+
}
45+
46+
return $data;
47+
}
48+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
/**
3+
* @link https://github.com/phpviet/omnipay-momo
4+
* @copyright (c) PHP Viet
5+
* @license [MIT](http://www.opensource.org/licenses/MIT)
6+
*/
7+
8+
namespace Omnipay\MoMo\Message\QRCode;
9+
10+
use Omnipay\MoMo\Message\AbstractSignatureResponse;
11+
12+
/**
13+
* @author Vuong Minh <[email protected]>
14+
* @since 1.0.0
15+
*/
16+
class NotificationResponse extends AbstractSignatureResponse
17+
{
18+
/**
19+
* {@inheritdoc}
20+
*/
21+
protected function getSignatureParameters(): array
22+
{
23+
return [
24+
'accessKey', 'amount', 'message', 'momoTransId', 'partnerCode', 'partnerRefId', 'partnerTransId',
25+
'responseTime', 'status', 'storeId', 'transType',
26+
];
27+
}
28+
29+
/**
30+
* {@inheritdoc}
31+
*/
32+
public function getCode(): ?string
33+
{
34+
return $this->data['status'] ?? null;
35+
}
36+
37+
/**
38+
* {@inheritdoc}
39+
*/
40+
public function getTransactionId(): ?string
41+
{
42+
return $this->data['partnerRefId'] ?? null;
43+
}
44+
45+
/**
46+
* {@inheritdoc}
47+
*/
48+
public function getTransactionReference()
49+
{
50+
return $this->data['momoTransId'] ?? null;
51+
}
52+
}

src/PosGateway.php renamed to src/POSGateway.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,23 @@
99

1010
use Omnipay\Common\AbstractGateway;
1111
use Omnipay\MoMo\Message\PayConfirmResponse;
12-
use Omnipay\MoMo\Message\Pos\PurchaseRequest;
12+
use Omnipay\MoMo\Message\POS\PurchaseRequest;
1313

1414
/**
1515
* @author Vuong Minh <[email protected]>
1616
* @since 1.0.0
1717
*/
18-
class PosGateway extends AbstractGateway
18+
class POSGateway extends AbstractGateway
1919
{
20-
use Concerns\PosParameters;
20+
use Concerns\POSParameters;
21+
22+
/**
23+
* {@inheritdoc}
24+
*/
25+
public function getName(): string
26+
{
27+
return 'MoMo POS';
28+
}
2129

2230
/**
2331
* {@inheritdoc}

src/QRCodeGateway.php

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* @link https://github.com/phpviet/omnipay-momo
4+
* @copyright (c) PHP Viet
5+
* @license [MIT](http://www.opensource.org/licenses/MIT)
6+
*/
7+
8+
namespace Omnipay\MoMo;
9+
10+
use Omnipay\Common\AbstractGateway;
11+
use Omnipay\MoMo\Message\PayConfirmResponse;
12+
use Omnipay\MoMo\Message\QRCode\NotificationRequest;
13+
14+
/**
15+
* @author Vuong Minh <[email protected]>
16+
* @since 1.0.0
17+
*/
18+
class QRCodeGateway extends AbstractGateway
19+
{
20+
use Concerns\QRCodeParameters;
21+
22+
/**
23+
* {@inheritdoc}
24+
*/
25+
public function getName(): string
26+
{
27+
return 'MoMo QRCode';
28+
}
29+
30+
/**
31+
* Tạo request notification gửi từ MoMo.
32+
*
33+
* @param array $options
34+
* @return \Omnipay\Common\Message\RequestInterface|NotificationRequest
35+
*/
36+
public function notification(array $options = []): NotificationRequest
37+
{
38+
return $this->createRequest(NotificationRequest::class, $options);
39+
}
40+
41+
/**
42+
* Tạo yêu cầu xác nhận hoàn thành hoặc hủy bỏ giao dịch đến MoMo.
43+
*
44+
* @param array $options
45+
* @return \Omnipay\Common\Message\RequestInterface|PayConfirmResponse
46+
*/
47+
public function payConfirm(array $options = []): PayConfirmResponse
48+
{
49+
return $this->createRequest(PayConfirmResponse::class, $options);
50+
}
51+
}

0 commit comments

Comments
 (0)