【发布时间】:2021-01-29 10:50:21
【问题描述】:
我需要传入一系列行项目以使用 Stripe 生成发票。最终结果应如下所示:注意 line_items 部分。
$checkout_session = \Stripe\Checkout\Session::create([
'payment_method_types' => ['card'],
'line_items' => [['quantity' => 1,
'price_data' => ['currency' => 'CAD',
'unit_amount' => 750,
'product_data' => ['name' => 'Name goes here',
'description' => 'Description goes here']]],
['quantity' => 1,
'price_data' => ['currency' => 'CAD',
'unit_amount' => 450,
'product_data' => ['name' => 'Name goes here',
'description' => 'Description goes here']]]
],
'mode' => 'payment',
'success_url' => $YOUR_DOMAIN.'/success.htm',
'cancel_url' => $YOUR_DOMAIN.'/cancel.htm',
]);
在此示例中,有两个订单项,但可以有任意数量的项目。问题当然是我需要在传递它之前生成这个订单项数组,并且这些订单项是从数据库中出来的。
因此,理想情况下,我可以在变量中生成 line_items 数组,然后直接传入该变量;像这样:
$checkout_session = \Stripe\Checkout\Session::create([
'payment_method_types' => ['card'],
'line_items' => [$lineitems],
'mode' => 'payment',
'success_url' => $YOUR_DOMAIN.'/success.htm',
'cancel_url' => $YOUR_DOMAIN.'/cancel.htm',
]);
然而,到目前为止,还没有运气。我可以像这样在循环中生成一个字符串:
foreach($rows as $row){
$linedescription = $row["itemname"];
$lineamount = $row["amount"];
$linecomment = $row["comment"];
$lineamount = $lineamount * 100;
if($index > 0){
$lineitems .= ",";
}
$lineitems = "['quantity' => 1,'price_data' => ['currency' => '".$currencycode."','unit_amount' => ".$lineamount.", 'product_data' => ['name' => '".$linecomment."','description' => '".$linedescription."']]]";
$index ++;
}
这正好给了我这个:
"[['quantity' => 1,
'price_data' => ['currency' => 'CAD',
'unit_amount' => 750,
'product_data' => ['name' => 'Name goes here',
'description' => 'Description goes here']]],
['quantity' => 1,
'price_data' => ['currency' => 'CAD',
'unit_amount' => 450,
'product_data' => ['name' => 'Name goes here',
'description' => 'Description goes here']]]
]"
问题是,它是一个字符串,我需要它是一个数组。我已经尝试过 json_decode 并且我已经尝试在循环中生成一个数组而不是一个字符串,但我似乎无法得到我需要的东西。肯定有一种简单的方法可以做到这一点?
【问题讨论】:
标签: php arrays stripe-payments