|
| 1 | +All In One Gateway |
| 2 | +------------------- |
| 3 | + |
| 4 | +Để nắm sơ lược về khái niệm và cách sử dụng các **Omnipay** gateways bạn hãy truy cập vào [đây](https://omnipay.thephpleague.com/) |
| 5 | +để kham khảo. |
| 6 | + |
| 7 | +## Khởi tạo gateway: |
| 8 | + |
| 9 | +```php |
| 10 | +use Omnipay\Omnipay; |
| 11 | + |
| 12 | +$gateway = Omnipay::create('MoMo_AllInOne'); |
| 13 | +$gateway->setAccessKey('Do MoMo cấp.'); |
| 14 | +$gateway->setPartnerCode('Do MoMo cấp.'); |
| 15 | +$gateway->setSecretKey('Do MoMo cấp.'); |
| 16 | +``` |
| 17 | + |
| 18 | +Gateway khởi tạo ở trên dùng để tạo các yêu cầu xử lý đến MoMo hoặc dùng để nhận yêu cầu do MoMo gửi đến. |
| 19 | + |
| 20 | +## Tạo yêu cầu thanh toán: |
| 21 | + |
| 22 | +```php |
| 23 | +$response = $gateway->purchase([ |
| 24 | + 'amount' => 20000, |
| 25 | + 'returnUrl' => 'http://domaincuaban.com/thanh-toan-thanh-cong/', |
| 26 | + 'notifyUrl' => 'http://domaincuaban.com/ipn/', |
| 27 | + 'orderId' => 'Mã đơn hàng', |
| 28 | + 'requestId' => 'Mã request id, gợi ý nên xài uuid4', |
| 29 | +])->send(); |
| 30 | + |
| 31 | +if ($response->isRedirect()) { |
| 32 | + $redirectUrl = $response->getRedirectUrl(); |
| 33 | + |
| 34 | + // TODO: chuyển khách sang trang MoMo để thanh toán |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +Kham khảo thêm các tham trị khi tạo yêu cầu và MoMo trả về tại [đây](https://developers.momo.vn/#/docs/aio/?id=ph%c6%b0%c6%a1ng-th%e1%bb%a9c-thanh-to%c3%a1n). |
| 39 | + |
| 40 | +## Kiểm tra thông tin `returnUrl` khi khách được MoMo redirect về: |
| 41 | + |
| 42 | +```php |
| 43 | +$response = $gateway->completePurchase()->send(); |
| 44 | + |
| 45 | +if ($response->isSuccessful()) { |
| 46 | + // TODO: xử lý kết quả và hiển thị. |
| 47 | + print $response->amount; |
| 48 | + print $response->orderId; |
| 49 | + |
| 50 | + var_dump($response->getData()); // toàn bộ data do MoMo gửi sang. |
| 51 | + |
| 52 | +} else { |
| 53 | + |
| 54 | + print $response->getMessage(); |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +Kham khảo thêm các tham trị khi MoMo trả về tại [đây](https://developers.momo.vn/#/docs/aio/?id=th%c3%b4ng-tin-tham-s%e1%bb%91). |
| 59 | + |
| 60 | +## Kiểm tra thông tin `notifyUrl` do MoMo gửi sang: |
| 61 | + |
| 62 | +```php |
| 63 | +$response = $gateway->notification()->send(); |
| 64 | + |
| 65 | +if ($response->isSuccessful()) { |
| 66 | + // TODO: xử lý kết quả và hiển thị. |
| 67 | + print $response->amount; |
| 68 | + print $response->orderId; |
| 69 | + |
| 70 | + var_dump($response->getData()); // toàn bộ data do MoMo gửi sang. |
| 71 | + |
| 72 | +} else { |
| 73 | + |
| 74 | + print $response->getMessage(); |
| 75 | +} |
| 76 | +``` |
| 77 | + |
| 78 | +Kham khảo thêm các tham trị khi MoMo gửi sang tại [đây](https://developers.momo.vn/#/docs/aio/?id=th%c3%b4ng-tin-tham-s%e1%bb%91). |
| 79 | + |
| 80 | +## Kiểm tra trạng thái giao dịch: |
| 81 | + |
| 82 | +```php |
| 83 | +$response = $gateway->queryTransaction([ |
| 84 | + 'orderId' => '123', |
| 85 | + 'requestId' => '456', |
| 86 | +])->send(); |
| 87 | + |
| 88 | +if ($response->isSuccessful()) { |
| 89 | + // TODO: xử lý kết quả và hiển thị. |
| 90 | + print $response->amount; |
| 91 | + print $response->orderId; |
| 92 | + |
| 93 | + var_dump($response->getData()); // toàn bộ data do MoMo gửi về. |
| 94 | + |
| 95 | +} else { |
| 96 | + |
| 97 | + print $response->getMessage(); |
| 98 | +} |
| 99 | +``` |
| 100 | + |
| 101 | +Kham khảo thêm các tham trị khi tạo yêu cầu và MoMo trả về tại [đây](https://developers.momo.vn/#/docs/aio/?id=ki%e1%bb%83m-tra-tr%e1%ba%a1ng-th%c3%a1i-giao-d%e1%bb%8bch). |
| 102 | + |
| 103 | +## Yêu cầu hoàn tiền: |
| 104 | + |
| 105 | +```php |
| 106 | +$response = $gateway->refund([ |
| 107 | + 'orderId' => '123', |
| 108 | + 'requestId' => '999', |
| 109 | + 'transId' => 321, |
| 110 | + 'amount' => 50000, |
| 111 | +])->send(); |
| 112 | + |
| 113 | +if ($response->isSuccessful()) { |
| 114 | + // TODO: xử lý kết quả và hiển thị. |
| 115 | + print $response->amount; |
| 116 | + print $response->orderId; |
| 117 | + |
| 118 | + var_dump($response->getData()); // toàn bộ data do MoMo gửi về. |
| 119 | + |
| 120 | +} else { |
| 121 | + |
| 122 | + print $response->getMessage(); |
| 123 | +} |
| 124 | +``` |
| 125 | + |
| 126 | +Kham khảo thêm các tham trị khi tạo yêu cầu và MoMo trả về tại [đây](https://developers.momo.vn/#/docs/aio/?id=ho%c3%a0n-ti%e1%bb%81n-giao-d%e1%bb%8bch). |
| 127 | + |
| 128 | +## Kiểm tra trạng thái hoàn tiền: |
| 129 | + |
| 130 | +```php |
| 131 | +$response = $gateway->queryRefund([ |
| 132 | + 'orderId' => '123', |
| 133 | + 'requestId' => '321', |
| 134 | +])->send(); |
| 135 | + |
| 136 | +if ($response->isSuccessful()) { |
| 137 | + // TODO: xử lý kết quả và hiển thị. |
| 138 | + print $response->amount; |
| 139 | + print $response->orderId; |
| 140 | + |
| 141 | + var_dump($response->getData()); // toàn bộ data do MoMo gửi về. |
| 142 | + |
| 143 | +} else { |
| 144 | + |
| 145 | + print $response->getMessage(); |
| 146 | +} |
| 147 | +``` |
| 148 | + |
| 149 | +Kham khảo thêm các tham trị khi tạo yêu cầu và MoMo trả về tại [đây](https://developers.momo.vn/#/docs/aio/?id=ki%e1%bb%83m-tra-tr%e1%ba%a1ng-th%c3%a1i-ho%c3%a0n-ti%e1%bb%81n). |
| 150 | + |
| 151 | +## Phương thức hổ trợ debug: |
| 152 | + |
| 153 | +Một số phương thức chung hổ trợ debug khi `isSuccessful()` trả về `FALSE`: |
| 154 | + |
| 155 | +```php |
| 156 | + print $response->getCode(); // mã báo lỗi do MoMo gửi sang. |
| 157 | + print $response->getMessage(); // câu thông báo lỗi do MoMo gửi sang. |
| 158 | +``` |
| 159 | + |
| 160 | +Kham khảo bảng báo lỗi `getCode()` chi tiết tại [đây](https://developers.momo.vn/#/docs/aio/?id=b%e1%ba%a3ng-m%c3%a3-l%e1%bb%97i). |
0 commit comments