Skip to content

Commit fbafcb3

Browse files
Added app in app gateway.
1 parent 24b1092 commit fbafcb3

12 files changed

+326
-24
lines changed

src/AppInAppGateway.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,44 @@
88
namespace Omnipay\MoMo;
99

1010
use Omnipay\Common\AbstractGateway;
11+
use Omnipay\MoMo\Message\PayConfirmResponse;
12+
use Omnipay\MoMo\Message\AppInApp\PurchaseRequest;
1113

1214
/**
1315
* @author Vuong Minh <[email protected]>
1416
* @since 1.0.0
1517
*/
1618
class AppInAppGateway extends AbstractGateway
1719
{
18-
use Concerns\Parameters;
20+
use Concerns\AppInAppParameters;
21+
22+
/**
23+
* {@inheritdoc}
24+
*/
25+
public function getDefaultParameters(): array
26+
{
27+
return [
28+
'version' => '2.0',
29+
];
30+
}
31+
32+
/**
33+
* {@inheritdoc}
34+
* @return \Omnipay\Common\Message\RequestInterface|PurchaseRequest
35+
*/
36+
public function purchase(array $options = []): PurchaseRequest
37+
{
38+
return $this->createRequest(PurchaseRequest::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+
}
1951
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
trait AppInAppParameters
11+
{
12+
use Parameters;
13+
14+
/**
15+
* Thiết lập public key do MoMo cấp.
16+
*
17+
* @param string $key
18+
*/
19+
public function setPublicKey(string $key): void
20+
{
21+
$this->setParameter('publicKey', $key);
22+
}
23+
24+
/**
25+
* Thiết lập pos version.
26+
*
27+
* @param string $version
28+
*/
29+
public function setVersion(string $version): void
30+
{
31+
$this->setParameter('version', $version);
32+
}
33+
}

src/Message/AbstractHashRequest.php

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

88
namespace Omnipay\MoMo\Message;
99

10-
use Omnipay\Common\Message\AbstractRequest;
11-
1210
/**
1311
* @author Vuong Minh <[email protected]>
1412
* @since 1.0.0

src/Message/AbstractIncomingRequest.php

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

88
namespace Omnipay\MoMo\Message;
99

10-
use Omnipay\Common\Message\AbstractRequest;
11-
1210
/**
1311
* @author Vuong Minh <[email protected]>
1412
* @since 1.0.0
@@ -21,7 +19,10 @@ abstract class AbstractIncomingRequest extends AbstractRequest
2119
*/
2220
public function getData(): array
2321
{
24-
call_user_func_array([$this, 'validate'], array_keys($parameters = $this->getParameters()));
22+
call_user_func_array(
23+
[$this, 'validate'],
24+
array_keys($parameters = $this->getIncomingParameters())
25+
);
2526

2627
return $parameters;
2728
}
@@ -33,7 +34,9 @@ public function initialize(array $parameters = []): self
3334
{
3435
parent::initialize();
3536

36-
$this->parameters->replace($this->getIncomingParameters());
37+
$this->parameters->replace(
38+
$this->getIncomingParameters()
39+
);
3740

3841
return $this;
3942
}

src/Message/AbstractRequest.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\Message;
9+
10+
use Omnipay\Common\Message\AbstractRequest as BaseAbstractRequest;
11+
12+
/**
13+
* @author Vuong Minh <[email protected]>
14+
* @since 1.0.0
15+
*/
16+
abstract class AbstractRequest extends BaseAbstractRequest
17+
{
18+
/**
19+
* Thiết lập request id của đơn hàng.
20+
*
21+
* @param string $id
22+
*/
23+
public function setRequestId(string $id): void
24+
{
25+
$this->setParameter('requestId', $id);
26+
}
27+
}

src/Message/AbstractSignatureRequest.php

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

88
namespace Omnipay\MoMo\Message;
99

10-
use Omnipay\Common\Message\AbstractRequest;
11-
1210
/**
1311
* @author Vuong Minh <[email protected]>
1412
* @since 1.0.0
@@ -24,20 +22,13 @@ abstract class AbstractSignatureRequest extends AbstractRequest
2422
public function getData(): array
2523
{
2624
$parameters = $this->getParameters();
27-
call_user_func_array([$this, 'validate'], $this->getSignatureParameters());
25+
call_user_func_array(
26+
[$this, 'validate'],
27+
$this->getSignatureParameters()
28+
);
2829
$parameters['signature'] = $this->generateSignature();
2930
unset($parameters['secretKey'], $parameters['testMode']);
3031

3132
return $parameters;
3233
}
33-
34-
/**
35-
* Thiết lập request id của đơn hàng.
36-
*
37-
* @param string $id
38-
*/
39-
public function setRequestId(string $id): void
40-
{
41-
$this->setParameter('requestId', $id);
42-
}
4334
}
Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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\AppInApp;
9+
10+
use Omnipay\MoMo\Concerns\AppInAppParameters;
11+
use Omnipay\MoMo\Message\AbstractHashRequest;
12+
13+
/**
14+
* @author Vuong Minh <[email protected]>
15+
* @since 1.0.0
16+
*/
17+
class PurchaseRequest extends AbstractHashRequest
18+
{
19+
use AppInAppParameters;
20+
21+
/**
22+
* {@inheritdoc}
23+
*/
24+
public function getData(): array
25+
{
26+
$this->validate('appData', 'customerNumber');
27+
$this->setParameter('payType', 3);
28+
29+
return parent::getData();
30+
}
31+
32+
/**
33+
* {@inheritdoc}
34+
*/
35+
public function sendData($data)
36+
{
37+
$response = $this->httpClient->request('POST', $this->getEndpoint().'/pay/app', [
38+
'Content-Type' => 'application/json; charset=utf-8',
39+
], json_encode($data));
40+
$responseData = $response->getBody()->getContents();
41+
42+
return $this->response = new PurchaseResponse($this, json_decode($responseData, true) ?? []);
43+
}
44+
45+
/**
46+
* Thiết app token từ app MoMo gửi sang.
47+
*
48+
* @param string $appData
49+
*/
50+
public function setAppData(string $appData): void
51+
{
52+
$this->setParameter('appData', $appData);
53+
}
54+
55+
/**
56+
* Thiết lập mã cửa hàng.
57+
*
58+
* @param string $id
59+
*/
60+
public function setStoreId(string $id): void
61+
{
62+
$this->setParameter('storeId', $id);
63+
}
64+
65+
/**
66+
* Thiết lập tên cửa hàng.
67+
*
68+
* @param string $name
69+
*/
70+
public function setStoreName(string $name): void
71+
{
72+
$this->setParameter('storeName', $name);
73+
}
74+
75+
/**
76+
* Thiết lập mã đơn hàng.
77+
*
78+
* @param string $id
79+
*/
80+
public function setPartnerRefId(string $id): void
81+
{
82+
$this->setParameter('partnerRefId', $id);
83+
}
84+
85+
/**
86+
* Thiết lập mã đơn hàng bổ sung.
87+
*
88+
* @param string $id
89+
*/
90+
public function setPartnerTransId(string $id): void
91+
{
92+
$this->setParameter('partnerTransId', $id);
93+
}
94+
95+
/**
96+
* Thiết lập tên công ty, tổ chức của bạn.
97+
*
98+
* @param string $name
99+
*/
100+
public function setPartnerName(string $name): void
101+
{
102+
$this->setParameter('partnerName', $name);
103+
}
104+
105+
/**
106+
* Thiết lập số điện thoại khách hàng.
107+
*
108+
* @param string $number
109+
*/
110+
public function setCustomerNumber(string $number): void
111+
{
112+
$this->setParameter('customerNumber', $number);
113+
}
114+
115+
/**
116+
* {@inheritdoc}
117+
*/
118+
protected function getHashParameters(): array
119+
{
120+
$parameters = [
121+
'partnerCode', 'partnerRefId', 'amount',
122+
];
123+
124+
if ($this->getParameter('partnerName')) {
125+
$parameters[] = 'partnerName';
126+
}
127+
128+
if ($this->getParameter('partnerTransId')) {
129+
$parameters[] = 'partnerTransId';
130+
}
131+
132+
if ($this->getParameter('storeId')) {
133+
$parameters[] = 'storeId';
134+
}
135+
136+
if ($this->getParameter('storeName')) {
137+
$parameters[] = 'storeName';
138+
}
139+
140+
return $parameters;
141+
}
142+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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\AppInApp;
9+
10+
use Omnipay\MoMo\Message\AbstractSignatureResponse;
11+
12+
/**
13+
* @author Vuong Minh <[email protected]>
14+
* @since 1.0.0
15+
*/
16+
class PurchaseResponse extends AbstractSignatureResponse
17+
{
18+
/**
19+
* {@inheritdoc}
20+
*/
21+
protected function getSignatureParameters(): array
22+
{
23+
return [
24+
'status', 'message', 'amount', 'transid',
25+
];
26+
}
27+
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
public function getCode(): ?string
32+
{
33+
return $this->data['status'] ?? null;
34+
}
35+
36+
/**
37+
* {@inheritdoc}
38+
*/
39+
public function getTransactionReference(): ?string
40+
{
41+
return $this->data['transid'] ?? null;
42+
}
43+
}

src/Message/PayConfirmRequest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@
1111

1212
/**
1313
* @link https://developers.momo.vn/#/docs/pos_payment?id=x%c3%a1c-nh%e1%ba%adn-giao-d%e1%bb%8bch
14+
* @link https://developers.momo.vn/#/docs/qr_payment?id=x%c3%a1c-nh%e1%ba%adn-giao-d%e1%bb%8bch
15+
* @link https://developers.momo.vn/#/docs/app_in_app?id=x%c3%a1c-nh%e1%ba%adn-giao-d%e1%bb%8bch
1416
*
1517
* @author Vuong Minh <[email protected]>
1618
* @since 1.0.0
1719
*/
1820
class PayConfirmRequest extends AbstractSignatureRequest
1921
{
2022
use Parameters;
21-
use Concerns\RequestSignature;
2223

2324
/**
2425
* Thiết lập mã đơn hàng.

0 commit comments

Comments
 (0)