Skip to content

Instantly share code, notes, and snippets.

@jonathonbyrdziak
Created March 16, 2012 23:41
Show Gist options
  • Save jonathonbyrdziak/2053628 to your computer and use it in GitHub Desktop.
Save jonathonbyrdziak/2053628 to your computer and use it in GitHub Desktop.
Paypal Subscription Button Class allows you to easily create a subscription form or email link from the given variables.
<?php
// initializing
$post = get_post($atts['item_number']);
$meta = get_post_custom($post->ID);
foreach ($meta as $p => $v)
$meta[$p] = $v[0];
$datts = array(
'item_number' => $post->ID,
'item_name' => $post->post_title,
);
$datts = wp_parse_args($datts, $meta);
$atts = wp_parse_args($atts, $datts);
// populating the class
$button = redrokk_ppbutton_class::getInstance($atts);
if (isset($meta['a1']) && $meta['a1'][0])
{
$button->set('a1', $meta['a1']);
$button->set('p1', $meta['p1']);
$button->set('t1', $meta['t1']);
}
if (isset($meta['a2']) && $meta['a2'])
{
$button->set('a2', $meta['a2']);
$button->set('p2', $meta['p2']);
$button->set('t2', $meta['t2']);
}
<?php
/**
* @Author Anonymous
* @link http://www.redrokk.com
* @Package Wordpress
* @SubPackage RedRokk Library
* @copyright Copyright (C) 2011+ Redrokk Interactive Media
*
* @version 0.1
*/
defined('ABSPATH') or die('You\'re not supposed to be here.');
/**
* Class creates a payment link for posting to paypal.
*
* @see https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/e_howto_html_Appx_websitestandard_htmlvariables
* @author Anonymous
* @example
$button = redrokk_ppbutton_class::getInstance();
*/
class redrokk_ppbutton_class
{
/**
* The post ID
*
* @var int
*/
var $_id;
/**
*
* @var subscription
*/
var $cmd = '_xclick-subscriptions';
/**
* (Required) Your PayPal ID or an email address associated with your
* PayPal account. Email addresses must be confirmed.
*
* @var string
*/
var $business;
/**
* (optional) The language of the login or sign-up page that
* subscribers see when they click the Subscribe button. If
* unspecified, PayPal determines the language by using a cookie
* in the subscriber’s browser. If there is no PayPal cookie, the
* default language is U.S. English.
*
* @see https://cms.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/e_howto_api_nvp_country_codes
*
* @var string
*/
var $lc = 'US';
/**
* Description of item. If this variable is omitted, buyers enter their
* own name during checkout.
*
* Optional for Buy Now, Donate, Subscribe, Automatic Billing, Installment
* Plan, and Add to Cart buttons. Not used with Buy Gift Certificate buttons.
*
* @var string
*/
var $item_name;
/**
* Pass-through variable for you to track product or service purchased or
* the contribution made. The value you specify is passed back to you upon
* payment completion. This variable is required if you want PayPal to
* track inventory or track profit and loss for the item the button sells.
*
* @var string
*/
var $item_number;
/**
* (Required) Do not prompt buyers to include a note with their payments.
* Allowable values for Subscribe buttons:
*
* 1 – hide the text box and the prompt
* For Subscribe buttons, always include no_note set to 1.
*
* @var boolean
*/
var $no_note = 1;
/**
* Do not prompt buyers for a shipping address.
* Allowable values are:
*
* 0 – prompt for an address, but do not require one. The default is 0.
* 1 – do not prompt for an address
* 2 – prompt for an address, and require one
*
* @var boolean
*/
var $no_shipping = 1;
/**
* The URL to which PayPal redirects buyers’ browser after they complete
* their payments. For example, specify a URL on your site that displays a
* "Thank you for your payment" page.
*
* Default – PayPal redirects the browser to a PayPal webpage.
*
* @var string
*/
var $return_url;
/**
* The URL to which the sender’s browser is redirected if the sender cancels the
* approval for a payment on paypal.com. Use the pay key to identify the payment as
* follows: payKey=${payKey}.
*
* @var unknown_type
*/
var $cancel_url;
/**
* The currency of the payment. The default is USD.
*
* @see https://merchant.paypal.com/us/cgi-bin/?cmd=_render-content&content_ID=developer/e_howto_api_nvp_currency_codes
*
* @var string
*/
var $currency_code = 'USD';
/**
* Recurring times. Number of times that subscription payments recur.
* Specify an integer above 1. Valid only if you specify src="1".
*
* @var boolean
*/
var $srt = 0;
/**
* Recurring payments. Subscription payments recur unless subscribers
* cancel their subscriptions before the end of the current billing cycle
* or you limit the number of times that payments recur with the value that
* you specify for srt.
* Allowable values are:
*
* 0 – subscription payments do not recur
* 1 – subscription payments recur
* The default is 0.
*
* @var boolean
*/
var $src = 1;
/**
*
* @var string
*/
var $notify_url;
/**
* An identifier of the source that built the code for the button that the buyer
* clicked, sometimes known as the build notation. Specify a value using the
* following format:
* <Company>_<Service>_<Product>_<Country>
*
* Substitute <Service> with an appropriate value from the following list:
* BuyNow
* AddToCart
* Donate
* Subscribe
* AutomaticBilling
* InstallmentPlan
* BuyGiftCertifcate
* ShoppingCart
*
* Substitute <Product> with WPS always for Website Payments Standard payment
* buttons and for the Website Payments Standard Cart Upload command.
*
* Substitute <Country> with an appropriate two-letter country code from codes
* defined by the ISO 3166-1 standard.
*
* For example, a Buy Now button on your website that you coded yourself might
* have the following line of code: bn="DesignerFotos_BuyNow_WPS_US"
*
* NOTE: HTML button code that you create on the PayPal website includes bn
* variables with valid values generated by PayPal.
*
* @var string
*/
var $bn = 'PP-SubscriptionsBF:btn_subscribeCC_LG.gif:NonHosted';
/**
* Variables make up the first trial period
*
* @var mixed
*/
var $a1;
var $p1;
var $t1;
/**
* Variables make up the second trial period
*
* @var mixed
*/
var $a2;
var $p2;
var $t2;
/**
* Regular subscription price.
*
* @var INT
*/
var $a3;
/**
* Regular subscription duration. Required if you specify a1. Specify an
* integer value in the allowable range for the units of duration that
* you specify with t1.
*
* @var INT
*/
var $p3;
/**
* Regular subscription units of duration. Required if you specify a1.
* Allowable values are:
*
* D – for days; allowable range for p2 is 1 to 90
* W – for weeks; allowable range for p2 is 1 to 52
* M – for months; allowable range for p2 is 1 to 24
* Y – for years; allowable range for p2 is 1 to 5
*
* @var string
*/
var $t3;
/**
* Variable is a passthru variable that comes back to us just as we sent it.
*
* @var mixed
*/
var $custom;
/**
* Variable determines wether or not this is a test purchase
*
* @var string
*/
var $_test;
/**
* The user can turn the trial offers on and off
*
* @var string
*/
var $_trial_1;
var $_trial_2;
/**
*
*
* @example https://www.paypal.com/cgi-bin/webscr?cmd=_xclick-subscriptions
* &business=jonathonbyrd%40gmail%2ecom
* &lc=US
* &item_name=name&item_number=subid
* &no_note=1&no_shipping=
* &rm=1&return=https%3a%2f%2fwww%2emystore%2ecom%2fsuccess
* &cancel_return=https%3a%2f%2fwww%2emystore%2ecom%2fcancel
* &a1=29%2e00&p1=1&t1=M&a2=3%2e00&p2=1&t2=M&src=1&a3=49%2e00&p3=1&t3=M
* &currency_code=USD
* &srt=2
* &bn=PP%2dSubscriptionsBF%3abtn_subscribeCC_LG%2egif%3aNonHosted
* &notify_url=https%3a%2f%2fwww%2emywebsite%2ecom%2fPayPal_IPN
*
*/
function toUrl()
{
$base = $this->_test ? 'https://www.sandbox.paypal.com/'
: 'https://www.paypal.com/cgi-bin/webscr'; //'https://www.paypal.com/';
return $base.'?'.$this->getQuerystring( 1 );
}
/**
* Function redirects the user to paypal immediately.
*/
function goNow()
{
header('Location: '.$this->toUrl());
exit;
}
/**
* Turns the properties into a querystring
*
* @return string
*/
function getQuerystring( $urlencode = false )
{
$query = array();
foreach($this->getProperties() as $property => $value)
{
$method = 'get'.str_replace(' ','',ucwords(str_replace('_',' ',$property)));
if (method_exists($this, $method)) {
$value = $this->$method( $value );
}
if ($value !== false)
{
if ($urlencode) {
$query[] = $property.'='.urlencode($value);
}
else {
$query[] = $property.'='.$value;
}
}
}
return implode('&', $query);
}
/**
* Returns the paypal email address
*/
function getBusiness( $value = NULL )
{
return apply_filters('ms_business', $value ?$value :$this->business, $this);
}
/**
* Returns the language of WordPress
*/
function getLc( $value = NULL )
{
return strtoupper( $value ?$value :$this->lc );
}
/**
* Whether or not payments are recurring. This return value is completely
* dependent on the srt value
*
* 0 – subscription payments do not recur
* 1 – subscription payments recur
*/
function getSrc( $value = NULL )
{
return $this->src;
}
/**
* Returns the currency code
*/
function getCurrencyCode( $value = NULL )
{
return $value ?$value : $this->currency_code;
}
/**
* Returns the IPN url
*/
function getNotifyUrl( $value = NULL )
{
return apply_filters('ms_notify_url', $value ?$value: $this->notify_url, $this);
}
/**
* Returns the receipt link
*/
function getReturnUrl( $value = NULL )
{
return apply_filters('ms_return_url', $value ?$value: $this->return_url, $this);
}
/**
* Returns the cancellation link
*/
function getCancelUrl( $value = NULL )
{
return apply_filters('ms_cancel_url', $value ?$value: $this->cancel_url, $this);
}
/**
*
*/
function getA1( $value = NULL )
{
if (!$this->_trial_1) return false;
$a = (int)trim(str_replace('$','',$this->a1));
return $value ?$value : ($a?$a:0);
}
/**
*
*/
function getP1( $value = NULL )
{
if (!$this->_trial_1) return false;
return $value ?$value : $this->p1;
}
/**
*
*/
function getT1( $value = NULL )
{
if (!$this->_trial_1) return false;
return $value ?$value : $this->t1;
}
/**
*
*/
function getA2( $value = NULL )
{
if (!$this->_trial_2) return false;
$a = (int)trim(str_replace('$','',$this->a2));
return $value ?$value : ($a?$a:0);
}
/**
*
*/
function getP2( $value = NULL )
{
if (!$this->_trial_2) return false;
return $value ?$value : $this->p2;
}
/**
*
*/
function getT2( $value = NULL )
{
if (!$this->_trial_2) return false;
return $value ?$value : $this->t2;
}
/**
*
* @param unknown_type $value
* @return Ambigous <unknown, number>
*/
function getA3( $value = NULL )
{
$a = (int)trim(str_replace('$','',$this->a3));
return $value ?$value : ($a?$a:0);
}
/**
*
*/
function getFirstPayment()
{
if ($this->_trial_1) {
return $this->getA1();
}
elseif ($this->_trial_2) {
return $this->getA2();
}
else{
return $this->getA3();
}
return false;
}
function debug()
{
$query = array();
foreach($this->getProperties() as $property => $value)
{
$method = 'get'.str_replace(' ','',ucwords(str_replace('_',' ',$property)));
if (method_exists($this, $method)) {
$value = $this->$method( $value );
}
if ($value)
$query[$property] = $value;
}
?><pre><?php var_export($query); ?></pre><?php
}
/**
* Constructor.
*
*/
function __construct( $options = array() )
{
//initializing
$this->bind($options);
}
/**
* Method to bind an associative array or object to the JTable instance.This
* method only binds properties that are publicly accessible and optionally
* takes an array of properties to ignore when binding.
*
* @param mixed $src An associative array or object to bind to the JTable instance.
* @param mixed $ignore An optional array or space separated list of properties to ignore while binding.
*
* @return boolean True on success.
*
* @link http://docs.joomla.org/JTable/bind
* @since 11.
*/
public function bind($src, $ignore = array())
{
// If the source value is not an array or object return false.
if (!is_object($src) && !is_array($src))
{
trigger_error('Bind failed as the provided source is not an array.');
return false;
}
// If the source value is an object, get its accessible properties.
if (is_object($src))
{
$src = get_object_vars($src);
}
// If the ignore value is a string, explode it over spaces.
if (!is_array($ignore))
{
$ignore = explode(' ', $ignore);
}
// Bind the source value, excluding the ignored fields.
foreach ($this->getProperties( false ) as $k => $v)
{
// Only process fields not in the ignore array.
if (!in_array($k, $ignore))
{
if (isset($src[$k]))
{
$this->$k = $src[$k];
}
}
}
return true;
}
/**
* Set the object properties based on a named array/hash.
*
* @param mixed $properties Either an associative array or another object.
*
* @return boolean
*
* @since 11.
*
* @see set()
*/
public function setProperties($properties)
{
if (is_array($properties) || is_object($properties))
{
foreach ((array) $properties as $k => $v)
{
// Use the set function which might be overridden.
$this->set($k, $v);
}
return true;
}
return false;
}
/**
* Modifies a property of the object, creating it if it does not already exist.
*
* @param string $property The name of the property.
* @param mixed $value The value of the property to set.
*
* @return mixed Previous value of the property.
*
* @since 11.
*/
public function set($property, $value = null)
{
$previous = isset($this->$property) ? $this->$property : null;
$this->$property = $value;
return $previous;
}
/**
* Returns an associative array of object properties.
*
* @param boolean $public If true, returns only the public properties.
*
* @return array
*
* @see get()
*/
public function getProperties($public = true)
{
$vars = get_object_vars($this);
if ($public)
{
foreach ($vars as $key => $value)
{
if ('_' == substr($key, 0, 1))
{
unset($vars[$key]);
}
}
}
return $vars;
}
/**
*
* contains the current instance of this class
* @var object
*/
static $_instances = null;
/**
* Method is called when we need to instantiate this class
*
* @param array $options
*/
public static function getInstance( $id, $options = array() )
{
if (!isset(self::$_instances[$id]))
{
$class = get_class();
$options['_id'] = $id;
self::$_instances[$id] =& new $class($options);
}
return self::$_instances[$id];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment