Search code examples
symfonyormdoctrinestripe-paymentspayum

Payum Stripe data flow with Symfony


I am trying to create a checkout that allows a user to create an account for a fee (premium accounts, if you will). A user will create an account (marked as unpaid), the user will pay, and then on a successful payment the account is marked as paid. I can create an account, and I can make a charge. My problem is linking the two things together. I'm not sure how to reference the created account from the successful charge. Here is what I have so far.

Payment.php

<?php
namespace AppBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Payum\Core\Model\ArrayObject;

/**
 * @ORM\Table
 * @ORM\Entity
 */
class Payment extends ArrayObject
{
    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     *
     * @var integer $id
     */
    protected $id;

    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }
}

CreateProfileController.php CreateAction

public function createASquareAction(Request $request, $coupon)
{
    $newUser = new User();
    $registrationForm = $this->getRegistrationForm($newUser);

    $registrationForm->handleRequest($request);

    if ($registrationForm->isSubmitted() && $registrationForm->isValid()) {
        $newSquare = new Square();
        $newSquare->setProduct($registrationForm->get('product')->getData());
        $newUser->addSquare($newSquare);

        $cost = $this->getTotal($newSquare->getProduct(), $registrationForm->get('coupon')->getData());
        $noPayment = $this->isAdmin() || $cost == 0;

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

        if ($noPayment) {
            $newSquare->setVerified(true);
            $em->persist($newSquare);
            $em->flush();

            return $this->redirectToRoute('edit', array(
                'id' => $newSquare->getMsid()
            ));
        } else {
            $gatewayName = 'stripe_checkout_test';
            $storage = $this->get('payum')->getStorage('AppBundle\Entity\Payment');
            $payment = $storage->create();
            $payment["amount"] = $cost;
            $payment["currency"] = 'USD';
            $payment["description"] = "Memorysquare";
            $storage->update($payment);

            $captureToken = $this->get('payum')->getTokenFactory()->createCaptureToken(
                $gatewayName,
                $payment,
                'test_payment_done' // the route to redirect after capture;
            );
            return $this->redirect($captureToken->getTargetUrl());
        }

    }

    return $this->render('create-a-square/create-a-square.html.twig', [
        'registrationForm' => $registrationForm->createView(),
        'coupon' => $coupon,
    ]);
}

Payment Complete Action

public function testPaymentDoneAction(Request $request)
{
    $token = $this->get('payum')->getHttpRequestVerifier()->verify($request);

    $identity = $token->getDetails();
    $model = $this->get('payum')->getStorage($identity->getClass())->find($identity);

    $gateway = $this->get('payum')->getGateway($token->getGatewayName());

    // or Payum can fetch the model for you while executing a request (Preferred).
    $gateway->execute($status = new GetHumanStatus($token));
    $details = $status->getFirstModel();

    return new JsonResponse(array(
        'status' => $status->getValue(),
        'details' => iterator_to_array($details),
    ));
}

Any help to get me on track would be greatly appreciated.


Solution

  • The answer to this was adding my order entity (or any entity you'd like) to the Payment (or PaymentDetails) entity like so (NOTE the cascade persist):

    Payment.php

    // ... all previous code ... //
    
    /**
     * @ORM\OneToOne(targetEntity="Order", cascade={"persist"})
     * @ORM\JoinColumn(name="orderid", referencedColumnName="orderid")
     */
    private $order;
    
    /**
     * Set order
     *
     * @param \AppBundle\Entity\Order $order
     *
     * @return Payment
     */
    public function setOrder(\AppBundle\Entity\Order $order = null)
    {
        $this->order = $order;
    
        return $this;
    }
    
    /**
     * Get order
     *
     * @return \AppBundle\Entity\Order
     */
    public function getOrder()
    {
        return $this->order;
    }
    

    Then in the payment preparation, I add the new order to the $payment object

    public function createASquareAction(Request $request, $coupon)
    {
        // ... previous code ... //
    
        $newOrder = new Order();
        // do some setting on my order 
        $payment->setOrder($newOrder);
        $storage->update($payment);
    
        // ... rest of code ... //
    }
    

    Maybe this will help someone in the future. I also created an event subscriber to check the order onUpdate, and mark as paid if the stripe payment was successful.