Ionic+Codeigniter4实现Stripe ACH一次性支付技术问询
Ionic + Codeigniter4 集成Stripe ACH支付问题解答
我正在尝试在结账流程中添加ACH支付:在Ionic前端自定义表单收集用户的路由号码、银行账号及账户持有人姓名,使用Stripe发布密钥通过this.stripe.createBankAccountToken(bstripeBankDetails)生成token后传递给Codeigniter4后端API。已启用Stripe测试账户的ACH Direct Debit,网页端用预构建页面实现,但Ionic中无法使用Secret Key,因此采用前端生成token传递给后端的方式。
现有代码
Ionic前端代码
this.stripe.setPublishableKey('pk_test_51P5kD0...'); const bstripeBankDetails = { "routing_number": '110000000', "account_number": '000123456789', "account_holder_name": 'John Smith', // optional "account_holder_type": 'individual', // optional "currency": 'USD', "country": 'US' } this.stripe.createBankAccountToken(bstripeBankDetails) .then(token => { console.log(token); console.log(token['bank_account']['id']); this.stripeError = ""; this.stripeBankToken = token['id']; this.stripeBankAccount = token['bank_account']['id']; console.log(this.stripeBankToken); console.log(this.stripeBankAccount); // 测试时传递数据到API let formData = { "banktoken": token['id'], "bankaccount": token['bank_account']['id'], } this.api.post("stripe_data.php", formData).subscribe((data: any) => { console.log("RESULT", data); }); }) .catch(error => { console.error("Stripe error 1 - ", error.message); console.error("Stripe error - ", error); this.stripeError = error; this.loaderService.hide(); this.loaderService.showToast(this.stripeError); return false; });
Codeigniter4后端API代码
// ACH payment if($payment_type == 'ach') { \Stripe\Stripe::setApiKey('sk_test_51P5k...'); $customer = \Stripe\Customer::create([ 'description' => 'Example customer', 'source' => $banktoken ]); try { $payment_intent = \Stripe\PaymentIntent::create([ 'amount' => 1000, // 金额单位:分 'currency' => 'usd', 'customer' => $customer->id, 'payment_method_types' => ['ach_debit'], 'payment_method' => $bankaccount, 'confirm' => true, ]); echo "bank_account<pre>"; print_r($payment_intent); echo "Payment Intent successful: " . $payment_intent->id; } catch (\Stripe\Exception\ApiErrorException $e) { $error = $e->getMessage(); $transaction_id = ''; array_push($myarray,array('stripemessage' => $error, "success"=>0 )); echo json_encode($myArray); exit; } catch (Exception $e) { $error = $e->getMessage(); $transaction_id = ''; array_push($myarray,array('message' => $error, "success"=>0 )); echo json_encode($myArray); exit; } } echo "transID::".$transactionID;exit;
问题解答
1. 实现ACH支付的正确步骤
- 前端:
- 收集用户银行账户核心信息(路由号、账号、账户持有人姓名等)
- 使用Stripe发布密钥调用
createBankAccountToken生成银行账户token - 将生成的token传递给后端API
- 后端:
- 使用Stripe秘钥初始化API客户端
- (推荐)创建Stripe Customer对象,将银行token关联到该Customer(或直接用token创建PaymentMethod)
- 创建
PaymentIntent,指定金额、货币、payment_method_types为ach_debit,关联对应的支付方式 - 配置Stripe Webhook,监听ACH支付的异步状态变化
- 向前端返回支付初始化结果,后续通过Webhook或主动查询告知用户最终支付状态
2. 是否应该使用PaymentIntent而非Charges?
是,必须使用PaymentIntent。Charges是Stripe的旧版API,已不再推荐用于新集成,尤其针对ACH这类需要异步处理、状态流转复杂的支付方式。PaymentIntent专门设计用于处理从发起支付到最终确认的全流程,能更好地支持ACH的延迟到账、失败重试等场景,符合Stripe当前的最佳实践。
3. 是否需要通过$verification = $bankaccount->verify(['amounts' => [32, 45]]);验证银行账户?
分场景判断:
- 测试环境:使用Stripe提供的测试路由号
110000000和测试账号000123456789时,无需验证即可发起支付 - 生产环境:首次添加的银行账户通常需要微存款验证(Stripe向用户账户打两笔小额金额,用户输入金额完成验证)。你代码中的验证方式属于旧版API调用,当前Stripe推荐使用
\Stripe\Customer::verifySource方法完成验证。是否强制验证取决于业务合规要求和Stripe风控规则。
4. 如何通过微存款或直接借记查询支付状态?
- 微存款验证状态:用户输入微存款金额后,调用Stripe API完成验证,可通过返回结果查看对应source/PaymentMethod的
verification字段;也可通过Webhook监听customer.source.verified事件获取验证完成通知 - 直接借记支付状态:
- Webhook监听:配置Stripe Webhook,监听
payment_intent.processing(支付处理中)、payment_intent.succeeded(支付成功)、payment_intent.payment_failed(支付失败)等事件,实时获取状态更新 - 主动查询:后端定时调用
PaymentIntent.retrieveAPI,通过status字段判断状态:processing表示正在处理(通常1-2个工作日),succeeded表示支付完成,failed表示支付失败
- Webhook监听:配置Stripe Webhook,监听
内容的提问来源于stack exchange,提问作者Agan
相关产品推荐
相关产品推荐

