Skip to content

Commit

Permalink
Done step2 momo
Browse files Browse the repository at this point in the history
  • Loading branch information
Tam Nguyen committed Aug 23, 2019
1 parent f013915 commit 5a9937e
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 31 deletions.
52 changes: 35 additions & 17 deletions Controller/MomoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Response;

class MomoController extends AbstractController
{
Expand All @@ -34,44 +35,60 @@ public function __construct(OrderRepository $OrderRepository,
$this->MomoPaymentService = $MomoPaymentService;
}

/**
* @Route("/ec4-momo/check-status/{orderId}", name="ec4_momo_check_order_status", requirements={"orderId" = "\d+"})
*/
public function checkOrderStatus(Request $request, $orderId)
{
$Order = $this->OrderRepository->find($orderId);
if (!$Order) {
return $this->json(['result' => 0]);
}
$transaction = $this->MomoTransactionRepository->findOneBy(['Order' => $Order]);
if (!$transaction) {
return $this->json(['result' => 0]);
}
return $this->json(['result' => $transaction->getStatus() === '0' ? 1 : 0]);
}

/**
* @Route("/ec4-momo/verify", name="ec4_momo_verify", )
*
*/
public function verify(Request $request)
{
log_info('[MOMO] Called from MOMO at ' . date('Y-m-d H:i:s'));
$params = json_decode($request->getContent());
if ('POST' === $request->getMethod()) {
$secret = $this->MomoConfig->getSecretKey();
$orderId = $request->get("partnerRefId");
$orderId = $params->partnerRefId;
$Order = $this->OrderRepository->find($orderId);
$transaction = $this->MomoTransactionRepository->findOneBy([
'Order' => $Order,
'signature' => $request->get("signature"),
'partnerCode' => $request->get("partnerCode"),
'accessKey' => $request->get("accessKey"),
'amount' => $request->get("amount"),
'signature' => $params->signature,
'partnerCode' => $params->partnerCode,
'accessKey' => $params->accessKey,
'amount' => $params->amount,
]);
$status = 0;
if ($transaction) {
$transaction->setMomoTransId($request->get("momoTransId"));
$transaction->setStatus($request->get("status"));
$transaction->setMessage($request->get("message"));
$transaction->setResponseTime($request->get("responseTime"));
$transaction->setMomoTransId($params->momoTransId);
$transaction->setStatus($params->status === 0 ? \Plugin\EC4MOMO\Entity\MomoTransaction::STATUS_SUCCESS : $params->status);
$transaction->setMessage($params->message);
$transaction->setResponseTime($params->responseTime);
$this->MomoTransactionRepository->save($transaction);
$Status = $entityManager->getRepository('Eccube\Entity\Master\OrderStatus')->find(OrderStatus::PAID);
$Status = $this->entityManager->getRepository('Eccube\Entity\Master\OrderStatus')->find(OrderStatus::PAID);
$Order->setOrderStatus($Status);
$this->OrderRepository->save($Order);
$this->entityManager->flush();
// $this->MomoPaymentService->confirm();
} else {
// $this->MomoPaymentService->rollback();
$status = 1;
}
$status = $status;
$message = $Order->getOrderStatus()->getName();
$amount = $request->get("amount");
$partnerRefId = $request->get("partnerRefId");
$momoTransId = $request->get("momoTransId");
$amount = $params->amount;
$partnerRefId = $params->partnerRefId;
$momoTransId = $params->momoTransId;
$reponse = [
"status" => $status,
"message" => $message,
Expand All @@ -80,8 +97,9 @@ public function verify(Request $request)
"momoTransId" => $momoTransId,
"signature" => hash_hmac('sha256', "amount=$amount&message=$message&momoTransId=$momoTransId&partnerRefId=$partnerRefId&status=$status", $secret)
];
$this->json($reponse);
return $this->json($reponse);
}
abort(404);
return new Response("Page not found", 404);

}
}
2 changes: 2 additions & 0 deletions Entity/MomoTransaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*/
class MomoTransaction
{
const STATUS_SUCCESS = 'success';

/**
* @var int
*
Expand Down
50 changes: 39 additions & 11 deletions Resource/template/momo/qrcode_popup.twig
Original file line number Diff line number Diff line change
@@ -1,18 +1,24 @@
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-content">
<div class="modal-dialog" role="document">
{% set VALID_PERIOD = 30 %}
<style>
#qrcode img {
margin: 5px auto;
}
</style>
<div class="modal fade" id="momoModal" tabindex="-1" role="dialog" aria-labelledby="momoModalLabel" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Quét mã QR để thanh toán với MOMO</h5>
<h5 class="modal-title" id="momoModalLabel">MOMO Thanh toán QR Code</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-body">
<div id="qrcode"></div>
<div id="note" class="text-center">Mã thanh toán của bạn sẽ hết hạn sau: <span id="valid-period">{{VALID_PERIOD}}</span> giây</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">{{ 'common.close' | trans }}</button>
</div>
</div>
</div>
Expand All @@ -21,16 +27,38 @@
<script src="{{ asset('EC4MOMO/assets/js/qrcode.min.js', 'plugin') }}"></script>
<script>
$(function() {
var interval = null;
var VALID_PERIOD = {{ VALID_PERIOD }};
new QRCode($("#qrcode")[0], "{{ QRCode }}");
function showQRCode() {
$('#exampleModal').modal('show');
$('#momoModal').modal('show');
$('#momoModal').on('hidden.bs.modal', function() {
clearInterval(interval);
});
}
$('#shopping-form [type="submit"]').off('click');
$('#shopping-form [type="submit"]').click(function(eve) {
eve.preventDefault();
showQRCode();
// setTimeout(function() {
// $('#shopping-form').submit();
// }, 2000);
var time = 0;
interval = setInterval(function() {
$.ajax({
url: "{{ url('ec4_momo_check_order_status', {'orderId': Order.id}) }}",
async: false,
success: function(res) {
time++;
if (res.result == 1) {
$('#shopping-form').submit();
} else {
if (time >= VALID_PERIOD) {
clearInterval(interval)
window.location.href = "{{ url('shopping_error') }}"
}
}
$('#valid-period').html(VALID_PERIOD - time)
}
});
}, 1000)
})
new QRCode($("#qrcode")[0], "{{ QRCode }}");
})
</script>
5 changes: 2 additions & 3 deletions Service/MomoPaymentService.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public function getQRCode($Order)
$signature = hash_hmac('sha256', "storeSlug=$storeSlug&amount=$amount&billId=$billId", $secret);

$transaction = $this->MomoTransationRepo->findOneBy(['Order' => $Order]);
// if (!$transaction) {
if (!$transaction) {
$transaction = new MomoTransaction();
$transaction->setEnv($domain);
$transaction->setPartnerCode($this->MomoConfig->getPartnerCode());
Expand All @@ -144,8 +144,7 @@ public function getQRCode($Order)
$transaction->setOrder($Order);
$this->MomoTransationRepo->save($transaction);
$this->EntityManager->flush();
dump($transaction); die;
// }
}
$url = "$domain/pay/store/$storeSlug?a=$amount&b=$billId&s=$signature";
return $url;
}
Expand Down

0 comments on commit 5a9937e

Please sign in to comment.