Php 条带:即使客户id不为空,也会创建新客户?
我正试图用下面的代码从Stripe中检索我用户的现有卡。也就是说,当我使用下面的后端时,即使我告诉Stripe仅在$customer\u id不存在的情况下创建一个新客户,即使customer\u id不为null,也会创建一个新客户id?我觉得我错过了一些明显的东西 .phpPhp 条带:即使客户id不为空,也会创建新客户?,php,stripe-payments,Php,Stripe Payments,我正试图用下面的代码从Stripe中检索我用户的现有卡。也就是说,当我使用下面的后端时,即使我告诉Stripe仅在$customer\u id不存在的情况下创建一个新客户,即使customer\u id不为null,也会创建一个新客户id?我觉得我错过了一些明显的东西 .php $email = $_POST['email']; $customer_id = $_POST['customer_id']; //get this id from somewhere a database table,
$email = $_POST['email'];
$customer_id = $_POST['customer_id']; //get this id from somewhere a database table, post parameter, etc.
$customer = \Stripe\Customer::create(array(
'email' => $email,
));
$customer_id = $_POST['customer_id']; //get this id from somewhere a database table, post parameter, etc.
// if the customer id doesn't exist create the customer
if ($customer_id !== null) {
$key = \Stripe\EphemeralKey::create(
["customer" => $customer->id],
["stripe_version" => $_POST['api_version']]
);
header('Content-Type: application/json');
exit(json_encode($key));
} else {
// \Stripe\Customer::retrieve($customer_id);
$cards = \Stripe\Customer::retrieve($customer_id)->sources->all();
// return the cards
header('Content-Type: application/json');
exit(json_encode($key));
}
如果你的情况需要我们调换。当前,如果存在
客户id
,则创建客户。根据描述,你想看到相反的情况,对吗
在这种情况下,只需切换if/else实体:
if ($customer_id !== null) {
$cards = \Stripe\Customer::retrieve($customer_id)->sources->all();
// return the cards
header('Content-Type: application/json');
exit(json_encode($cards)); // you might want to return the cards here?
} else {
$key = \Stripe\EphemeralKey::create(
["customer" => $customer->id],
["stripe_version" => $_POST['api_version']]
);
header('Content-Type: application/json');
exit(json_encode($key));
}
并删除顶部的“创建”块。这也将创建一个您不需要的客户对象。这与
ios
或obj-c
有什么关系?@YvesLeBorg抱歉,老习惯。前端为obj-c;习惯于把它放在标签上$customer\u id
可能包含空的但不是空的内容,如零或空字符串。您正在顶部创建客户(无任何条件)-为什么\Stripe\customer::create
不在空检查块中?如果将\Stripe\Customer::create
调用包装在If($Customer\u id!==null){
块中,这是否有效?