|
| 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 | +} |
0 commit comments