在PHP中将名称、地址、城市等添加到StripeCustomer对象中


Add name, address, city, etc to Stripe Customer object in PHP

我正在使用Stripe进行支付,并希望向用户对象添加一些附加信息(名字和姓氏、地址和电话)。

$token  = $_POST['stripeToken'];
$email  = strip_tags(trim($_POST['email']));
$donation_type = $_POST['type'];
$donation_type_other = $_POST['other'];
// User Info
$name_first = $_POST['name_first'];
$name_last = $_POST['name_last'];
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$user_info = array("First Name" => $name_first, "Last Name" => $name_last, "Address" => $address, "State" => $state, "Zip Code" => $zip);
// Metadata for the charge
$metadata_charge = array();
if (!empty($donation_type_other) && $donation_type == 'Other') {
    $metadata_charge = array("Donation Type" => $donation_type, "Other" => $donation_type_other);   
} else {
    $metadata_charge = array("Donation Type" => $donation_type);    
}
$customer = 'Stripe'Customer::create(array(
  'email' => $email,
  'card'  => $token,
  'metadata' => $user_info
));
$charge = 'Stripe'Charge::create(array(
  'customer' => $customer->id,
  'receipt_email' => $email,
  'amount'   => $_POST['amount']*100,
  'currency' => 'usd',
  "metadata" => $metadata_charge
));

最好的方法是什么?要在Customer对象上使用metadata?或者我会将其设置为发货地址/信息吗

由于您创建的是一个客户对象,基于您对想要存储的内容的描述,所以这似乎并不重要。Stripe不会对实物进行任何履行,因此他们在客户对象上提供的存储主要是为了您的利益。因此,当您访问客户对象(通过类似cus_8Dmu7vi6wah58z的ID)时,它应该返回所有的发货信息和元数据。

发货哈希中有一个专用的名称字段,但它不会从姓氏中提取名字。如果这是您真正想要的东西,那么将其存储在元数据字段中可能会更容易。

您还可以注意到,将"发货"信息存储在发货哈希中,并将"计费"信息存储到元数据哈希中可能会有所帮助。