razorpay_dart 0.1.0 copy "razorpay_dart: ^0.1.0" to clipboard
razorpay_dart: ^0.1.0 copied to clipboard

A Dart port of the official razorpay-node SDK for the Razorpay API.

example/razorpay_dart_example.dart

// A minimal end-to-end walkthrough: create an order, list what exists, then
// verify a payment signature the way your webhook handler would.
//
// Run with test-mode keys from the environment -- never hardcode them:
//
//   RAZORPAY_KEY_ID=rzp_test_xxx RAZORPAY_KEY_SECRET=yyy \
//     dart run example/razorpay_dart_example.dart
import 'dart:io';

import 'package:razorpay_dart/razorpay_dart.dart';

Future<void> main() async {
  final keyId = Platform.environment['RAZORPAY_KEY_ID'];
  final keySecret = Platform.environment['RAZORPAY_KEY_SECRET'];

  if (keyId == null || keySecret == null) {
    stderr.writeln(
      'Set RAZORPAY_KEY_ID and RAZORPAY_KEY_SECRET before running this.',
    );
    exitCode = 64; // EX_USAGE
    return;
  }

  final razorpay = Razorpay(keyId: keyId, keySecret: keySecret);

  try {
    // Amounts are in the smallest currency unit: 50000 paise = ₹500.
    final order = await razorpay.orders.create(
      params: const RazorpayOrderCreateRequestBody(
        amount: 50000,
        receipt: 'receipt#1',
        notes: {'purpose': 'razorpay_dart example'},
      ),
    );
    stdout.writeln('Created ${order.id} for ${order.amount} ${order.currency}');

    final fetched = await razorpay.orders.fetch(orderId: order.id);
    stdout.writeln('Fetched ${fetched.id}, status ${fetched.status}');

    final recent = await razorpay.orders.all(
      params: const RazorpayOrderQuery(count: 3),
    );
    stdout.writeln('Most recent ${recent.count} order(s):');
    for (final each in recent.items) {
      stdout.writeln('  ${each.id}  ${each.amount}  ${each.status}');
    }
  } on RazorpayApiException catch (error) {
    // Razorpay rejected the request and said why.
    stderr.writeln(
      'API error ${error.statusCode} ${error.code}: '
      '${error.description}',
    );
    if (error.field != null) stderr.writeln('  field: ${error.field}');
    exitCode = 1;
  } on RazorpayNetworkException catch (error) {
    // The request never reached Razorpay, so it is safe to retry.
    stderr.writeln('Network error: ${error.message}');
    exitCode = 1;
  }

  demonstrateSignatureVerification(keySecret);
}

/// What a Checkout callback or webhook handler does with a signature.
///
/// Never trust a `razorpay_payment_id` that arrives without a valid signature:
/// anyone can post one to your endpoint.
void demonstrateSignatureVerification(String keySecret) {
  const params = {'order_id': 'order_ExampleId', 'payment_id': 'pay_ExampleId'};

  // Your server never generates this -- Razorpay sends it. Computed here only
  // so the example is self-contained and actually verifies.
  final signature = RazorpaySignature.paymentPayload(params);
  stdout.writeln('\nSigning payload: $signature');

  final valid = Razorpay.validatePaymentVerification(
    params: params,
    signature: 'a-signature-that-will-not-match',
    secret: keySecret,
  );
  stdout.writeln('Tampered signature accepted? $valid  (must be false)');
}
0
likes
160
points
95
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

A Dart port of the official razorpay-node SDK for the Razorpay API.

Repository (GitHub)
View/report issues

Topics

#razorpay #payment #gateway #finance

License

(pending) (license)

Dependencies

crypto, dio, freezed_annotation, json_annotation, pointycastle

More

Packages that depend on razorpay_dart