gpt4 book ai didi

flutter - 如何在 flutter 中从 Stripe 信用卡获取 token ID

转载 作者:行者123 更新时间:2023-12-04 13:15:03 26 4
gpt4 key购买 nike

我想从我的信用卡中获取 tokenID。我看到这个 Article正在使用 StripeSource.addSource但是最新的stripe包没有StripeSource.addSource .所以我喜欢这样:

StripePayment.paymentRequestWithCardForm(
CardFormPaymentRequest())
.catchError((e) {
print('ERROR ${e.toString()}');
}).then((PaymentMethod paymentMethod) async {
final CreditCard testCard = CreditCard(
number: paymentMethod.card.number,
expMonth: paymentMethod.card.expMonth,
expYear: paymentMethod.card.expYear
);

StripePayment.createTokenWithCard(testCard).then((token) {
print(token.tokenId);
});
});

但我收到错误 Unhandled Exception: PlatformException(invalidRequest, Missing required param: card[number]., null)
任何人都可以帮我解决这个问题吗?谢谢

最佳答案

我喜欢这样:

class _MyHomePageState extends State<MyHomePage> {
static String secret = 'sk_test_xxxxxxxxxxxxx';

TextEditingController cardNumberController = new TextEditingController();
TextEditingController expYearController = new TextEditingController();
TextEditingController expMonthController = new TextEditingController();

@override
initState() {
super.initState();

StripePayment.setOptions(
StripeOptions(publishableKey: "pk_testxxxxxx", merchantId: "Test", androidPayMode: 'test')
);
}

@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextField(
controller: cardNumberController,
decoration: InputDecoration(labelText: 'Card Number'),
),
TextField(
controller: expMonthController,
keyboardType: TextInputType.number,
decoration: InputDecoration(labelText: 'Expired Month'),
),
TextField(
controller: expYearController,
keyboardType: TextInputType.number,
decoration: InputDecoration(labelText: 'Expired Year'),
),
RaisedButton(
child: Text('card'),
onPressed: () {
final CreditCard testCard = CreditCard(
number: cardNumberController.text,
expMonth: int.parse(expMonthController.text),
expYear: int.parse(expYearController.text)
);

StripePayment.createTokenWithCard(testCard).then((token) {
print(token.tokenId);

createCharge(token.tokenId);
});
},)
],
),
),
);
}

static Future<Map<String, dynamic>> createCharge(String tokenId) async {
try {
Map<String, dynamic> body = {
'amount': '2000',
'currency': 'usd',
'source': tokenId,
'description': 'My first try'
};
var response = await http.post(
'https://api.stripe.com/v1/charges',
body: body,
headers: { 'Authorization': 'Bearer ${secret}','Content-Type': 'application/x-www-form-urlencoded'}
);
return jsonDecode(response.body);
} catch (err) {
print('err charging user: ${err.toString()}');
}
return null;
}
}

关于flutter - 如何在 flutter 中从 Stripe 信用卡获取 token ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61323492/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com