Payum
Source🩷 Sponsor
  • Payum docs
  • Get started
  • Instant payment notification
  • ISO4217 or Currency Details
  • The Architecture
  • Your order integration
  • Supported Gateways
  • Storages
  • Debugging
  • Logging
  • Event Dispatcher
  • Configure gateways in backend
  • Working with sensitive information
  • Encrypt gateway configs stored in database
  • Mask credit card number
  • Develop a custom Payum gateway
  • Troubleshooting
  • Frameworks and e-commerce integration.
  • Payum vs Omnipay
  • How to contribute from sub repository
  • Examples
    • Paypal: Create Gateway
    • Paypal: Redirects
    • Handle redirect
    • Get Status
    • Stripe Js: Create gateway
    • Capture Payment
    • Get Http Reponse
    • Capture Credit Card
    • Authorise script
    • Capture Script
    • Done Script
    • index
    • Notify script
    • Payout Script
    • Refund script
  • Authorize-NET
    • AIM
      • Authorize.Net AIM: Get started
  • Be2Bill
    • Be2Bill Direct
    • Be2Bill Offsite
  • jms-payment-bridge
    • Get Started
  • Klarna
    • Checkout
      • Klarna Checkout: Get Started
    • Invoice
      • Klarna Invoice: Get Started
  • Laravel
    • Get Started
    • Blade Templating
    • Eloquent Storage
    • Payment done controller
    • Store gateway config in database
    • Examples
  • Offline
    • Get Started
  • Payex
    • Get Started
  • Paypal
    • Express Checkout
      • Get Started
      • Authorize order
      • Authorize token custom query parameters
      • Cancel recurring payment
      • Confirm order step
      • Recurring Payments Basics
    • IPN
      • Get Started
    • Masspay
      • Get Started
    • Pro Checkout
      • Get Started
    • Pro Hosted
      • Get Started
    • REST
      • Get Started
      • Credit Card Purchase
  • Silex
    • Get Started
    • Payment Done Controller
  • Sofort
    • Get Started
    • Disable Notifications
  • Stripe
    • Checkout
    • Direct
    • Stripe.js
    • Raw Capture
    • Store card and use later
    • Subscription Billing
  • Symfony
    • Get started
    • Authorize Payment
    • Configure payment in backend
    • Console commands
    • Container tags
    • Custom Action
    • Custom API usage
    • Creating custom view for payment page
    • Custom purchase examples
    • Debugging
    • Encrypt gateway configs stored in database
    • ISO4217 or Currency Details
    • Purchase done action
    • Refund Payment
    • Sandbox
    • Storages
    • Configuration Reference
    • Custom Purchase Examples
      • Authorize.NET AIM
      • Be2Bill onsite
      • Be2Bill Credit Card
      • Klarna Checkout
      • Klarna Invoice
      • Payex
      • Paypal Express Checkout
      • Paypal Pro Checkout
      • Paypal via Omnipay
      • Stripe checkout
      • Stripe.js
      • Stripe via Omnipay
Powered by GitBook
On this page
  • Configuration
  • Establish agreement (prepare.php)
  • Create recurring payment
  • Supporting Payum
Edit on GitHub
  1. Paypal
  2. Express Checkout

Recurring Payments Basics

In this chapter we describe the basic steps you have to follow to set up recurring payments. We will use weather subscription as example. Subscription costs $0.05 per day and would last for 7 days.

Configuration

Recurring payments require two additional models: First, one would contain agreement details and the second one recurring payment details. Let's define them:

<?php
namespace App\Model;

class AgreementDetails extends \ArrayObject
{
}

And recurring payment details model:

<?php
namespace App\Model;

class RecurringPaymentDetails extends \ArrayObject
{
}

Now we have to adjust config.php to support paypal recurring payments:

<?php
//config.php

use Payum\Core\Storage\FilesystemStorage;

$agreementClass = 'App\Model\AgreementDetails';
$recurringPaymentClass = 'App\Model\RecurringPaymentDetails';

