【问题标题】:Charging card multiple times充值卡多次
【发布时间】:2019-04-10 10:17:42
【问题描述】:

您好,我在我的应用程序中集成了 Stripe 以出售一些付费注册。在我的条纹仪表板中,我已经创建了一些 优惠券 一些有折扣和一些固定价值。我需要以多种货币向我的客户收费。 我在向客户收费时遇到问题。 假设我有一个每年 348 美元的计划并提供

  1. 客户 C1 使用优惠券 CP1 可享受 347 美元的优惠(因此费用仅为 1 美元)
  2. 客户 C2 一张优惠券 CP2 可享受 50% 的折扣(因此费用为 174 美元)

如果情况 2 没有问题并创建发票。 但在情况 1 中,它会收取 2 次费用:

  • 1 美元首发(折扣价)
  • 总金额 348 美元(不知道为什么要收费)

下面是我的代码:


计划价值已更新(已应用的优惠券有“一定金额的折扣)

$cc = $_POST['coupon'];
$actual_amount = $_POST['actual_amount'];
$cpnObj = \Stripe\Coupon::retrieve($cc);
$cpnDtl = $cpnObj->getLastResponse()->json;
if($cpnDtl['id']){
	$amount_off = $cpnDtl['amount_off'];
	$percent_off = $cpnDtl['percent_off'];
	
	if ($amount_off) {
		$discount = $amount_off/100;
		$paymoney = $actual_amount - $discount; 
	}

	if ($percent_off) {
		$discount = $actual_amount * $percent_off/100;
		$paymoney = $actual_amount - $discount;                
	}
}

稍后通过一些会话变量,我使用这个折扣金额向客户的卡收取费用

$token = $_POST['stripeToken'];  
$plan_id = $_POST['plan_id'];
		
//Creating Customer
$customer = \Stripe\Customer::create([
	"description" => "creating the customer",
	"email" => "myemail@somedomain.com",
	"source" => $token // obtained with Stripe.js
]);

//If Merchant account is created at Stripe end, Charge his Card
if($customer->id != ''){
	$chargeCard = \Stripe\Charge::create([
		"amount" => $paymoney (calculated above after applying the promo-code),
		"currency" => 'USD',
		"description" => "subscription for a year",
		"capture" => true,
		"customer" => $customer->id,
		"receipt_email" => "myemail@somedomain.com",
		"statement_descriptor" => "card is charged for so and so ... "
	]);
}

//If Card is charged successfully, create the Merchant's Subscription at Stripe end
if($chargeCard->id != ''){
	$subscription = \Stripe\Subscription::create(array(
		"customer" => $customer->id,
		"items" => [
						[
							"plan" => $plan_id,
						],
				],
		"billing" => "charge_automatically",
		"cancel_at_period_end" => false
	));
}

我在这里阅读了https://stripe.com/docs/api/ 的文档,在创建费用或客户时,“来源”是可选参数。这是“Source”的问题吗?同样在文档“creating a customer”中说

如果客户还没有来源,您必须提供来源 附加的有效来源,并且您正在订阅客户 对非免费计划自动收费。

那么我可以在创建客户和创建收费对象时使用相同的“Source”吗?

在这方面你能帮帮我吗?

提前致谢。

【问题讨论】:

    标签: stripe-payments


    【解决方案1】:

    我认为是您那里的费用和订阅代码的组合导致了双重费用。您首先发出一次性费用。成功并查看代码,然后立即开始订阅付款。您为什么要从卡中收费并创建订阅,是否有特定的原因?

    因为我认为您根本不需要创建第一个费用,因此您可以使用coupons directly on the subscription creation

    $subscription = \Stripe\Subscription::create(array(
        "customer" => $customer->id,
        "coupon" => $cc,
        "items" => [
                        [
                            "plan" => $plan_id,
                        ],
                ],
        "billing" => "charge_automatically",
        "cancel_at_period_end" => false
    ));
    

    所以你一开始不需要那个计算位。

    关于来源(存储卡、银行帐户等)的问题:当您使用客户对象时,它会自动使用它的默认来源。除非他们存储了更多卡片并且他们选择了要使用的一张,否则您不需要指定它。

    【讨论】:

    • 嗨,伙计,感谢您的回复,并对我迟到的反应感到抱歉。如果是因为“收费和订阅的组合”而出现这种情况,那么我不确定为什么在 50% 折扣的情况下它可以正常工作,在这种情况下也应该存在问题。您的方法很好地结合了这两件事(订阅和充值卡),但在这里,在这种情况下,我想我将无法获得 Charge_id(以 ch_ 开头)作为响应。我只能获取订阅 ID(以 sub_ 开头)。
    • 嗨@user2811 是正确的,你只得到子ID。但是,如果您有 webhook 端点设置,如果您侦听 charge.succeeded 事件类型,则可以 get the charge details from the event.data
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-06
    • 1970-01-01
    • 2014-05-13
    • 1970-01-01
    • 2015-11-23
    • 2022-07-29
    • 1970-01-01
    相关资源
    最近更新 更多