Advertisement
Guest User

Untitled

a guest
May 8th, 2014
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.68 KB | None | 0 0
  1. <?php
  2. /*
  3. Addon Name: PesaPal Single Payments Gateway
  4. Author: David Gikandi
  5. Author URI: http://chamabox.co.ke
  6. Gateway ID: pesapal
  7. */
  8.  
  9. class pesapal extends Membership_Gateway {
  10.  
  11. var $gateway = 'pesapal' ;
  12. var $title = 'PesaPal with Single Payments' ;
  13. var $issingle = true ;
  14.  
  15. public function __construct() {
  16. parent::__construct();
  17.  
  18. add_action( 'M_gateways_settings_' . $this->gateway, array( &$this, 'mysettings' ) );
  19.  
  20. // If I want to override the transactions output - then I can use this action
  21. //add_action('M_gateways_transactions_' . $this->gateway, array(&$this, 'mytransactions'));
  22.  
  23. if ( $this->is_active() ) {
  24. // Subscription form gateway
  25. add_action( 'membership_purchase_button', array( &$this, 'display_subscribe_button' ), 1, 3 );
  26.  
  27. // Payment return
  28. add_action( 'membership_handle_payment_return_' . $this->gateway, array( &$this, 'handle_pesapal_return' ) );
  29. add_filter( 'membership_subscription_form_subscription_process', array( &$this, 'signup_free_subscription' ), 10, 2 );
  30. }
  31. }
  32.  
  33. function mysettings() {
  34. //
  35. }
  36.  
  37. function single_sub_button($pricing, $subscription, $user_id, $norepeat = false) {
  38.  
  39. global $M_options;
  40. $form = "
  41. <img src='http://chamabox.co.ke/JoinNow.gif' alt='Subscribe' title='Subscribe' style='width:128px' />
  42. ";
  43.  
  44. return $form;
  45. }
  46.  
  47. function build_subscribe_button($subscription, $pricing, $user_id, $sublevel) {
  48.  
  49. if(!empty($pricing)) {
  50.  
  51. // check to make sure there is a price in the subscription
  52. // we don't want to display free ones for a payment system
  53. $free = true;
  54. foreach($pricing as $key => $price) {
  55. if(!empty($price['amount']) && $price['amount'] > 0 ) {
  56. $free = false;
  57. }
  58. }
  59.  
  60. if(!$free) {
  61. if(count($pricing) == 1) {
  62. // A basic price or a single subscription
  63. if(in_array($pricing[0]['type'], array('indefinite','finite'))) {
  64. // one-off payment
  65. return $this->single_sub_button($pricing, $subscription, $user_id, false);
  66. } else {
  67. // simple subscription
  68. return $this->single_sub_button($pricing, $subscription, $user_id, true);
  69. }
  70. } else {
  71. // something much more complex
  72. // Complex buttons currently not supported by PesaPal
  73. //return $this->complex_sub_button($pricing, $subscription, $user_id);
  74.  
  75. }
  76. }
  77.  
  78. }
  79.  
  80. }
  81.  
  82. function display_subscribe_button($subscription, $pricing, $user_id, $sublevel = 1) {
  83.  
  84. if(isset($pricing[$sublevel - 1]) && $pricing[$sublevel - 1]['amount'] < 1)
  85. echo $this->single_free_button($pricing, $subscription, $user_id, $sublevel);
  86. else
  87. echo $this->build_subscribe_button($subscription, $pricing, $user_id, $sublevel);
  88.  
  89. }
  90.  
  91. function build_custom( $user_id, $sub_id, $amount, $sublevel = 0, $fromsub = 0 ) {
  92. global $M_options;
  93.  
  94. $custom = time() . ':' . $user_id . ':' . $sub_id . ':';
  95. $key = md5('MEMBERSHIP' . $amount);
  96.  
  97. if ( $fromsub === false ) {
  98. $fromsub = filter_input( INPUT_GET, 'from_subscription', FILTER_VALIDATE_INT );
  99. }
  100.  
  101. $custom .= $key;
  102. $custom .= ":" . $sublevel . ":" . $fromsub;
  103.  
  104. return $custom;
  105. }
  106.  
  107. function update() {
  108. // default action is to return true
  109. return true;
  110. }
  111.  
  112. }
  113.  
  114. Membership_Gateway::register_gateway( 'pesapal', 'pesapal' );
  115.  
  116.  
  117. // Step 1: Package the request to post to PesaPal and sign it
  118. // Include OAuth
  119.  
  120. include_once ( 'OAuth.php' ) ;
  121.  
  122. //Assign Variables
  123.  
  124. $token = $params = NULL ;
  125. $consumer_key = 'XXXXXXXXXXXX' ;
  126. $consumer_secret = 'XXXXXXXXXXXX' ;
  127. $signature_method = new OAuthSignatureMethod_HMAC_SHA1( ) ;
  128. $iframelink = 'https://www.pesapal.com/API/PostPesapalDirectOrderV4' ;
  129.  
  130. // Step 2: Post the request to PesaPal and load the PesaPal payments page
  131. // Assign Form Details passed to pesapal?iframe.php from shopping?cart?form.php to the specified variables.
  132. //get form details
  133.  
  134. $amount = $_POST['amount'];
  135. $amount = number_format( $amount, 2 ) ;
  136.  
  137. //format amount to 2 decimal places
  138.  
  139. $desc = $_POST['description'];
  140. $type = $_POST['type']; //default value = MERCHANT
  141. $reference = $_POST['reference'];
  142.  
  143. //unique order id of the transaction, generated by merchant
  144.  
  145. $first_name = $_POST['first_name']; //[optional]
  146. $last_name = $_POST['last_name']; //[optional]
  147. $email = $_POST['email'];
  148.  
  149. // Construct the post_xml variable. The format is standard so no editing is required. Encode the variable using htmlentities.
  150.  
  151. $callback_url = 'http://chamabox.co.ke/callback_redirect.php' ; //redirect url, the page that will handle the
  152. response from pesapal.
  153. $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\" />" ;
  154. $post_xml = htmlentities( $post_xml ) ;
  155.  
  156. // 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)
  157.  
  158. $consumer = new OAuthConsumer( $consumer_key, $consumer_secret ) ;
  159.  
  160. //post transaction to pesapal
  161.  
  162. $iframe_src = OAuthRequest :: from_consumer_and_token( $consumer, $token, "GET", $iframelink, $params ) ;
  163. $iframe_src->set_parameter( "oauth_callback", $callback_url ) ;
  164. $iframe_src->set_parameter( "pesapal_request_data", $post_xml ) ;
  165. $iframe_src->sign_request( $signature_method, $consumer, $token ) ;
  166.  
  167. // Step 3: Display a post-payment page to your customer
  168. //display pesapal - iframe and pass iframe_src
  169.  
  170. < iframe src = "<?php echo $iframe_src;?>" width = "100%" height = "700px" scrolling = "no" frameBorder = "0" > < p > Browser unable to load iFrame < / p > < / iframe >
  171.  
  172. // Step 4: Query PesaPal for payment status
  173.  
  174. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement