user3613676
user3613676

Reputation: 48

Create a recurring or subscription payment using Payum Stripe on Symfony 2

I am attempting to attach a plan to a Stripe charge using Payum on Symfony 2. I am able to successfully charge the card but I do not understand how to add the plan to the charge.

I started looking into using the Stripe_Customer object directly, instead of using Payum but I am then having trouble getting the Stripe Token. I added a plan method to the payment object. Any help would be greatly appreciated!

<?php
namespace SEC\PaymentBundle\Controller;
use Payum\Core\Model\CreditCard;
use Payum\Core\Request\GetHttpRequest;
use Payum\Stripe\Action\Api\ObtainTokenAction;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use SEC\PaymentBundle\Entity\Payment;
use Payum\Core\Request\GetHumanStatus;
use Payum\Stripe\Request\Api\CreateCustomer;
use Payum\Stripe\StripeJsGatewayFactory;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
class PaymentController extends Controller
{
    public function prepareAction(Request $request)
    {
        $factory = new StripeJsGatewayFactory();
        $gateway = $factory->create(array(
            'publishable_key' => '****',
            'secret_key' => '****',
        ));
        $card = new \Payum\Core\Model\CreditCard();
        $card->setNumber('4111111111111111');
        $card->setExpireAt(new \DateTime('2018-10-10'));
        $card->setSecurityCode('123');
        $card->setHolder('Test');
        $customer = new \Stripe_Customer();
        $storage = $this->get('payum')->getStorage('SEC\PaymentBundle\Entity\Payment');
        $model = $storage->create();
        $model->setNumber(uniqid());
        $model->setCurrencyCode('USD');
        $model->setTotalAmount(100);
        $model->setClientId($this->getUser());
        $model->setDateCreated(new \DateTime('now'));
        $model->setClientEmail('[email protected]');
        $model->setCreatedBy($this->getUser());
        $model->setCreditCard($card);
        $model->setCustomer($customer);
        $model->setDescription('This is a description');
        $model->setPlan(1);
        $storage->update($model);
        $gateway->execute(new \Payum\Core\Request\Capture($model));
        $captureToken = $this->get('payum.security.token_factory')->createCaptureToken(
            'offline',
            $model,
            'done' // the route to redirect after capture
        );
        return $this->render('SECPaymentBundle:Default:debug.html.twig', array(
            'model2' => $model,
            'model' => $captureToken,
            'model3' => ''
        ));
    }

Upvotes: 1

Views: 2014

Answers (2)

Aerendir
Aerendir

Reputation: 6399

It is explained in the documentation.

Subscribing a customer to a plan

This is a usual charge as we showed it in get-it-started with only these additions:

<?php // prepare.php

/** @var \Payum\Core\Model\PaymentInterface $payment */

$payment->setDetails([
    'amount' => 2000,
    'currency' => 'USD',

    // everything in this section is never sent to the payment gateway
    'local' => [
        'save_card' => true,
        'customer' => ['plan' => $plan['id']],
    ], ]);

As you can see in the array passed to $payment->setDetails() method, the key local has a subkey customer: this subkey accepts an array and setting in it the key plan with the corresponding plan ID on Stripe will subscribe the customer to that plan.

Upvotes: 1

lunetics
lunetics

Reputation: 21

I'm currently working on it, there's already a CreateCustomerAction which can be reused.

So after the CreateCustomerAction creates the customer, you have to add the cust_XXXX token to the model, which can than later be used as charge source (instead of card).

But that needs some kind of different workflow / actions.

Upvotes: 2

Related Questions