'How to active Razorpay subscription from authenticated stage
I have run all my code to be active the subscription but its didn't, still is in authenticated process, please help me how can i active razorpay subscription. I created a plan using the api and get a response of planid
$key_id = '';
$key_secret = '';
$startTime = (time()+300);
$amount = '400';
$currency_code = 'INR';
//Create plan for user
$planReqdata = array(
'period' => 'daily',
'interval' => 7,
'item' => array(
'name' => 'Test daily 1 plan',
'description' => 'Description for the daily 1 plan',
'amount' => $amount,
'currency' => $currency_code
)
);
$planurl = 'https://api.razorpay.com/v1/plans';
$palnparams = http_build_query($planReqdata);
//cURL Request
$planch = curl_init();
//set the url, number of POST vars, POST planReqdata
curl_setopt($planch, CURLOPT_URL, $planurl);
curl_setopt($planch, CURLOPT_USERPWD, $key_id . ':' . $key_secret);
curl_setopt($planch, CURLOPT_TIMEOUT, 60);
curl_setopt($planch, CURLOPT_POST, 1);
curl_setopt($planch, CURLOPT_POSTFIELDS, $palnparams);
curl_setopt($planch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($planch, CURLOPT_SSL_VERIFYPEER, true);
$planResult = curl_exec($planch);
$planRes = json_decode($planResult);
//echo $planRes->id;
Using the plan id i have created a subscription and get a response of subscription id
//Create subscription for user
$subdata = array(
'plan_id' => $planRes->id,
'customer_notify' => 1,
'total_count' => 6,
'start_at' => $startTime,
'addons' => array(
array(
'item' => array(
'name' => 'Delivery charges',
'amount' => $amount,
'currency' => $currency_code
)
)
)
);
$suburl = 'https://api.razorpay.com/v1/subscriptions';
$subparams = http_build_query($subdata);
//cURL Request
$subch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($subch, CURLOPT_URL, $suburl);
curl_setopt($subch, CURLOPT_USERPWD, $key_id . ':' . $key_secret);
curl_setopt($subch, CURLOPT_TIMEOUT, 60);
curl_setopt($subch, CURLOPT_POST, 1);
curl_setopt($subch, CURLOPT_POSTFIELDS, $subparams);
curl_setopt($subch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($subch, CURLOPT_SSL_VERIFYPEER, true);
$subResult = curl_exec($subch);
$subres = json_decode($subResult);
//echo $subres->id;
Using the subscription id i have call checkout script
<button id = "rzp-button1">Pay</button>
<script src = "https://checkout.razorpay.com/v1/checkout.js"></script>
<script>
var options = {
"key": "<?php echo $key_id; ?>",
"subscription_id": "<?php echo $subres->id; ?>",
"name": "Test.",
"description": "Daily Test Plan",
"image": "http://localhost/iac/images/logo.png",
"callback_url": "http://localhost/iac/subres.php",
"prefill": {
"name": "Atanu",
"email": "[email protected]",
"contact": "+919876543210"
},
"notes": {
"note_key_1": "Tea. Earl Grey. Hot",
"note_key_2": "Make it so."
},
"theme": {
"color": "#F37254"
}
};
var rzp1 = new Razorpay(options);
document.getElementById('rzp-button1').onclick = function(e) {
rzp1.open();
e.preventDefault();
}
</script>
And pass the response to the below code to fulfill the payment
$razorpayPaymentId = $_POST['razorpay_payment_id'];
$subscriptionId= $_POST['razorpay_subscription_id'];
$secret = '########';
$razorpaySignature = $_POST['razorpay_signature'];
$expectedSignature = hash_hmac('SHA256', $razorpayPaymentId . '|' . $subscriptionId, $secret);
//$expectedSignature = hash_hmac(self::SHA256, $razorpayPaymentId . '|' . $subscriptionId, $secret);
if ($expectedSignature === $razorpaySignature)
{
echo "Payment is successful!";
}
Solution 1:[1]
There is no issue, it's just that when you specify a start date, it will not charge an immediate subscription, so creating a subscription without a start date would immediately make an active subscription.
For further information on why, follow the section Make an Authorized Payment on the following link. There it explains the format of Subscription A which is an instant payment subscription format.
Razorpay Subscription: Make an Authorized Payment
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | S.I. |

