Flutter Web PhonePe 支付网关集成错误 [已关闭]

Flutter Web PhonePe Payment Gateway Integration Error [closed]

提问人:Mani Tech 提问时间:11/15/2023 最后编辑:Mark RotteveelMani Tech 更新时间:11/15/2023 访问量:54

问:


编辑问题以包括所需的行为、特定问题或错误以及重现问题所需的最短代码。这将有助于其他人回答这个问题。

9天前关闭。

这篇文章是 9 天前编辑并提交审核的。

我使用 Flutter web 和 Firebase 开发了一个电子商务网站,并部署在 Hostinger 中。集成支付网关后,它显示 CORS 错误。

我的整个 Flutter 网页代码:

import 'dart:convert';
import 'package:crypto/crypto.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:shelf/shelf.dart' as shelf;
import 'package:url_launcher/url_launcher.dart';

void main() {
shelf.Response 
responseWithCorsHeaders(shelf.Response 
response) {
return response.change(headers: {
  'Access-Control-Allow-Origin': '*',
  'Access-Control-Allow-Methods': 'GET, POST, 
OPTIONS',
  'Access-Control-Allow-Headers': 'Origin, Content- 
 Type, Accept, X-Verify',
});
}

runApp(const MaterialApp(
home: MyApp(),
));
}


class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);

@override
State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
 @override
 Widget build(BuildContext context) {
 return MaterialApp(
  home: Scaffold(
    body: Center(
      child: ElevatedButton(
        onPressed: () async {
          await transaction();
          await initiatePhonePeTransaction();
        },
        child: const Text("Pay with PhonePe"),
      ),
    ),
  ),
  );
 }

 var mTransactionId = 
 DateTime.now().millisecondsSinceEpoch.toString();

 var 
  mUserId=
  DateTime.now().microsecondsSinceEpoch.toString()
 .trimRight();

 String get jsonString {
return '''{
  "merchantId": "MY_VALID_MERCHANTID",
  "merchantTransactionId": "SETU$mTransactionId",
  "merchantUserId": "MUID$mUserId",
  "amount": 100,
  "redirectUrl": "https://kalpasetu.com",
  "redirectMode": "REDIRECT",
  "callbackUrl": "https://kalpasetu.com",
  "mobileNumber": "MY_VALID_MOBILENUMBER",
  "paymentInstrument": {
    "type": "PAY_PAGE"
  }
}''';
}

 var sha256St = "";

 var base64String = "";

 transaction() async {
 base64String = encodeJsonToBase64(jsonString);

 String data = 
 "$base64String/pg/v1/
 MY_VALID_SALTKEY_GIVEN_by_phonepe";

final sha256String = 
sha256.convert(utf8.encode(data));
sha256St = "$sha256String###1";
 }

 String encodeJsonToBase64(String jsonString) {
final bytes = utf8.encode(jsonString);
final base64String = base64.encode(bytes);
return base64String;
}

Future<void> initiatePhonePeTransaction() async {
final url = 

Uri.parse("https://api.phonepe.com/apis/
hermes/pg/v1/pay");

final headers = {
  'Content-Type': 'application/json',
  'X-VERIFY': sha256St,
  'accept': 'application/json',
};
final body = jsonEncode({"request": base64String});
try {
  final response = await http.post(
    url,
    headers: headers,
    body: body,
  );

  print('Response status: ${response.statusCode}');
  print('Response body: ${response.body}');

  if (response.statusCode == 200) {
    final redirectUrl = json.decode(response.body) 
  ['data']
        ['instrumentResponse']['redirectInfo'] 
['url'];
    final Uri url = Uri.parse(redirectUrl);
    // Launch the URL using url_launcher
    await launchUrl(url);
    setState(() {
      mTransactionId = 
 DateTime.now().millisecondsSinceEpoch.toString();
      mUserId =
          
DateTime.now().microsecondsSinceEpoch.toString()
.trimRight();
    });
  } else {
    print('Transaction failed. Status: 
${response.statusCode}');
  }
} catch (error) {
  print('Error: $error');
 }
}
}

在此之后,我在 CMD 中运行:flutter run -d chrome --web-renderer html

在localhost中,单击(使用PhonePe付款)按钮后,它将启动付款,因为我已禁用web_security。

在 CMD: 中运行并部署到 Hostinger 后,当我们单击(使用 PhonePe 付款)按钮时,它会显示flutter build web --release --web-renderer html

访问 XMLHttpRequest,网址为 “https://api.phonepe.com/apis/hermes/pg/v1/pay”来自原产地 “mywebsite.com”已被 CORS 策略阻止:请求 mywebsite.com/:1 标头字段 x-verify 不允许 预检响应中的 Access-Control-Allow-Headers

flutter cors xmlhttprequest phonepe

评论


答: 暂无答案