Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Addon Name: PesaPal Single Payments Gateway
- Author: David Gikandi
- Author URI: http://chamabox.co.ke
- Gateway ID: pesapal
- */
- class pesapal extends Membership_Gateway {
- var $gateway = 'pesapal' ;
- var $title = 'PesaPal with Single Payments' ;
- var $issingle = true ;
- public function __construct() {
- parent::__construct();
- add_action( 'M_gateways_settings_' . $this->gateway, array( &$this, 'mysettings' ) );
- // If I want to override the transactions output - then I can use this action
- //add_action('M_gateways_transactions_' . $this->gateway, array(&$this, 'mytransactions'));
- if ( $this->is_active() ) {
- // Subscription form gateway
- add_action( 'membership_purchase_button', array( &$this, 'display_subscribe_button' ), 1, 3 );
- // Payment return
- add_action( 'membership_handle_payment_return_' . $this->gateway, array( &$this, 'handle_pesapal_return' ) );
- add_filter( 'membership_subscription_form_subscription_process', array( &$this, 'signup_free_subscription' ), 10, 2 );
- }
- }
- function mysettings() {
- //
- }
- function single_sub_button($pricing, $subscription, $user_id, $norepeat = false) {
- global $M_options;
- $form = "
- <img src='http://chamabox.co.ke/JoinNow.gif' alt='Subscribe' title='Subscribe' style='width:128px' />
- ";
- return $form;
- }
- function build_subscribe_button($subscription, $pricing, $user_id, $sublevel) {
- if(!empty($pricing)) {
- // check to make sure there is a price in the subscription
- // we don't want to display free ones for a payment system
- $free = true;
- foreach($pricing as $key => $price) {
- if(!empty($price['amount']) && $price['amount'] > 0 ) {
- $free = false;
- }
- }
- if(!$free) {
- if(count($pricing) == 1) {
- // A basic price or a single subscription
- if(in_array($pricing[0]['type'], array('indefinite','finite'))) {
- // one-off payment
- return $this->single_sub_button($pricing, $subscription, $user_id, false);
- } else {
- // simple subscription
- return $this->single_sub_button($pricing, $subscription, $user_id, true);
- }
- } else {
- // something much more complex
- // Complex buttons currently not supported by PesaPal
- //return $this->complex_sub_button($pricing, $subscription, $user_id);
- }
- }
- }
- }
- function display_subscribe_button($subscription, $pricing, $user_id, $sublevel = 1) {
- if(isset($pricing[$sublevel - 1]) && $pricing[$sublevel - 1]['amount'] < 1)
- echo $this->single_free_button($pricing, $subscription, $user_id, $sublevel);
- else
- echo $this->build_subscribe_button($subscription, $pricing, $user_id, $sublevel);
- }
- function build_custom( $user_id, $sub_id, $amount, $sublevel = 0, $fromsub = 0 ) {
- global $M_options;
- $custom = time() . ':' . $user_id . ':' . $sub_id . ':';
- $key = md5('MEMBERSHIP' . $amount);
- if ( $fromsub === false ) {
- $fromsub = filter_input( INPUT_GET, 'from_subscription', FILTER_VALIDATE_INT );
- }
- $custom .= $key;
- $custom .= ":" . $sublevel . ":" . $fromsub;
- return $custom;
- }
- function update() {
- // default action is to return true
- return true;
- }
- }
- Membership_Gateway::register_gateway( 'pesapal', 'pesapal' );
- // Step 1: Package the request to post to PesaPal and sign it
- // Include OAuth
- include_once ( 'OAuth.php' ) ;
- //Assign Variables
- $token = $params = NULL ;
- $consumer_key = 'XXXXXXXXXXXX' ;
- $consumer_secret = 'XXXXXXXXXXXX' ;
- $signature_method = new OAuthSignatureMethod_HMAC_SHA1( ) ;
- $iframelink = 'https://www.pesapal.com/API/PostPesapalDirectOrderV4' ;
- // Step 2: Post the request to PesaPal and load the PesaPal payments page
- // Assign Form Details passed to pesapal?iframe.php from shopping?cart?form.php to the specified variables.
- //get form details
- $amount = $_POST['amount'];
- $amount = number_format( $amount, 2 ) ;
- //format amount to 2 decimal places
- $desc = $_POST['description'];
- $type = $_POST['type']; //default value = MERCHANT
- $reference = $_POST['reference'];
- //unique order id of the transaction, generated by merchant
- $first_name = $_POST['first_name']; //[optional]
- $last_name = $_POST['last_name']; //[optional]
- $email = $_POST['email'];
- // Construct the post_xml variable. The format is standard so no editing is required. Encode the variable using htmlentities.
- $callback_url = 'http://chamabox.co.ke/callback_redirect.php' ; //redirect url, the page that will handle the
- response from pesapal.
- $post_xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><PesapalDirectOrderInfo xmlns:xsi=\"http://www.w3.org/2001/XMLSchemainstance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" Amount=\"" . $amount . "\" Description=\"" . $desc . "\" Type=\"" . $type . "\" Reference=\"" . $reference . "\" FirstName=\"" . $first_name . "\" LastName=\"" . $last_name . "\" Email=\"" . $email . "\" PhoneNumber=\"" . $phonenumber . "\" xmlns=\"http://www.pesapal.com\" />" ;
- $post_xml = htmlentities( $post_xml ) ;
- // Construct the OAuth Request url. Using the Oauth class included construct the oauth request url using the parameters declared above (the format is standard so no editing is required)
- $consumer = new OAuthConsumer( $consumer_key, $consumer_secret ) ;
- //post transaction to pesapal
- $iframe_src = OAuthRequest :: from_consumer_and_token( $consumer, $token, "GET", $iframelink, $params ) ;
- $iframe_src->set_parameter( "oauth_callback", $callback_url ) ;
- $iframe_src->set_parameter( "pesapal_request_data", $post_xml ) ;
- $iframe_src->sign_request( $signature_method, $consumer, $token ) ;
- // Step 3: Display a post-payment page to your customer
- //display pesapal - iframe and pass iframe_src
- < iframe src = "<?php echo $iframe_src;?>" width = "100%" height = "700px" scrolling = "no" frameBorder = "0" > < p > Browser unable to load iFrame < / p > < / iframe >
- // Step 4: Query PesaPal for payment status
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement