Search code examples
symfonypayum

Payum - Where is the best place to modify payment details?


I started using the PayumBundle which works great out of the box. No I extended my PaypalExpressPaymentDetails entity with my User object:

/**
 * @ORM\OneToOne(targetEntity="Bidder", inversedBy="paymentPaypal")
 * @ORM\JoinColumn(name="bidder_id", referencedColumnName="id")
 */
protected $bidder;

Now I have a question. What is the best place to modify the payment detail entity before and also - more important i think - after the Paypal interaction?

I was having a look at the sandbox code and I guess this must be the right location for setting data before paypal transaction:

        $paymentDetails->setReturnurl($captureToken->getTargetUrl());
        $paymentDetails->setCancelurl($captureToken->getTargetUrl());
        $paymentDetails->setInvnum($paymentDetails->getId());
        $paymentDetails->setBidder($bidder); //i think this should be fine at this position
        $storage->updateModel($paymentDetails);

Here is what I did in the Controller once I got redirected after paying from Paypal (also referencing sandbox code):

    $status = new BinaryMaskStatusRequest($token);
    $payment->execute($status);
    if($status->isSuccess()){
        //mark bidder as paid <- right position?
        $bidder = $status->getModel()->getBidder();
        $bidder->setIsPaid(true);
        $em = $this->getDoctrine()->getManager();
        $em->persist($bidder);
        $em->flush();
    }

Not sure if I should like it as it is. How about an Event Listener for the paypal response? Would be thankful for a suggestion.

Thanks, Marc


Solution

  • As far as I can see you get this right.

    In general you have three steps to go through (more info could be found in payum's get it started).

    1. prepare - where you create payment details and set values into it.
    2. capture - payum takes care of this step.
    3. done - where you getting payment and checking its status. Here you you are free to do whatever you want. Let's say you selling e-books. So here, on success, you generate pdf and send it to buyer email. On fail\cancel etc you show user about it.

    As far as I can see you get this right. About an event: Keep it simple if this the all code you have related to payments. If things become more complected you can introduce an event and move this to a listener:

    $bidder = $status->getModel()->getBidder();
    $bidder->setIsPaid(true);
    $em = $this->getDoctrine()->getManager();
    $em->persist($bidder);
    $em->flush();