John Davidson

php - Razorpay recurring payment through token

0 comments
Message:


I am trying to make a recurring payment through charge token.I want to implement this by API.
I am using the below code make fist payment and save token this is working fine.


 <script src="https://checkout.razorpay.com/v1/checkout.js"
data-key="RAZORPAY_KEY"
data-amount="20000"
data-name="Recurring"
data-description="Test Recurring payment"
data-image="logo.png"
data-prefill.name="User_Name"
data-prefill.email="User Email"
data-description="Total Payable Amount"
data-order_id ="ORDER_ID"
data-customer_id="CUSTOMER_ID"
data-recurring ="1"
data-theme.color="#55A410"></script>

I am able to charge payment through dashboard.
I want to create copy of previous order and deduct payment through token that was saved in first payment.




My Answer:
php

require 'razorpay-php/Razorpay.php';

use Razorpay\Api\Api;

$keyId = 'YOUR_KEY_ID';
$keySecret = 'YOUR_KEY_SECRET';

$api = new Api($keyId, $keySecret);

// Create a customer
$customer = $api->customer->create(array(
'name' => 'John Doe',
'email' => 'john.doe@example.com',
'contact' => '9999999999',
'fail_existing' => 0
));

// Create a token
$token = $api->token->create(array(
'customer_id' => $customer['id'],
'type' => 'card',
'card[number]' => '4111111111111111',
'card[expiry_month]' => '12',
'card[expiry_year]' => '2025',
'card[cvv]' => '123',
'card[name]' => 'John Doe'
));

// Make a payment
$payment = $api->payment->create(array(
'amount' => 1000,
'currency' => 'INR',
'customer_id' => $customer['id'],
'token_id' => $token['id'],
'description' => 'Test Payment',
'capture' => 1
));

// Save the token for future recurring payments
$token_id = $token['id'];

// Make a recurring payment using the saved token
$recurring_payment = $api->payment->create(array(
'amount' => 1000,
'currency' => 'INR',
'customer_id' => $customer['id'],
'token_id' => $token_id,
'description' => 'Recurring Payment',
'capture' => 1
));

?>


This code snippet demonstrates how to make a recurring payment through a charge token using the Razorpay API. It first creates a customer, then creates a token for the customer's card details, makes a payment using the token, saves the token for future recurring payments, and finally makes a recurring payment using the saved token. Make sure to replace 'YOUR_KEY_ID' and 'YOUR_KEY_SECRET' with your actual Razorpay API key and secret.

Rate this post

5 of 5 based on 7868 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us