所以基本上我尝试做的是让客户添加信用卡以供以后使用,我在flutter上所做的是:
StripePayment.setOptions(StripeOptions(
publishableKey: "MY_KEY_HERE"));并添加了一个按钮:
onPressed: () async {
dynamic value =
await StripePayment.paymentRequestWithCardForm(
CardFormPaymentRequest(),
).catchError(setError).then(
(PaymentMethod paymentMethod) async {
try {
addCreditCard(paymentMethod); // post request to laravel cashier api with the payment method
print("Payment Method ID: ${paymentMethod.id}");
} catch (e) {
print(e);
}
},
);
},因此,我在这里所做的是,用户能够按下一个按钮,弹出一个输入表单,其中包括条带验证算法所需的所有卡详细信息,一旦输入了所有内容,我将获得一个返回的PaymentMethod对象(它包含一个令牌和最后4位数字等)。之后,我将这个PaymentMethod对象发送到laravel cashier api,其中包含以下函数:
public function create_card(Request $request)
{
$user = $request->user();
$user->addPaymentMethod($request->method); //Laravel billable cashier function
return response()->json([
'message' => 'Successfully updated credit card'
], 201);
}任何帮助都是最好的。
拉威尔的收银员计费addPaymentMethod函数:
public function addPaymentMethod($paymentMethod)
{
$this->assertCustomerExists();
$stripePaymentMethod = $this->resolveStripePaymentMethod($paymentMethod);
if ($stripePaymentMethod->customer !== $this->stripe_id) {
$stripePaymentMethod = $stripePaymentMethod->attach(
['customer' => $this->stripe_id], $this->stripeOptions()
);
}
return new PaymentMethod($this, $stripePaymentMethod);
}发布于 2019-12-08 19:37:49
尝试删除"async“并打印paymentMethod,以检查您是否获得了付款方式id:
StripePayment
.paymentRequestWithCardForm(CardFormPaymentRequest())
.then((paymentMethod) {
print(paymentMethod);
}).catchError((error)=>{print(error)});发布于 2021-04-20 06:59:15
对于任何还在为这个问题而苦苦挣扎的人,我终于找到了一个添加支付方法的可行解决方案。
我的设置是:
Laravel Framework 8.37.0
"laravel/cashier": "^12.12",
"laravel/sanctum": "^2.9",
Flutter (Channel stable, 2.0.5)
Dart 2.12.3
stripe_payment: ^1.0.11颤动侧:
import 'package:my_sample_package/services/payment_services.dart';
import 'package:flutter/material.dart';
import 'package:stripe_payment/stripe_payment.dart';
class Subscribe extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Scaffold(
body: SafeArea(
child: Center(
child: ElevatedButton(
child: Text("Add new Payment Method"),
onPressed: () {
StripeService.addNewPayment();
},
)
)
),
);
}
}payment_services.dart是处理Stripe和Api调用的文件:
import 'dart:convert';
import 'dart:developer';
import 'package:dio/dio.dart';
import 'package:flutter/services.dart';
import 'package:stripe_payment/stripe_payment.dart';
class StripeService {
Dio dio = new Dio();
static Future<dynamic> addNewPayment() async {
StripePayment.setOptions(
StripeOptions(
publishableKey: "your_stripe_publishable_key",
merchantId: "Test",
androidPayMode: 'test'
)
);
Dio dio = new Dio(); // I'm using Dio instead of the http package
String baseUrl = 'http://192.168.1.122:8000/api/'; // Your host, I started mine using ## php artisan serve --host 192.168.1.122 --port 8000 ##
String token = 'my_laravel_access_token'; // This is the token I get from Laravel Sanctum
await StripePayment.paymentRequestWithCardForm(
CardFormPaymentRequest(), // This will show the Native Payment Card form
)
.then(
(PaymentMethod paymentMethod) async {
try {
inspect(paymentMethod);
final response = await dio.post(
baseUrl + "user/subscription/payment-method/update", // My Laravel endpoint for updating the payment method
options: Options(
headers: { 'Authorization': 'Bearer $token' },
),
data: {
"payment_method": paymentMethod
}
);
inspect(response);
} catch (e) {
inspect(e);
}
},
).onError((error, stackTrace) {
inspect(error);
});
}
}Laravel Side:
routes/api.php文件来注册我们需要的路由:
...
Route::group(['prefix' => 'user', 'middleware' => ['auth:sanctum']], function () {
Route::post('subscription/payment-method/update', [UserController::class, 'updatePaymentMethod']);
});
...Controllers/UserController.php文件来注册我们的方法:
public function updatePaymentMethod(Request $request): \Illuminate\Http\JsonResponse
{
$user = auth()->user();
$user->updateDefaultPaymentMethod($request->payment_method['id']); // You need to pass just the payment method ID, not the whole object
return response()->json([
'message' => 'Payment method saved'
], 200);
}https://stackoverflow.com/questions/59212402
复制相似问题