birkof / netopia-mobilpay-bundle

Netopia MobilPay Payment Gateway Symfony Bundle
MIT License
5 stars 4 forks source link

How to use it? #2

Closed crauliuc closed 5 years ago

crauliuc commented 6 years ago

I installed the bundle. Enabled it in AppKernel. But I do not know how to use it. Can you provide some guidance please ?

birkof commented 5 years ago

@crauliuc without mentioning me I didn't saw your message :( Have you managed to install it, how about to use it? PS: The latest library it's a recode on PSR4

dinu-brie commented 5 years ago

@birkof Great work and thank you for sharing with us. Can you please share also some integration example code (sf4)

birkof commented 5 years ago

@dinu-brie thanks!

Did you manage to install it like in the README file?

dinu-brie commented 5 years ago

@birkof yes we managed to install it and succeeded to make some requests to sandbox, we are still facing some issues with their post on our return url. Some example code would be helpful for us. Thanks in advance.

birkof commented 5 years ago

I'll leave here a piece of my code for credit card confirmation routine:

/**
 * @Route(
 *     "/confirm-payment-transaction.{_format}",
 *     name="netopia_mobilpay_confirm_url",
 *     methods={"POST"},
 *     defaults={"_format"="xml"}
 * )
 *
 * @param Request                         $request
 * @param NetopiaMobilPayServiceInterface $mobilPayService
 *
 * @return Response
 */
public function confirmCreditCard(Request $request, NetopiaMobilPayServiceInterface $mobilPayService) {
     $env_key = $request->request->get('env_key');
     $data = $request->request->get('data');

     if (!empty($env_key) && !empty($data)) {
          try {
               $objPmReq = RequestAbstract::factoryFromEncrypted(
                    $env_key,
                    $data,
                    $mobilPayService->getMobilPayConfiguration()->getPrivateKey()
                );

               if ($objPmReq->objPmNotify->errorCode === '0') {
                    // Switch between actions received
                    switch ($objPmReq->objPmNotify->action) {
                        case 'confirmed':
                        break;
                        ...
                        // @see https://github.com/mobilpay/PHP_CARD/blob/master/cardConfirm.php#L32
                    }
               } else {
                    $errorMessage = trim($objPmReq->objPmNotify->errorMessage);
                    $errorMessage = !empty($errorMessage) ? $errorMessage : PaymentTransaction::RESPONSE_REJECT_MESSAGE;

                    // Update transaction status as rejected
                    $paymentTransaction
                        ->setResponseMessage($errorMessage)
                        ->setStatus(PaymentTransaction::STATUS_REJECTED);
               }
          } catch (\Exception $e) {
               $errorType = RequestAbstract::CONFIRM_ERROR_TYPE_TEMPORARY;
               $errorCode = $e->getCode();
               $errorMessage = $e->getMessage();

               $this->logger->warning(
                    'Payment Confirmation Error',
                    compact('errorType', 'errorCode', 'errorMessage')
                );
          }
     } else {
          $errorType = RequestAbstract::CONFIRM_ERROR_TYPE_PERMANENT;
          $errorCode = RequestAbstract::ERROR_CONFIRM_INVALID_POST_PARAMETERS;
          $errorMessage = 'mobilpay.ro posted invalid parameters';
      }

     /** @var string $format */
     $format = $request->getRequestFormat();

     return $this->render(
          sprintf('payment/confirm.%s.twig', $format),
          compact('errorType', 'errorCode', 'errorMessage')
     );
}

and template file (payment/confirm.xml.twig) should contain:

<?xml version="1.0" encoding="utf-8"?>
{% if not errorCode %}
    <crc>{{ errorMessage }}</crc>
{% else %}
    <crc error_type="{{ errorType }}" error_code="{{ errorCode }}">{{ errorMessage }}</crc>
{% endif %}
dinu-brie commented 5 years ago

@birkof Thank you!

birkof commented 5 years ago

@dinu-brie welcome! As this library got some attention, I'll try to find time to write some documentation for it, also provide some code examples.