$storages[$agreementClass] = new FilesystemStorage(
    __DIR__.'/storage',
    $agreementClass
);
$storages[$recurringPaymentClass] = new FilesystemStorage(
    __DIR__.'/storage',
    $recurringPaymentClass
);

Establish agreement (prepare.php)

A user has to agree to be charged periodically. For this we have to create an agreement with him.

<?php
//prepare.php

include __DIR__.'/config.php';

use Payum\Paypal\ExpressCheckout\Nvp\Api;

/** @var \ArrayObject $agreementClass */

/** @var \Payum\Core\Payum $payum */
$storage = $payum->getStorage($agreementClass);

$agreement = $storage->create();
$agreement['PAYMENTREQUEST_0_AMT'] = 0; // For an initial amount to be charged please add it here, eg $10 setup fee
$agreement['L_BILLINGTYPE0'] = Api::BILLINGTYPE_RECURRING_PAYMENTS;
$agreement['L_BILLINGAGREEMENTDESCRIPTION0'] = "Insert some description here";
$agreement['NOSHIPPING'] = 1;
$storage->update($agreement);

$captureToken = $payum->getTokenFactory()->createCaptureToken('paypal', $agreement, 'create_recurring_payment.php');

$storage->update($agreement);

header("Location: ".$captureToken->getTargetUrl());

This script is pretty similar to an ordinary purchase. The only difference here is that we set some special options to agreementDetails. The rest is the same. Create capture token. The 'done' token in this example is renamed to createRecurringPaymentToken. This is because we have one more step to do before we can go to done.php.

Create recurring payment

After capture has done it's job and the agreement has been created we are redirected back to the create_recurring_payment.php script. Here we will check the status of the agreement and if it has been accepted: create a recurring payment. After everything is complete we should redirect the user to a safe page - the page that shows payment details could be a good starting place.

<?php
// create_recurring_payment.php

use Payum\Core\Request\Sync;
use Payum\Core\Request\GetHumanStatus;
use Payum\Paypal\ExpressCheckout\Nvp\Request\Api\CreateRecurringPaymentProfile;
use Payum\Paypal\ExpressCheckout\Nvp\Api;

include __DIR__.'/config.php';

/** @var \ArrayObject $recurringPaymentClass */

/** @var \Payum\Core\Payum $payum */
$token = $payum->getHttpRequestVerifier()->verify($_REQUEST);
$payum->getHttpRequestVerifier()->invalidate($token);

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

$agreementStatus = new GetHumanStatus($token);
$gateway->execute($agreementStatus);

if (!$agreementStatus->isCaptured()) {
    header('HTTP/1.1 400 Bad Request', true, 400);
    exit;
}

$agreement = $agreementStatus->getModel();

$storage = $payum->getStorage($recurringPaymentClass);

$recurringPayment = $storage->create();
$recurringPayment['TOKEN'] = $agreement['TOKEN'];
$recurringPayment['DESC'] = 'Subscribe to weather forecast for a week. It is 0.05$ per day.'; // Desc must match agreement 'L_BILLINGAGREEMENTDESCRIPTION' in prepare.php
$recurringPayment['EMAIL'] = $agreement['EMAIL'];
$recurringPayment['AMT'] = 0.05;
$recurringPayment['CURRENCYCODE'] = 'USD';
$recurringPayment['BILLINGFREQUENCY'] = 7;
$recurringPayment['PROFILESTARTDATE'] = date(DATE_ATOM);
$recurringPayment['BILLINGPERIOD'] = Api::BILLINGPERIOD_DAY;

$gateway->execute(new CreateRecurringPaymentProfile($recurringPayment));
$gateway->execute(new Sync($recurringPayment));

$doneToken = $payum->getTokenFactory()->createToken('paypal', $recurringPayment, 'done.php');

header("Location: " . $doneToken->getTargetUrl());

Supporting Payum

Payum is an MIT-licensed open source project with its ongoing development made possible entirely by the support of community and our customers. If you'd like to join them, please consider:

PreviousConfirm order stepNextIPN

Last updated 1 year ago

Become a sponsor