amitabhaghosh197
9/27/2014 - 9:30 AM

edd

edd

<?php
/*
Plugin Name: Easy Digital Downloads - Sample Gateway
Plugin URL: http://easydigitaldownloads.com/extension/sample-gateway
Description: A sample gateway for Easy Digital Downloads
Version: 1.0
Author: Pippin Williamson
Author URI: http://pippinsplugins.com
Contributors: mordauk
*/

// Don't forget to load the text domain here. Sample text domain is pw_edd


// registers the gateway
function pw_edd_register_gateway( $gateways ) {
	$gateways['payu_money_gateway'] = array( 'admin_label' => 'Pay U Money Gateway', 'checkout_label' => __( 'Pay U Money Gateway', 'pw_edd' ) );
	return $gateways;
}
add_filter( 'edd_payment_gateways', 'pw_edd_register_gateway' );


// Remove this if you want a credit card form
add_action( 'edd_payu_money_gateway_cc_form', '__return_false' );

//function edd_payu_money_gateway_cc_form() {
	// we only register the action so that the default CC form is not shown
	
//}
//add_action( 'edd_payu_money_cc_form', 'edd_payu_money_gateway_cc_form' );



// processes the payment
function pw_edd_process_payment( $purchase_data ) {

	global $edd_options;

	/**********************************
	* set transaction mode
	**********************************/
	$succes_url         =  edd_send_to_success_page();
	$redirect_url       =  edd_send_back_to_checkout( '?payment-mode=' . $purchase_data['post_data']['edd-gateway'] );
	$txn_id             =  $data['download_id']; 
	$amount             =  $data['mc_gross'];
	$product_info       =  $purchase_data['cart_details'];
	$first_name         =  $purchase_data['user_info']['user_first'];
	$email              =  $purchase_data['user_email'];
	         
	$str = "merchant_id|$txnid|$amount|$product_info|$first_name|$email|$txn_id||||||||||salt_key";
	$hash = strtolower(hash('sha512', $str));
	
	$payu_redirect_args =  array(
	        'key'          => $payu_money_gateway_settings['merchant_id'],
			'txnid'        => $data['download_id'],
			'hash'         => $hash,
	        'price'        => $purchase_data['price'],
			'email'        => $purchase_data['user_email'],
			'amount'       => $data['mc_gross'],
	        'productinfo'  => $purchase_data['cart_details'],
			'firstname'    => $purchase_data['user_info']['user_first'],
			'lastname'     => $purchase_data['user_info']['user_last'],
			//'phone'        =>  $user_id-> _edd_user_phone,
			 'surl'        => $succes_url,
			'furl'         => $redirect_url,
			//'curl'         => edd_send_to_success_page(),
			'service_provider' => 'payu_paisa'
			
	);

   $payu_redirect_args_array = array();
   $edd_remove_order = edd_remove_item_url( $cart_key, $post, $ajax );
   
   foreach($payu_redirect_args as $key => $value){
				$payu_redirect_args_array[] = "<input type='hidden' name='$key' value='$value'/>";
			}
			
			return '	<form action="'.$payu_redirect.'" method="post" id="payupaisa_payment_form">
  				' . implode('', $payu_redirect_args_array) . '
				
				<input type="submit" class="button-alt" id="submit_payupaisa_payment_form" value="'.__('Pay via PayU Money', 'edd').'" /> <a class="button cancel" href="'.$edd_remove_order.'">'.__('Cancel order &amp; restore cart', 'edd').'</a>
					<script type="text/javascript">
					jQuery(function(){
					jQuery("body").block({
						message: "'.__('Thank you for your order. We are now redirecting you to Payment Gateway to make payment.', 'edd').'",
						overlayCSS: {
							background		: "#fff",
							opacity			: 0.6
						},
						css: {
							padding			: 20,
							textAlign		: "center",
							color			: "#555",
							border			: "3px solid #aaa",
							backgroundColor	: "#fff",
							cursor			: "wait",
							lineHeight		: "32px"
						}
					});
					jQuery("#submit_payupaisa_payment_form").click();});
					</script>
				</form>';
			
	if ( edd_is_test_mode() ) {
		// set test credentials here
		
		$payu_redirect = 'https://test.payu.in/_payment';
	} else {
		// set live credentials here
		
		$payu_redirect = 'https://secure.payu.in/_payment';
		
	}
	
	
		

		
		// Fix for some sites that encode the entities
	$payu_redirect = str_replace( '&amp;', '&', $payu_redirect );
		
	$payu_redirect .= http_build_query( $payu_redirect_args);
  wp_redirect($payu_redirect);
  exit;
	/**********************************
	* check for errors here
	**********************************/

	
	// errors can be set like this
	if( ! isset($_POST['card_number'] ) ) {
		// error code followed by error message
		edd_set_error('empty_card', __('You must enter a card number', 'edd'));
	}
	


	/**********************************
	* Purchase data comes in like this:

    $purchase_data = array(
        'downloads'     => array of download IDs,
        'tax' 			=> taxed amount on shopping cart
        'fees' 			=> array of arbitrary cart fees
        'discount' 		=> discounted amount, if any
        'subtotal'		=> total price before tax
        'price'         => total price of cart contents after taxes,
        'purchase_key'  =>  // Random key
        'user_email'    => $user_email,
        'date'          => date( 'Y-m-d H:i:s' ),
        'user_id'       => $user_id,
        'post_data'     => $_POST,
        'user_info'     => array of user's information and used discount code
        'cart_details'  => array of cart details,
     );
    */

	// check for any stored errors
	$errors = edd_get_errors();
	if ( ! $errors ) {

		$purchase_summary = edd_get_purchase_summary( $purchase_data );

		/****************************************
		* setup the payment details to be stored
		****************************************/

		$payment = array(
		    
			'price'        => $purchase_data['price'],
			'date'         => $purchase_data['date'],
			'user_email'   => $purchase_data['user_email'],
			'purchase_key' => $purchase_data['purchase_key'],
			'currency'     => $edd_options['currency'],
			'downloads'    => $purchase_data['downloads'],
			'cart_details' => $purchase_data['cart_details'],
			'user_info'    => $purchase_data['user_info'],
			'status'       => 'pending'
		);

		// record the pending payment
		$payment = edd_insert_payment( $payment );

		$merchant_payment_confirmed = false;

		/**********************************
		* Process the credit card here.
		* If not using a credit card
		* then redirect to merchant
		* and verify payment with an IPN
		**********************************/

		// if the merchant payment is complete, set a flag
		$merchant_payment_confirmed = true;

		if ( $merchant_payment_confirmed ) { // this is used when processing credit cards on site

			// once a transaction is successful, set the purchase to complete
			edd_update_payment_status( $payment, 'complete' );

			// record transaction ID, or any other notes you need
			edd_insert_payment_note( $payment, 'txnid' );

			// go to the success page
			edd_send_to_success_page();

		} else {
			$fail = true; // payment wasn't recorded
		}

	} else {
		$fail = true; // errors were detected
	}

	if ( $fail !== false ) {
		// if errors are present, send the user back to the purchase page so they can be corrected
		edd_send_back_to_checkout( '?payment-mode=' . $purchase_data['post_data']['edd-gateway'] );
	}
}
add_action( 'edd_gateway_payu_money_gateway', 'pw_edd_process_payment' );


// adds the settings to the Payment Gateways section
function pw_edd_add_settings( $settings ) {

	$payu_money_gateway_settings = array(
		'header' =>array(
			'id' => 'payu_money_gateway_settings',
			'name' => '<strong>' . __( 'PayU Money Gateway Settings', 'pw_edd' ) . '</strong>',
			'desc' => __( 'Configure the gateway settings', 'pw_edd' ),
			'type' => 'header'
		),
		'merchant_id' => array(
			'id' => 'live_api_key',
			'name' => __( 'Live Merchant Key', 'pw_edd' ),
			'desc' => __( 'Enter your live Merchant key, found in your gateway Account Settings', 'pw_edd' ),
			'type' => 'text',
			'size' => 'regular'
		),
		'salt_key' => array(
			'id' => 'live_salt_key',
			'name' => __( 'Live Salt Key', 'pw_edd' ),
			'desc' => __( 'Enter your live Salt key, found in your gateway Account Settings', 'pw_edd' ),
			'type' => 'text',
			'size' => 'regular'
		),
	
	);

	return array_merge( $settings, $payu_money_gateway_settings );
}
add_filter( 'edd_settings_gateways', 'pw_edd_add_settings' );