当前位置: 首页 > 知识库问答 >
问题:

为什么Razorpay支付成功处理程序方法不调用我的函数?

雷方伟
2023-03-14

我正在尝试获取付款的详细信息,并更新数据库订阅表中的详细信息,我使用Razorpay,Razorpay提供了处理发生的不同事件的方法。我想将发布请求发送到rest api,不幸的是,Razorpay的支付成功事件处理程序方法没有调用我的_subscription()函数。

这是我的付款代码。

import 'dart:convert';
import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_spinkit/flutter_spinkit.dart';
import 'package:razorpay_flutter/razorpay_flutter.dart';
import 'package:tneos_eduloution/network_utils/api.dart';
import 'package:flutter/services.dart';
import 'package:fluttertoast/fluttertoast.dart';

class Payment extends StatefulWidget {
  final int amount;
  final int categoryId;
  final int userId;
  final String name;
  final String image;
  final int packageClass;
  final String subject;
  final String board;
  Payment(this.amount, this.categoryId, this.userId, this.name, this.image, this.packageClass, this.subject, this.board);
  @override
  _PaymentState createState() => _PaymentState(this.amount, this.categoryId, this.categoryId, this.name, this.image, this.packageClass, this.subject, this.board);
}

class _PaymentState extends State<Payment> {
  static const platform = const MethodChannel("razorpay_flutter");
  int amount;
  int categoryId;
  int userId;
  String name;
  String image;
  int packageClass;
  String subject;
  String board;

  Razorpay _razorpay;
  // var orderId;
  // var razorpayId;

  _PaymentState(this.amount, this.categoryId, this.userId, this.name,
      this.image, this.packageClass, this.subject, this.board);


  @override
  Widget build(BuildContext context) {
    return Scaffold(
     // Code ....
        body: Container(
         // Code ....
              Expanded(
                flex: 2,
                child: Padding(
                  padding: const EdgeInsets.all(24.0),
                  child: RaisedButton(
                      elevation: 10,
                      shape: RoundedRectangleBorder(
                        borderRadius: BorderRadius.circular(18),
                      ),
                      color: ArgonColors.success,
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.center,
                        children: [
                          Icon(
                            Icons.add_shopping_cart,
                            color: ArgonColors.bgColorScreen,
                            size: 32,
                          ),
                          Text('BUY NOW', style: TextStyle(
                            color: ArgonColors.white,
                            fontSize: 32,
                            fontWeight: FontWeight.w600,
                            letterSpacing: 2,

                          ))
                        ],
                      ),
                      onPressed: () {
                        openCheckout();
                      }),
                ),
   // Code....
  }

  @override
  void initState() {
    super.initState();
    _razorpay = Razorpay();
    _razorpay.on(Razorpay.EVENT_PAYMENT_SUCCESS, _handlePaymentSuccess);
    _razorpay.on(Razorpay.EVENT_PAYMENT_ERROR, _handlePaymentError);
    _razorpay.on(Razorpay.EVENT_EXTERNAL_WALLET, _handleExternalWallet);
  }

  @override
  void dispose() {
    super.dispose();
    _razorpay.clear();
  }


  void openCheckout() async {
    var options = {
      'key': 'rzp_test_YbFME7OVSi2Kvx',
      'amount': amount * 100,
      'name': name,
      'image': 'https://tneos.in/app-assets/img/core-img/favicon.ico',
      'description': 'Buy the Course to get access for all live classes',
      'prefill': {'contact': '', 'email': ''},
      'external': {
        'wallets': ['paytm']
      }
    };
    try {
      _razorpay.open(options);
    } catch (e) {
      debugPrint(e);
    }
  }

  Future<void> _handlePaymentSuccess(PaymentSuccessResponse response) async {
    // orderId = 12123;
    // razorpayId = response.paymentId;
    _subscription();
    Fluttertoast.showToast(
        msg: "SUCCESS: " + response.paymentId, timeInSecForIosWeb: 4);
  }

  void _handlePaymentError(PaymentFailureResponse response) {
    Fluttertoast.showToast(
        msg: "ERROR: " + response.code.toString() + " - " + response.message,
        timeInSecForIosWeb: 4);
  }

  void _handleExternalWallet(ExternalWalletResponse response) {
    Fluttertoast.showToast(
        msg: "EXTERNAL_WALLET: " + response.walletName, timeInSecForIosWeb: 4);
  }

void _subscription() async {
  var data = {
    'name': name,
    'amount': amount,
    // 'order_id': orderId,
    // 'razorpay_id': razorpayId,
    'category_id': categoryId,
    'user_id': userId};
  var res = await Network().authData(data, '/subscription');
  var body = json.decode(res.body);
  if (body['success']) {
    print('success');
  } else {
    print('failure');
  }
}
}

AuthData来自网络类

import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:shared_preferences/shared_preferences.dart';

class Network {
  final String _url = 'http://10.0.2.2:8000/api/v1';

  //if you are using android studio emulator, change localhost to 10.0.2.2
  var token;

  _getToken() async {
    SharedPreferences localStorage = await SharedPreferences.getInstance();
    token = jsonDecode(localStorage.getString('token'))['token'];
  }

  authData(data, apiUrl) async {
    var fullUrl = _url + apiUrl;
    return await http.post(fullUrl,
        body: jsonEncode(data), headers: _setHeaders());
  }

  getData(apiUrl) async {
    var fullUrl = _url + apiUrl;
    await _getToken();
    return await http.get(fullUrl, headers: _setHeaders());
  }

  _setHeaders() => {
        'Content-type': 'application/json',
        'Accept': 'application/json',
        'Authorization': 'Bearer $token',
        'connection': 'keep-alive',
      };
}

Laravel方法API函数正在运行。

public function subscribed(Request $request) {
     
    $subscription = new Subscription;
    $subscription->title = $request->name;
    $subscription->amount = $request->amount;
    // $subscription->payment_id = $request->order_id;
    // $subscription->razorpay_id = $request->razorpay_id;
    $subscription->payment_done = 1;
    $subscription->category_id = $request->category_id;
    $subscription->user_id = $request->user_id;

    $subscription->save();

    return response()->json($subscription);

邮递员回复工作。邮递员API响应

共有1个答案

贝阳泽
2023-03-14

这是我的错误,实际上我做得不好,我在网络课上首先创建了新方法postData

  postData(data, apiUrl) async {
    var fullUrl = _url + apiUrl;
    await _getToken();
    return await http.post(fullUrl, body:jsonEncode(data), headers: _setHeaders());
  }

然后我更新了Razorpay的成功事件处理程序方法。


void _handlePaymentSuccess(PaymentSuccessResponse response)  async {
    // orderId = 12123;
    // razorpayId = response.paymentId;
    var data = {
      'name': name,
      'amount': amount,
      // 'order_id': orderId,
      // 'razorpay_id': razorpayId,
      'category_id': categoryId,
      'user_id': userId};
    var res = await Network().postData(data, '/subscription');
    var body = json.decode(res.body);
    if (body['success']) {
       Fluttertoast.showToast(
           msg: "SUCCESS: " + response.paymentId, timeInSecForIosWeb: 4);
    } else {
      Fluttertoast.showToast(
          msg: "Kindly Contact US: " + response.paymentId, timeInSecForIosWeb: 4);
    }
    // _subscription();

  }

 类似资料:
  • 本文向大家介绍小程序调用微信支付的方法,包括了小程序调用微信支付的方法的使用技巧和注意事项,需要的朋友参考一下 本文实例为大家分享了小程序调用微信支付的具体代码,供大家参考,具体内容如下 首先调用小程序wx.login登录接口  获取用户code 将code作为参数  传给后端  调用后端接口wechat/pay/prepay 获取后端  这五个返回值 nonceStr  package2 pay

  • 本文向大家介绍php对微信支付回调处理的方法,包括了php对微信支付回调处理的方法的使用技巧和注意事项,需要的朋友参考一下 应用场景 支付完成后,微信会把相关支付结果和用户信息发送给商户,商户需要接收处理,并返回应答。 对后台通知交互时,如果微信收到商户的应答不是成功或超时,微信认为通知失败,微信会通过一定的策略定期重新发起通知,尽可能提高通知的成功率,但微信不保证通知最终能成功。 (通知频率为1

  • 在我的项目中有一个ViewController,它包含一些子视图(例如按钮)。它显示/隐藏这些按钮,总是带有动画。 它有一个这样的界面: 实现如下所示: 当被调用时,然后经过一段时间( 我尝试将“隐藏”持续时间更改为0.1,但没有帮助-在“隐藏(0.1)”“显示(0.2)”调用之后,“隐藏”回调在“显示”回调之后调用,我的按钮不可见。 我添加了一个快速修复方法,缓存visible参数,如果状态应该

  • 我正在为Spring MVC中的异常处理开发示例演示应用程序。我正在尝试使用@ControllerAdvice处理异常 我遵循了本链接中描述的步骤。 但是当我运行我的应用程序时,我得到了以下错误 以下是我正在学习的课程 应用程序异常。JAVA 控制器类 ApplicationExceptionController。JAVA 下面是GlobalExceptionHandler类 GlobalExce

  • 我在Android应用程序中使用Razorpay进行支付。 目前,支付工作正常。 现在我需要使用Razorpay实现经常性支付(自动续订/订阅)。 我找不到任何干净的文件。请随时为我更新有价值的信息。

  • 问题内容: 在React教程中,它说 这样做会立即发出警报,而不是在单击按钮时发出警报。 但是,我不明白为什么会这样…任何人都可以为我澄清一下吗?为什么不能将函数调用作为处理程序传递? 问题答案: 当您执行此操作时,将调用该函数并将返回的值()分配给该属性。所以,React看到的是这样说的: 嗯,那不是一个函数,为什么我要添加这样的处理程序? 您要传递的是一个函数,而不是。 因此,您必须做:可以像