Last active
March 20, 2023 12:35
-
-
Save suchithm/aa7e411e44d3383e8f6bc92d8ba97895 to your computer and use it in GitHub Desktop.
RazorPay Android checkout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Com.Razorpay; | |
using System.Net.Http; | |
using Newtonsoft.Json | |
using Xamarin.Forms; | |
[assembly: Dependency(typeof(PaymentInterface))] | |
namespace PlatformServices | |
{ | |
public class PaymentInterface :IPaymentInterface | |
{ | |
public void OnPaymentError(int p0, string p1, PaymentData p2) | |
{ | |
var orderId = p2.OrderId; | |
var paymentId = p2.PaymentId; | |
var PaymentFailureError = p1; | |
} | |
public void OnPaymentSuccess(string p0, PaymentData p1) | |
{ | |
var orderId = p1.OrderId; | |
var paymentId = p1.PaymentId; | |
} | |
public async Task PayViaRazor() | |
{ | |
RazorPayload payload = new RazorPayload(); //update model class | |
payload.amount = (int)amount * 100; | |
payload.currency = "INR"; | |
payload.receipt = Helper.GenerateRefNo(); //refer the above method | |
payload.payment_capture = 1; | |
using (var httpClient = new HttpClient()) | |
{ | |
using (var request = new HttpRequestMessage(new HttpMethod("POST"), "https://api.razorpay.com/v1/orders")) | |
{ | |
var plainTextBytes = Encoding.UTF8.GetBytes($"{RazorPay_Key}:{RazorPay_Secret}"); //update razorpay credentials here | |
var basicAuthKey = Convert.ToBase64String(plainTextBytes); | |
request.Headers.TryAddWithoutValidation("Authorization", $"Basic {basicAuthKey}"); | |
string jsonData = JsonConvert.SerializeObject(payload); | |
request.Content = new StringContent(jsonData); | |
request.Content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json"); | |
var response = await httpClient.SendAsync(request); | |
string jsonResp = await response.Content.ReadAsStringAsync(); | |
RazorResp resp = JsonConvert.DeserializeObject<RazorResp>(jsonResp); | |
if (!string.IsNullOrEmpty(resp.id)) | |
{ | |
// checkout | |
Checkout checkout = new Checkout(); | |
checkout.SetImage(Resource.Drawable.splash_logo); | |
//checkout.SetFullScreenDisable(false); | |
checkout.SetKeyID(username); | |
JSONObject options = new JSONObject(); | |
options.Put("name", "CodeLog"); | |
options.Put("description", $"Reference No. {payload.receipt}"); | |
options.Put("image", "https://s3.amazonaws.com/rzp-mobile/images/rzp.png"); | |
options.Put("order_id", resp.id);//from response of step 3. | |
options.Put("theme.color", "#3399cc"); | |
options.Put("currency", "INR"); | |
options.Put("amount", payload.amount);//pass amount in currency subunits | |
options.Put("prefill.email", myemail); | |
options.Put("prefill.contact", mycontact); | |
checkout.Open(this, options); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment