• This repository has been archived on 25/Apr/2019
  • Stars
    star
    125
  • Rank 285,464 (Top 6 %)
  • Language
    PHP
  • License
    Other
  • Created over 13 years ago
  • Updated about 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

The missing PayPal Digital Goods PHP library. Discontinued - PayPal no longer offer the Digital Goods product.

PayPal Digital Goods PHP Library

Important: PayPal Digital Goods is deprecated as of January 1, 2017. PayPal continues to support existing merchants using this method, but new features and enhancements will not be applied to these integrations. For new integrations, see the PayPal Checkout Integration Guide.

PayPal's Digital Goods for Express Checkout service is a great payment solution with a needlessly complicated API and an unfortunately verbose name.

This library makes it easier to write code using the PayPal Digital Goods API. It supports both one off purchases and recurring payments (subscriptions).

If you are not a programmer, or just want to save yourself the heartache of working with PayPal APIs, try setting up PayPal Digital Goods with WooCommerce - no code required.

Why Use a Class

Using a class to interact with the PayPal API provides all the advantages you love about Object-Oriented programming:

  • Abstraction: the complexity of the PayPal NVP API is hidden behind simple function calls for common operations.
  • Encapsulation: update your application to use the most recent version of the API without changing your application's code.

Examples

Like to learn by example?

Check out the PayPal Digital Goods PHP Examples repository.

Usage

Configuration

Before creating a payment, you need to register a few settings with the PayPal_Digital_Goods_Configuration class.

The minimum configuration settings required are your PayPal API Credentials, a return URI and cancel URI.

<?php 
require_once( 'paypal-digital-goods.class.php' );

PayPal_Digital_Goods_Configuration::username( 'PAYPAL_API_USERNAME' );
PayPal_Digital_Goods_Configuration::password( 'PAYPAL_API_PASSWORD' );
PayPal_Digital_Goods_Configuration::signature( 'PAYPAL_API_SIGNATURE' );

PayPal_Digital_Goods_Configuration::return_url( 'http://example.com/return.php?paypal=paid' );
PayPal_Digital_Goods_Configuration::cancel_url( 'http://example.com/return.php?paypal=cancel' );
PayPal_Digital_Goods_Configuration::notify_url( 'http://example.com/return.php?paypal=notify' );

PayPal_Digital_Goods_Configuration::currency( 'USD' ); // 3 char character code, must be one of the values here: https://developer.paypal.com/docs/classic/api/currency_codes/
?>

Once the PayPal library is configured, you can create a Purchase or Subscription object, or a few of each if you prefer.

Creating a Purchase Object

The PayPal_Purchase class is used to create digital goods purchases. The class's constructor takes a multi-dimensional array of named parameters to customise the purchase to your needs. The individual parameters are explained in detail in the constructor's comments.

Below is a quick example which creates a purchase of two different goods with a total transaction value of $12.00.

<?php 
require_once( 'paypal-purchase.class.php' );

$purchase_details = array(
	'name'        => 'Digital Good Purchase Example',
	'description' => 'Example Digital Good Purchase',
	'amount'      => '14.50', // Total including tax
	'tax_amount'      => '2.50', // Just the total tax amount
	'items'       => array(
		array( // First item
			'item_name'        => 'First item name',
			'item_description' => 'This is a description of the first item in the cart, it costs $9.00',
			'item_amount'      => '9.00',
			'item_tax'         => '1.00',
			'item_quantity'    => 1,
			'item_number'      => 'XF100',
		),
		array( // Second item
			'item_name'        => 'Second Item',
			'item_description' => 'This is a description of the SECOND item in the cart, it costs $1.00 but there are 3 of them.',
			'item_amount'      => '1.00',
			'item_tax'         => '0.50',
			'item_quantity'    => 3,
			'item_number'      => 'XJ100',
		),
	)
);

$paypal_purchase = new PayPal_Purchase( $purchase_details );
?>

These are the purchase details used in the PayPal Digital Goods PHP Examples repository.

Creating a Subscription

The PayPal_Subscription class is used to create recurring payments for digital goods. Much like the PayPal_Purchase class, the PayPal_Subscription class's constructor takes a multi-dimensional array of named parameters to customise the subscription to your needs. The individual parameters are explained in detail in the constructor's comments.

Below is a quick example which creates a $2/week subscription for 4 weeks with a $10.00 sign-up fee.

<?php 

require_once( 'paypal-subscription.class.php' );

$subscription_details = array(
	'description'        => 'Example Subscription: $10 sign-up fee then $2/week for the next four weeks.',
	'initial_amount'     => '10.00',
	'amount'             => '2.00',
	'period'             => 'Week',
	'frequency'          => '1', 
	'total_cycles'       => '4',
);

