关于Stripe费用计算,有没有什么办法可以根据提供的金额获得Stripe费用。
我们必须以这样的方式实施这一点:我们必须向一个经销商支付 x 金额,向另一经销商支付 y 金额。
第一个案例:
假设我们有 100 美元要支付给 Stripe。
根据我们的需求,我们要先计算 Stripe 费用,然后将该费用添加到 100 美元金额中。
例如:
需要支付的金额为 $100 + $3(条纹费)= $103(总计),您需要从客户帐户中扣除。
第二种情况:
我们需要向经销商支付 95 美元,剩下的 5 美元我们要保留在我们的账户中(不包括 Stripe 费用)。
如果这是可能的,我们如何实现?
最简单的方法就是为余额交易添加展开
$charge = \Stripe\Charge::create(array(
"amount" => $totalAmount,
"currency" => $currency_code,
"source" => $stripeToken,
"transfer_group" => $orderId,
"expand" => array("balance_transaction")
));
这将为您提供条纹收取的费用,然后您可以进行剩余的计算
适合寻找 JavaScript 代码来计算 Stripe 费用的人(也许是要求客户支付 Stripe 费用)。我写了一个小脚本来做到这一点
/**
* Calculate stripe fee from amount
* so you can charge stripe fee to customers
* lafif <[email protected]>
*/
var fees = {
USD: { Percent: 2.9, Fixed: 0.30 },
GBP: { Percent: 2.4, Fixed: 0.20 },
EUR: { Percent: 2.4, Fixed: 0.24 },
CAD: { Percent: 2.9, Fixed: 0.30 },
AUD: { Percent: 2.9, Fixed: 0.30 },
NOK: { Percent: 2.9, Fixed: 2 },
DKK: { Percent: 2.9, Fixed: 1.8 },
SEK: { Percent: 2.9, Fixed: 1.8 },
JPY: { Percent: 3.6, Fixed: 0 },
MXN: { Percent: 3.6, Fixed: 3 }
};
function calcFee(amount, currency) {
var _fee = fees[currency];
var amount = parseFloat(amount);
var total = (amount + parseFloat(_fee.Fixed)) / (1 - parseFloat(_fee.Percent) / 100);
var fee = total - amount;
return {
amount: amount,
fee: fee.toFixed(2),
total: total.toFixed(2)
};
}
var charge_data = calcFee(100, 'USD');
console.log('You should ask: ' + charge_data.total + ' to customer, to cover ' + charge_data.fee + ' fee from ' + charge_data.amount);
console.log(charge_data);
从Stripe Charge ID中我们可以得到从金额中扣除的处理费
stripe.Charge.retrieve("ch_1DBKfWECTOB5aCAKpzxm5VIW", expand=['balance_transaction'])
"id": "txn_1DBKfWECTOB5aCAKtwwLMCjd",
"net": 941,
"object": "balance_transaction",
"source": "ch_1DBKfWECTOB5aCAKpzxm5VIW",
"status": "pending",
"type": "charge"
"net": 941 is the amount credited to merchant account
目前,Stripe 的 API 无法在创建费用之前计算费用。你需要自己做这件事。
如果您想将费用转嫁给付费客户,以下支持文章将非常有帮助:https://support.stripe.com/questions/can-i-charge-my-stripe-fees-to-my -客户
要代表另一个帐户处理付款,并可选择从交易中删除,您需要使用 Stripe Connect。您可以在文档中阅读更多内容:https://stripe.com/docs/connect。
只是想加入 Harshal Lonare 的答案,为了发送付款意向确认,您可以通过以下方式取回余额交易数据:
"expand" => array("charges.data.balance_transaction")
使用管理帐户终于可以实现上述场景了。
用于计算Stripe Fee。
stripe_fixed_fee = 0.30; //分 条纹电荷= 0.029; //分
您可以参考这个链接 http://www.blackdog.ie/stripe/ https://support.stripe.com/questions/can-i-charge-my-stripe-fees-to-my-customers
谢谢!
您可以提前计算Stripe费用。 只需在这里查看他们最新的计算公式即可:https://stripe.com/us/pricing(请记住更改 URL 以匹配您所在的国家/地区,例如对于我(法国),URL 是 https:// stripe.com/fr/pricing)
所以,就我而言,它有点特别:
对于美国,Stripe 费用为 2.9% + 0.30 美元
注:百分比是占总金额的百分比。 示例:对于美国账户,如果我以 100 美元的价格出售产品,Stripe 费用将为:
(100 * 0.029) + 0.30 = 3.2 USD
然后您可以根据自己的需要随意分摊 Stripe 费用
您可以使用这样的函数来计算绝对支付金额(“需要支付”金额+条纹“税”):
const stripeFee = (amount) => {
if (amount <= 0) return 0;
const amountTax = amount / 100 * stripeProcessingFee;
const minFeeTax = stripeProcessingMinFee / 100 * stripeProcessingFee;
const tax = amountTax
+ (amountTax + minFeeTax) / 100 * stripeProcessingFee
+ minFeeTax
+ stripeProcessingMinFee;
return Math.ceil(amount + tax);
};
*stripeProcessingFee - Stripe 即用即付定价百分比 (2.9 %)
*stripeProcessingMinFee - Stripe 即用即付定价最低价值,以美分(30 美分)为单位
尽管大部分费用计算都是正确的,我仍然认为最顺利的方法是询问报告API而不是进行计算。我只用节点完成了它,没有使用 PHP,但这是我的代码:
require('dotenv').config()
const stripe = require('stripe')(process.env.STRIPE_SECRET)
const { DateTime } = require('luxon')
const fetch = require('node-fetch')
const { encode } = require('base-64')
const CSV = require('csv-string')
//Important Timestamps
const dt = DateTime.local().setZone('America/Los_Angeles')
const endOfLastMonth = dt.startOf('month').toSeconds()
const startOfLastMonthLA = dt.minus({ month : 1 }).startOf('month').toSeconds()
const params = {
created : {
gt : startOfLastMonthLA, lt : endOfLastMonth
}
}
const gather = async () => {
const reportRun = await stripe.reporting.reportRuns.create({
report_type : 'balance_change_from_activity.summary.1', parameters : {
interval_start : startOfLastMonthLA, interval_end : endOfLastMonth
}
})
let reportTest
console.time('generateReport')
while ( true ) {
console.log('start')
await new Promise(resolve => {
setTimeout(resolve, 2000)
})
reportTest = await stripe.reporting.reportRuns.retrieve(reportRun.id)
if (reportTest.status === 'succeeded') {
console.log(reportTest.id)
break
}
}
console.timeEnd('generateReport')
const actualReport = await fetch(reportTest.result.url, {
headers : {
'Authorization' : 'Basic ' + encode(process.env.STRIPE_SECRET + ':')
}
})
const data = await actualReport.text()
//This is the net profit!
console.log(CSV.parse(data)[4][5])
}
gather().catch(e => console.log(e))
信息全部在数据中,我建议查看数据字符串。它基本上是当您单击仪表板中的报告时获得的报告,它们有不同的报告类型。从语义上讲,通过报告 api 获取报告更为正确,然后与更适合处理/检查单项费用的 api 相比。我更喜欢 stripe 直接以 JSON 的形式向我发送该信息,但 csv 也可以。
在 NodeJs 中使用支付意图 id 来获取条带所收取的处理费
const paymentIntent = await this.stripe.paymentIntents.retrieve(id, {
expand: ['charges.data.balance_transaction'],
});
//Stripe fee
const stripe_fee = paymentIntent.charges.data[0].balance_transaction.fee;
它将在费用对象内的 paymentIntent 中给出以下响应(balance_transaction的fee_details)
"balance_transaction": {
"id": "txn_3JQ9ddddgRF81Q43rnFI1Zn2US9u",
"object": "balance_transaction",
"exchange_rate": null,
"fee": 76,
"fee_details": [
{
"amount": 76,
"application": null,
"currency": "usd",
"description": "Stripe processing fees",
"type": "stripe_fee"
}
],
},
可以使用付款意向 ID 检索 Stripe 处理费。
\Stripe\Stripe::setApiKey('{{secret}}');
$paymentIntent = \Stripe\PaymentIntent::retrieve([
'id' => '{{payementIntentid}}',
'expand' => ['charges.data.balance_transaction'],
]);
$feeDetails = $paymentIntent->charges->data[0]->balance_transaction->fee_details;
这些答案没有考虑到费用是按总金额收取的。因此,如果您的账单为 100 美元 + (100 美元 * 2.9% + 0.30 美元) = 103.20 美元,Stripe 现在将按 103.20 美元向您收取费用,而不是原来的 100 美元。
def calculate(desired_total, percent_fee, fixed_fee)
return 0 if desired_total <= 0
# X = what to charge
# Y = what we want
#
# The basic formula is:
# X - (X * 0.029) - 0.30 = Y
#
# First... we want our percentage (0.029) to be a whole number so we will
# multiply each variable by 1000
multiplier = 1000
y = desired_total * multiplier
percent_component = (percent_fee) * multiplier # => 29
base_component = fixed_fee * multiplier # => 300
coefficient = multiplier - percent_component # => 971
# This gives us
# 1000X - 29X - 300 = Y
#
# Which we then re-arrange the terms to
# 971X = Y + 300
#
# And can then calculate the total we need to charge as
# X = (Y + 300) / 971
total_charge = (y + base_component) / coefficient
end
p calculate(100, 0.029, 0.30) => # 103.29557157569516
p calculate(200, 0.029, 0.30) => # 206.2821833161689