Flutter web Paypal 付款并在付款成功后注册用户

问题描述 投票:0回答:1

我正在尝试从我的 flutter Web 应用程序执行以下操作。

  1. 用户填写注册表并提供电子邮件和密码。
  2. 然后用户单击“订阅”按钮。
  3. 这将通过 PayPal 启动付款流程。
  4. 支付成功后,我需要调用我的后端API,以便将用户注册到我的数据库中。
  5. 如果付款不成功,我不想注册用户。

在执行此操作时,我发现,在 paypal 付款成功后,它会重定向到给定的 return_url,并且我找不到如何使用提供的电子邮件和密码调用我的注册用户 api 的方法。 也许我在某个地方错过了一些愚蠢的东西。如果有人可以帮助我,请。

// User fill up and click the button here:
 
    class _MyHomePageState extends State<MyHomePage> {
          final PaypalService _service = PaypalService();
          final TextEditingController email = TextEditingController();
          final TextEditingController password = TextEditingController();
       
      subscribe() {
        _service.paypalPayment();
      }
    
      registerUser() {
        print(email.text);
        print(password.text);
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Column(
            children: [
              TextField(
                controller: email,
                decoration: const InputDecoration(
                  hintText: "Email",
                ),
              ),
              const SizedBox(
                height: 10,
              ),
              TextField(
                controller: password,
                decoration: const InputDecoration(
                  hintText: "Password",
                ),
              ),
              TextButton(
                onPressed: () {
                  subscribe();
                },
                child: const Text("Subscribe"),
              ),
            ],
          ),
        );
      }
    }


//paypal payment happening here
import 'package:papalgateway/secret.dart';

import 'models.dart';
import 'payment_api.dart';
// ignore: avoid_web_libraries_in_flutter
import 'dart:html' as html;

class PaypalService {
  Future<void> paypalPayment() async {
    PaypalSecret secret = PaypalSecret(
      clientId: Secret.clientIdSandbox,
      clientSecret: Secret.clientSecretSandbox,
      paymentMode: PaymentMode.sandbox,
    );

    PaypalPayment paymentService = PaypalPayment(paypalSecret: secret);

    Token token = await paymentService.getAccessToken(secret);

    if (token.token == null) {
      return;
    }
    Payment payment = await paymentService.createPayment(
      transactionSubscription(),
      token.token!,
    );
    if (payment.approvalUrl == null) {
      return;
    }
    html.window.open(payment.approvalUrl!, "Payment");
  }
}


// this is the "transactionSubscription()" for body in paypal api call
transactionSubscription() {
  String baseUrl = html.window.location.origin;
  Map<String, dynamic> transactions = {
    "intent": "CAPTURE",
    "purchase_units": [
      {
        // "reference_id": "d9f80740-38f0-11e8-b467-0ed5f89f718b",
        "amount": {"currency_code": "MYR", "value": "10.00"}
      }
    ],
    "payment_source": {
      "paypal": {
        "experience_context": {
          "payment_method_preference": "IMMEDIATE_PAYMENT_REQUIRED",
          "brand_name": "EXAMPLE INC",
          "locale": "en-US",
          "landing_page": "LOGIN",
          // "shipping_preference": "SET_PROVIDED_ADDRESS",
          "user_action": "PAY_NOW",
          "return_url": "$baseUrl/payment_success",
          //"https://example.com/successUrl", //https://example.com/cancelUrl
          "cancel_url":
              "$baseUrl/payment_failed", //"https://example.com/cancelUrl"
        }
      }
    }
  };

  return transactions;
}

支付成功后,paypal会重定向到“$baseUrl/ payment_success”这个url。 如果我可以在此屏幕中获取电子邮件和密码,我就可以执行注册功能。但我知道将电子邮件和密码传递到此屏幕的唯一方法是使用网址本身中的“参数”。但人们说,通过“url 参数”传递电子邮件和密码并不安全

//完整代码我已经上传到Github了 https://github.com/smile675/paypal_test/

非常感谢大家。

flutter paypal paypal-subscriptions
1个回答
0
投票

帐户应在 PayPal 订阅付款之前或之后创建。之前这样做更容易实现。

支付 PayPal 订阅费用后,激活帐户。不要依赖返回功能来实现此目的,请为事件注册一个 Webhook 侦听器 URL

PAYMENT.SALE.COMPLETED

如果对协调有用,请在创建订阅时包含具有某些值的

custom_id
参数。该值将在订阅的所有付款事件中返回。

© www.soinside.com 2019 - 2024. All rights reserved.