I am trying to do the following from my flutter web application.
While doing this, I found, after payment success in paypal, it redirect to a give return_url, and i could not find a way how to call my register user api with the provided email and password. Maybe I am missing something silly somewhere. If anybody can help me out please.
// 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;
}
After the payment completed successfully, paypal redirect to "$baseUrl/payment_success" this url. If i could get the email and password in this screen, i could execute register function. but i know only one way to pass email and password to this screen is to use "parameters" in the url itself. but people says, it is not secure to pass email and password through "url parameters"
//I have uploaded the complete code in Github https://github.com/smile675/paypal_test/
Thanks a lot everybody.
The account should be created before or after the PayPal subscription is paid for. Creating before payment is simpler to implement.
When the PayPal subscription is paid for, activate the account. Do not depend solely on return functionality for this to happen, as there are scenarios where a return may never happen (which is party of why creating an account before the subscription is paid is simplest to implement). Register a webhook listener URL for the event PAYMENT.SALE.COMPLETED
, use this to both activate an account to start and to refresh its validity after every new payment cycle.
If useful for reconciliation, include a custom_id
parameter with some value when creating the subscription. This value will be returned in all payment events for the subscription.