Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DoEC not available - payment not work #91

Closed
jeremydu opened this issue Jan 16, 2019 · 1 comment
Closed

DoEC not available - payment not work #91

jeremydu opened this issue Jan 16, 2019 · 1 comment

Comments

@jeremydu
Copy link

Hello,

I use Sumfony 3.2 and I want to add the payment.
So I implemented the bundles JMS Core Bundle and JMS Paypal Bundle.

From the site, I click paypal, I am redirected to paypal, I made the payment.
Once the payment is done, I am redirected to the site.
Process OK

Problem: the payment is not made or debited on one side or credited to the other.

The bank account is confirmed

According to paypal:

By cons, the next 2 steps are missing (especially the DoEC, GetEC being optional) so the payment is not done, there is just basically the initialization of the payment page is done correctly but no validation thereafter .

How to send validation with the bundle

`<?php

namespace appBundle\Controller;

use appBundle\Entity\Order;
use appBundle\Entity\User;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use JMS\Payment\CoreBundle\PluginController\Result;
use JMS\Payment\CoreBundle\Form\ChoosePaymentMethodType;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use JMS\Payment\CoreBundle\Plugin\Exception\Action\VisitUrl;
use JMS\Payment\CoreBundle\Plugin\Exception\ActionRequiredException;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\HttpFoundation\Cookie;
use Symfony\Component\HttpFoundation\RedirectResponse;

class OrdersController extends Controller
{
public function newAction($strToken, $language)
{
$em = $this->getDoctrine()->getManager();

    if ($language == "") {
        $language = 'fr';
    }
    //Récupère l'utilisateur
    $user = $em
      ->getRepository(User::class)
      ->findOneBy(array('strTokenIdent' => $strToken));

    if (empty($user)) {
        return $this->render('appBundle:Site:premium.html.twig', array(
            'strToken' => $strToken
        ));
    }

    if($language != null)
    {
        $this->get('session')->set('_locale', $language);
    }

     
    $em = $this->getDoctrine()->getManager();

    $order = new Order(12);
    $order->setUser($user);
    $em->persist($order);
    $em->flush();

    return $this->redirect($this->generateUrl('id_show', [
        'id' => $order->getId(),
    ]));
}

public function showAction(Request $request, Order $order)
{
    $config = [
        'paypal_express_checkout' => [
            'return_url' => $this->generateUrl('id_payment_complete', [
                'id' => $order->getId(), 'strToken' => $order->getUser()->getStrTokenIdent(), 'userId' => $order->getUser()->getId()
            ], UrlGeneratorInterface::ABSOLUTE_URL),
            'cancel_url' => $this->generateUrl('id_show', [
                'id' => $order->getId(), 'strToken' => $order->getUser()->getStrTokenIdent(), 'userId' => $order->getUser()->getId()
            ], UrlGeneratorInterface::ABSOLUTE_URL),
            'useraction' => 'commit',
            'default_method' => 'payment_paypal', // Optional
        ],
    ];
    $form = $this->createForm(ChoosePaymentMethodType::class, null, [
        'amount'   => $order->getAmount(),
        'currency' => 'EUR',
        'allowed_methods' => ['paypal_express_checkout'],
        'default_method'  => 'paypal_express_checkout',
        'predefined_data' => $config,
    ]);

    $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $ppc = $this->get('payment.plugin_controller');
        $ppc->createPaymentInstruction($instruction = $form->getData());

        $order->setPaymentInstruction($instruction);

        $em = $this->getDoctrine()->getManager();
        $em->persist($order);
        $em->flush($order);

        return $this->redirect($this->generateUrl('id_payment_create', [
            'id' => $order->getId(),
            'strToken' => $order->getUser()->getStrTokenIdent(),
            'user' => $order->getUser()->getId()
        ]));
    }

    return $this->render('appBundle:Site:show_order.html.twig', array(
        'order' => $order,
        'form'  => $form->createView(),
    ));
}

private function createPayment($order)
{
    $instruction = $order->getPaymentInstruction();
    $pendingTransaction = $instruction->getPendingTransaction();
 
    if ($pendingTransaction !== null) {
        return $pendingTransaction->getPayment();
    }
 
    $ppc = $this->get('payment.plugin_controller');
    $amount = $instruction->getAmount() - $instruction->getDepositedAmount();
 
    return $ppc->createPayment($instruction->getId(), $amount);
}

public function paymentCreateAction(Order $order)
{
    $payment = $this->createPayment($order);

    $ppc = $this->get('payment.plugin_controller');
    $result = $ppc->approveAndDeposit($payment->getId(), $payment->getTargetAmount());

    if ($result->getStatus() === Result::STATUS_PENDING) {
        $ex = $result->getPluginException();
        if ($ex instanceof ActionRequiredException) {
            $action = $ex->getAction();

            if ($action instanceof VisitUrl) {
                return $this->redirect($action->getUrl());
            }
        }
    }
     
    // throw $result->getPluginException();
     
    // In a real-world application you wouldn't throw the exception. You would,
    // for example, redirect to the showAction with a flash message informing
    // the user that the payment was not successful.
    return $this->redirect($this->generateUrl('id_payment_complete', [
                'id' => $order->getId(),
                'strToken' => $order->getUser()->getStrTokenIdent(),
                'userId' => $order->getUser()->getId()
                ]));
}


public function paymentCompleteAction(Order $order, String $strToken, $userId)
{

    $em = $this->getDoctrine()->getManager();

    //Récupère l'utilisateur
    $user = $em
      ->getRepository(User::class)
      ->findOneBy(array('strTokenIdent' => $strToken, 'id' => $userId));

    if (empty($user)) {
        return $this->render('appBundle:Site:premium.html.twig', array(
            'strToken' => $strToken
        ));
    }
     
    $em = $this->getDoctrine()->getManager();
    $user->setLAbonnement(1);
    $user->setLActionListeCourse(0);
    $user->setLActionMenu(0);
    $user->setDtFinAbo(new \DateTime(date('Y-m-d', strtotime('+1 years'))));
    $em->persist($user);
    $em->flush($user);
    return $this->render('appBundle:Site:premium_valid.html.twig', array(
        'strToken' => $strToken
    ));
}

}`

@jeremydu jeremydu changed the title DoEC not available DoEC not available - payment not work Jan 16, 2019
@psrpinto
Copy link
Collaborator

psrpinto commented Feb 6, 2019

Closing in favour of #92

@psrpinto psrpinto closed this as completed Feb 6, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants