<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Razorpay subscription Archives - Cody Paste</title>
	<atom:link href="https://www.codypaste.com/tag/razorpay-subscription/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.codypaste.com/tag/razorpay-subscription/</link>
	<description>THE WEB PLAYGROUND</description>
	<lastBuildDate>Fri, 30 Dec 2022 11:12:24 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9</generator>

<image>
	<url>https://www.codypaste.com/wp-content/uploads/2022/12/cropped-codypaste-32x32.png</url>
	<title>Razorpay subscription Archives - Cody Paste</title>
	<link>https://www.codypaste.com/tag/razorpay-subscription/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Razorpay Subscription</title>
		<link>https://www.codypaste.com/razorpay-subscription/</link>
					<comments>https://www.codypaste.com/razorpay-subscription/?noamp=mobile#respond</comments>
		
		<dc:creator><![CDATA[Admin]]></dc:creator>
		<pubDate>Fri, 30 Dec 2022 09:29:06 +0000</pubDate>
				<category><![CDATA[CodeIgniter]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[razorpay]]></category>
		<category><![CDATA[Razorpay subscription]]></category>
		<category><![CDATA[recurring payment]]></category>
		<guid isPermaLink="false">https://www.codypaste.com/?p=824</guid>

					<description><![CDATA[<p>Razorpay Subscription or subscription in Razorpay Payment Gateway integration is not so easy if you want to implement it in your custom project rather than WordPress, Shopify or any other e-commerce platform. There are many plugins available for e-commerce platforms, but here we will integrate Razorpay Subscription / Recurring Payment in CodeIgniter. STEP 1: Go to &#8230; </p>
<p class="link-more"><a href="https://www.codypaste.com/razorpay-subscription/" class="more-link">read more<span class="screen-reader-text"> "Razorpay Subscription"</span></a></p>
<p>The post <a href="https://www.codypaste.com/razorpay-subscription/">Razorpay Subscription</a> appeared first on <a href="https://www.codypaste.com">Cody Paste</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>Razorpay Subscription or subscription in Razorpay Payment Gateway integration is not so easy if you want to implement it in your custom project rather than WordPress, Shopify or any other e-commerce platform.</p>
<p>There are many plugins available for e-commerce platforms, but here we will integrate Razorpay Subscription / Recurring Payment in CodeIgniter.</p>
<h4>STEP 1:</h4>
<p>Go to Razorpay Dashboard and create a &#8216;Plan&#8217;.<br />
<strong>Subscriptions -&gt; Plans</strong></p>
<p>&nbsp;</p>
<h4>STEP 2:</h4>
<p>Create a view file in CodeIgniter view: <strong>application/views/subscription.php</strong> and paste the given code below in that file.</p><pre class="urvanov-syntax-highlighter-plain-tag">&lt;?php
defined('BASEPATH') OR exit('No direct script access allowed');
?&gt;
&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
	&lt;meta charset="utf-8"&gt;
	&lt;title&gt;Razorpay Subscription&lt;/title&gt;	
&lt;/head&gt;
&lt;body&gt;

&lt;div id="container"&gt;

&lt;div id="FormErr" style="width:100%;"&gt;&lt;/div&gt;&lt;br&gt;
&lt;h2&gt;PLAN : 799&lt;/h2&gt;
&lt;p&gt;(Plan Created with the same Amount on Razorpay Dashboard)&lt;/p&gt;
	&lt;input type="text" id="full_name" placeholder="Type your Full name"&gt;
	&lt;input type="text" id="pay_email" placeholder="Type your Email"&gt;
	&lt;input type="text" id="pay_phone" placeholder="Type your Mobile Number"&gt;
	&lt;textarea id="pay_address" &gt;Type your Address&lt;/textarea&gt;
	&lt;button  class="btn-main mybtn1 cmn-btn" id="razor-subscription-pay-now"&gt;Subscribe&lt;/button&gt;
&lt;/div&gt;

&lt;div class="subscribe-loader"&gt;Wait...&lt;/div&gt;


&lt;span id="render-subscription-pay-info"&gt;&lt;/span&gt;&lt;!-- Required DIV--&gt;

&lt;script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"&gt;&lt;/script&gt;
&lt;script type="text/javascript"&gt;
$(document).ready(function(){ 
jQuery(document).on('click', '#razor-subscription-pay-now', function () {	
       jQuery('.subscribe-loader').show();
	   $full_name 	= $('#full_name').val();
	   $pay_email 	= $('#pay_email').val();
	   $pay_phone 	= $('#pay_phone').val();
	   $pay_address = $('#pay_address').val();
	   
      jQuery.ajax({
          url: '&lt;?php echo base_url(); ?&gt;home/initiateSubscriptions',
          type: 'post',
          data: {
			  full_name: $full_name, 
			  pay_email: $pay_email, 
			  pay_phone: $pay_phone, 
			  pay_address: $pay_address
		 },
          dataType: 'json',
          beforeSend: function () {
              //jQuery('#razor-subscription-pay-now').button('loading');
          },
          complete: function () {
              //jQuery('#razor-subscription-pay-now').button('reset');
          },
          success: function (json) {
              $('.text-danger').remove();
              jQuery('.subscribe-loader').hide();
              if (json['error']) {
                  for (i in json['error']) {
                      $('#FormErr').append('&lt;small class="text-danger" style="float:left;"&gt;' + json['error'][i] + '&lt;/small&gt;');
                  }
              } else {                  
                  jQuery.ajax({
                      url: '&lt;?php echo base_url(); ?&gt;home/createSubscription',
                      type: 'post',
                      data: {
						  full_name: $full_name, 
						  pay_email: $pay_email, 
						  pay_phone: $pay_phone, 
						  pay_address: $pay_address
					  },
                      dataType: 'html',                      
                      success: function (html) {                                                   
                       jQuery('span#render-subscription-pay-info').html(html);                       
                      },
                      error: function (xhr, ajaxOptions, thrownError) {
                          console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
                      }
                  });
              }
          },
          error: function (xhr, ajaxOptions, thrownError) {              
              console.log(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
          }
      });
  });
    });
&lt;/script&gt;


&lt;style&gt;
#FormErr small{width:100%; display:inline-block;}
#FormErr small.text-danger{color:red;}
.subscribe-loader{background:#fff; position:fixed; top:0; bottom:0; left:0; right:0; display:none; text-align: center;}
&lt;/style&gt;

&lt;/body&gt;
&lt;/html&gt;</pre><p>&nbsp;</p>
<h4>STEP 3:</h4>
<p>Create a view file in CodeIgniter Controller: <strong>application/controllers/Home.php</strong> and paste the given code below in that file.</p><pre class="urvanov-syntax-highlighter-plain-tag">&lt;?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Home extends CI_Controller {

	public function __construct(){
	    error_reporting(0);
        parent::__construct();
        $this-&gt;load-&gt;library(array('form_validation','session','cart'));
		//$this-&gt;load-&gt;database();
    } 
	public function index(){	   
		$this-&gt;load-&gt;view('index');		
	}
	
	public function initiateSubscriptions() {
    $json = array();
    $firstName 	= $this-&gt;input-&gt;post('full_name');
    $email 		= $this-&gt;input-&gt;post('pay_email');
    $contactNum = $this-&gt;input-&gt;post('pay_phone');
    $address 	= $this-&gt;input-&gt;post('pay_address');
    if (empty($firstName)) {
        $json['error']['fullname'] = 'Please enter full name.';
    }
    if (empty(trim($email))) {
        $json['error']['email'] = 'Please enter valid email.';
    } else if (!preg_match('/^[^\@]+@.*.[a-z]{2,15}$/i', $email)) {
        $json['error']['email'] = 'Please enter valid email.';
    }
    if (empty(trim($contactNum))) {
        $json['error']['contactno'] = 'Please enter valid contact no.';
    } else if (strlen($contactNum) &lt; 7 || !is_numeric($contactNum)) {
        $json['error']['contactno'] = 'Please enter valid contact no.';
    }
    if (empty($address)) {
        $json['error']['address'] = 'Please enter address';
    }
    if(empty($json['error'])){          
           
            $planID = 'plan_JDgG3Q5GGeusiL'; // CReate From Razorpay Dashboard, Subscriptions-&gt;Plans //           
            $note = 'CodyPaste Write Anything';           
            //$offer_id = 'offer_Ky3C9fbr3qg4Ju'; // Not mandatory, but if you have any offer in Razorpay Dashboard, you can mention here. //
            
            $subscriptionData = array(
                'plan_id' =&gt; $planID,
                'customer_notify' =&gt; 1,
                'total_count' =&gt; '1000',
                /*'addons' =&gt; 
                  array (
                    0 =&gt; 
                    array (
                      'item' =&gt; 
                      array (
                        'name' =&gt; 'Coupon Discount',
                        'amount' =&gt; '500',
                        'currency' =&gt; 'INR',
                      ),
                    ),
                  ),*/
                  
                'offer_id'=&gt; $offer_id,
                'notes' =&gt; array(
                    'name' =&gt; $note,
                ),
            );
            $ch = $this-&gt;get_curl_handle_subscriptions($subscriptionData); // Method/Function created below with Razorpay Key and Secret //
            $result = curl_exec($ch);
			
			//print_r($result); die();
            
            $data = json_decode($result);
            $json['subscription_id'] = $data-&gt;id;
            $json['plan_id'] = $data-&gt;plan_id;
            // store value in session
            $this-&gt;session-&gt;set_userdata(
                array(
                    'subscription_id' =&gt; $data-&gt;id,
                    'plan_id' =&gt; $data-&gt;plan_id,
                    'created_at' =&gt; $data-&gt;created_at,
                    'charge_at' =&gt; $data-&gt;charge_at,
                    'start_at' =&gt; $data-&gt;start_at,
                    'offer_id' =&gt; $data-&gt;offer_id,
                )
            );

        
    }
		$this-&gt;output-&gt;set_header('Content-Type: application/json');
		echo json_encode($json);
	}
	
	// initialized cURL Request subscription
	private function get_curl_handle_subscriptions($subscriptionData) {
		$url = 'https://api.razorpay.com/v1/subscriptions';
		$key_id = 'rzp_test_PgK1AWmEYNiWMd'; // Generate from Razorpay Dashboard, Setting -&gt; API Keys
		$key_secret = '6jP4b9WrnKt5JDyDMmJH7zdE'; // Generate from Razorpay Dashboard, Setting -&gt; API Keys
		//cURL Request
		$ch = curl_init();
		//set the url, number of POST vars, POST data
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_USERPWD, $key_id . ':' . $key_secret);
		curl_setopt($ch, CURLOPT_TIMEOUT, 60);
		curl_setopt($ch, CURLOPT_POST, 1);
		$data = $subscriptionData;
		$params = http_build_query($data);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
		return $ch;
	}
	
	    // create subscription first time
public function createSubscription() {
    $fullName 	= $this-&gt;input-&gt;post('full_name');
    $email 		= $this-&gt;input-&gt;post('pay_email');
    $contactNum = $this-&gt;input-&gt;post('pay_phone');
    $address 	= $this-&gt;input-&gt;post('pay_address');
	
	$totalamount = '100';
	$productinfo = 'ORDERID-01';
	$surl = base_url().'subscription-thankyou';
    $furl = base_url().'subscription-failed';
	$orderid = 'ORD-01';
	
    $dataFlesh = array(
        'txnid' =&gt; time(),
        'card_holder_name' 	=&gt; $fullName,
        'amount' 			=&gt; $totalamount,
        'email' 			=&gt; $email,
        'phone' 			=&gt; $contactNum,
        'productinfo' 		=&gt; $productinfo,
        'surl' 				=&gt; $surl,
        'furl' 				=&gt; $furl,
        'currency_code' 	=&gt; 'INR',
        'order_id' 			=&gt; $orderid,
        'lang' 				=&gt; 'en',
        'store_name' 		=&gt; 'Cody Paste',
        'return_url' 		=&gt; site_url() . 'home/callbacksubscriptions',
        'payment_type' 		=&gt; 'create_subscriptions',
        'subscription_id' 	=&gt; $this-&gt;session-&gt;userdata('subscription_id'), // Session craeted at 'initiateSubscriptions()'
        'plan_id' 			=&gt; $this-&gt;session-&gt;userdata('plan_id'),
        'created_at' 		=&gt; $this-&gt;session-&gt;userdata('created_at'),
        'charge_at' 		=&gt; $this-&gt;session-&gt;userdata('charge_at'),
        'date_end_plan_at' 	=&gt; strtotime("+10 years", $this-&gt;session-&gt;userdata('charge_at')),
        'start_at' 			=&gt; $this-&gt;session-&gt;userdata('start_at'),
        'package' 			=&gt; 'CodyPaste Subsription',
        'price' 			=&gt; '100',
        'package_plan_id' 	=&gt; 'plan_JDgG3Q5GGeusiL',
        'package_type' 		=&gt; 'Monthly', // it could be 'Days'
        'offer_id' 			=&gt; $this-&gt;session-&gt;userdata('offer_id'), // Session craeted at 'initiateSubscriptions()'
    );

    $this-&gt;session-&gt;set_userdata('subscription_ci_seesion_key', $dataFlesh); 
    $payInfo = $dataFlesh;
    $json['payInfo'] = $payInfo;
    $json['msg'] = 'success';
    //$this-&gt;output-&gt;set_header('Content-Type: application/json');
    $this-&gt;load-&gt;view('Razorpay-Hidden-Form', $json);
	
	//--END --&gt;
    
   
    //--======-----After that You can Insert in Database table for creating order from below === --&gt;
		//----
	     
	}
	
	
	
	// callback method
public function callbacksubscriptions() {
    if (!empty($this-&gt;input-&gt;post('razorpay_payment_id')) &amp;&amp; !empty($this-&gt;input-&gt;post('merchant_order_id'))) {
        $success = true;
        $error = '';
        try {
            // store temprary data
            $dataFlesh = array(
                'txnid' =&gt; $this-&gt;input-&gt;post('merchant_trans_id'),
                'card_holder_name' =&gt; $this-&gt;input-&gt;post('card_holder_name_id'),
                'productinfo' =&gt; $this-&gt;input-&gt;post('merchant_product_info_id'),
                'surl' =&gt; $this-&gt;input-&gt;post('merchant_surl_id'),
                'furl' =&gt; $this-&gt;input-&gt;post('merchant_furl_id'),
                'order_id' =&gt; $this-&gt;input-&gt;post('merchant_order_id'),
                'razorpay_payment_id' =&gt; $this-&gt;input-&gt;post('razorpay_payment_id'),
                'merchant_subscription_id' =&gt; $this-&gt;input-&gt;post('merchant_subscription_id'),
                'merchant_amount' =&gt; $this-&gt;input-&gt;post('merchant_amount'),
                'merchant_plan_id' =&gt; $this-&gt;input-&gt;post('merchant_plan_id'),
                'created_at' =&gt; time(),
            );
            $this-&gt;session-&gt;set_flashdata('paymentInfo', $dataFlesh);
            
            $this-&gt;session-&gt;set_userdata('paymentInfoReturn', $dataFlesh);
            
            
        } catch (Exception $e) {
            $success = false;
            $error = 'Request to Razorpay Failed';
        }
        if ($success === true) {
            if (!empty($this-&gt;session-&gt;userdata('ci_subscription_keys'))) {
                $this-&gt;session-&gt;unset_userdata('ci_subscription_keys');
            }
            if (!empty($order_info['order_status_id'])) {
                redirect($this-&gt;input-&gt;post('merchant_surl_id'));
            } else {
                redirect($this-&gt;input-&gt;post('merchant_surl_id'));
            }
        } else {
            redirect($this-&gt;input-&gt;post('merchant_furl_id'));
        }
    } else {
        echo 'An error occured. Contact site administrator, please!';
    }
	}
	
	public function subscription_thankyou(){
		echo'Thankyou For Subscription&lt;br&gt;';
		echo 'Subscription ID: '.$this-&gt;session-&gt;userdata('subscription_id');
		
	}
	public function subscription_failed(){
		echo'Sorry! Try Again.';
	}
	
}</pre><p>&nbsp;</p>
<h4><span style="color: #ff9900;"><a style="color: #ff9900;" href="https://github.com/vipulrai/Razorpay-Subscription-CI" target="_blank" rel="noopener"><span style="font-size: 24pt;"><strong>[ DOWNLOAD ]</strong></span></a></span></h4>
<p>&nbsp;</p>
<p>The post <a href="https://www.codypaste.com/razorpay-subscription/">Razorpay Subscription</a> appeared first on <a href="https://www.codypaste.com">Cody Paste</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.codypaste.com/razorpay-subscription/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