$paypal_subscription = new PayPal_Subscription( $subscription_details );
?>

Note, it is highly recommend you include the subscription amounts and details in the 'description' parameter as PayPal does not display the subscription details on the confirmation page.

Adding the PayPal Buy Button

Once you have created a purchase or subscription object, the rest is simple.

Add the following line to your checkout or payment page. It will add all necessary scripts and set-up the transaction with PayPal.

<?php $paypal_purchase->print_buy_button(); ?>

Processing a Payment and Starting a Subscription

When a user returns from PayPal, processing their payment or starting their subscription is also a one line operation.

Process a Payment

To process a payment once a user has authorized the purchase with PayPal, call the process_payment() operation on your PayPal_Purchase object.

<?php $paypal_purchase->process_payment(); ?>

Start a Subscription

To start a subscription after a user has authorized the recurring payment plan with PayPal, call the start_subscription() operation on your PayPal_Subscription object.

<?php $paypal_subscription->start_subscription(); ?>

From Sandbox to a Live Environment

By default, the library uses the PayPal Sandbox. Switching from the Sandbox to the live PayPal site is easy, set the environment configuration setting to live.

<?php PayPal_Digital_Goods_Configuration::environment( 'live' ); ?>

Supported PayPal Operations

Supported PayPal API Operations:

  • SetExpressCheckout via request_checkout_token()
  • GetExpressCheckoutDetails via get_checkout_details()
  • DoExpressCheckoutPayment via process_payment()
  • GetTransactionDetails via get_transaction_details( $transaction_id )
  • CreateRecurringPaymentsProfile via start_subscription()
  • GetRecurringPaymentsProfileDetails via get_profile_details( $profile_id )

Using the Library as a Git Submodule

To use the library as a submodule in your application's main git repository, use the following command:

# Add the PayPal Library as a submodule
git submodule add git://github.com/thenbrent/paypal-digital-goods.git paypal-digital-goods

When cloning your application's Git repo, be sure to specify the --recursive option to include the contents of the PayPal submodule.

# Clone my application and all submodules 
git clone --recursive git://github.com/username/app-name.git

Further Reading

PayPal has hidden some excellent articles within the x.commerce dev zone, including:

Pull Requests

Patches are welcome

To submit a patch:

  1. Fork the project.
  2. Make your feature addition or bug fix.
  3. Add examples for any new functionality.
  4. Send me a pull request. Bonus points for topic branches.

The class is written to be friendly to humans, so place special emphasis on readability of your code. It is more important than cleverness and brevity. Your syntax should conform to the WordPress Coding Standards. Provide a brief explanation of each functions purpose in header comments, and comment inline only to explain why your code works. Let your code explain how.

Programs must be written for people to read, and only incidentally for machines to execute. โ€” Structure and Interpretation of Computer Programs

Other Notes

PayPal Digital Goods is deprecated as of January 1, 2017. PayPal continues to support existing merchants using this method, but new features and enhancements will not be applied to these integrations. For new integrations, see the PayPal Checkout Integration Guide.

Also, recurring payments are not available for German PayPal accounts.

More Repositories

1

multisite-user-management

In a Multisite WordPress, automatically add new users to each of your sites.
PHP
122
star
2

paypal-digital-goods-php-examples

Payment & subscription examples using the PayPal Digital Goods PHP Library.
PHP
44
star
3

BB-API

PHP
23
star
4

WP-API-CORS

Enable CORS for the WP JSON REST API.
PHP
18
star
5

bbbolt

Subscription Driven Support for WordPress plugins. Powered by the bbPress plugin, thunder & lightning. Beta.
PHP
16
star
6

chosen-for-wordpress

Implements the Chosen jQuery Plugin for WordPress.
JavaScript
10
star
7

map-cap

Control who can publish, edit and delete custom post types in WordPress. Silly name, useful plugin.
PHP
9
star
8

secureish-file-download

A super simple plugin that offers basic authentication & url hashing to protect a file download link in WordPress.
PHP
9
star
9

grunion-ajax

Using Grunion Contact Form for WordPress? Make form submissions a little more slick with Grunion Ajax.
PHP
7
star
10

custom-taxonomy-columns

Add a column in the WordPress manage posts table for each of your custom taxonomies
Shell
3
star
11

Bing-Maps-for-WordPress-Widget

Builds on the excellent Bing Maps for WordPress plugin by adding a widget.
PHP
3
star
12

restrict-tags

In WordPress, allow a site admin to restrict the non-hierarchical taxonomy items available to other users on their site.
PHP
1
star
13

site-admin-skip-confirmation-email

Restore the skip email notification option on the add users page for site admins.
PHP
1
star
14

avatars-site-admin-fix

Allow site admins to change avatars of users on their site with the IncSub Avatars plugin.
PHP
1
star