我在我的一个codeigniter项目中使用了条带。
我正在使用订阅方法将用户添加到订阅中。
这是我的订阅代码。
$customer = Stripe_Customer::create(array(
"card" => $token,
"plan" => "monthly",
"email" => "user@domain.com",
"description" => "user@domain.com",
));
使用此代码我正在创建新客户并添加到订阅计划。 此代码创建新用户并将该用户添加到订阅计划中,但该用户不会收取金额。
我做错了什么或遗失了什么?
答案 0 :(得分:0)
您可能最好创建客户,卡片对象,并在单独的操作中订阅客户。 Stripe应该在添加到订阅时自动向客户收取费用,但可能会在一次完成所有操作时尝试以错误的顺序进行操作?你试过将它们分开吗?我已经编写了下面的库,用于CI和Stripe:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Stripe {
const PROCESS_URL = 'https://api.stripe.com/v1';
const API_KEY = '{YOUR-STRIPE-API-KEY}';
public $CI;
public $db;
/*
|-------------------------------------------
| STRIPE | CONSTRUCT
|-------------------------------------------
*/
public function __construct()
{
$this->CI = &get_instance();
$this->db = $this->CI->db;
}
/*
|-------------------------------------------
| STRIPE | CREATE CUSTOMER
|-------------------------------------------
*/
public function create_customer($request) {
$process_url = self::PROCESS_URL.'/customers';
return $this->post_request($process_url, $request);
}
/*
|-------------------------------------------
| STRIPE | GET CUSTOMER
|-------------------------------------------
*/
public function get_customer($cus_id) {
$process_url = self::PROCESS_URL.'/customers/'.$cus_id;
$customer_obj = $this->post_request($process_url);
$customer_arr = json_decode($customer_obj);
return $customer_arr;
}
/*
|-------------------------------------------
| STRIPE | GET CUSTOMER
|-------------------------------------------
*/
public function get_default_source($cus_id) {
$process_url = self::PROCESS_URL.'/customers/'.$cus_id;
$customer_obj = $this->post_request($process_url);
$customer_arr = json_decode($customer_obj);
$card_id = $customer_arr->default_source;
return $card_id;
}
/*
|-------------------------------------------
| STRIPE | GET CARD
|-------------------------------------------
*/
public function get_card($cus_id,$card_id) {
$process_url = self::PROCESS_URL.'/customers/'.$cus_id.'/cards/'.$card_id;
$card_obj = $this->post_request($process_url);
return $this->decode_response($card_obj);
}
/*
|-----------------------------------------------
| STRIPE | ADD CARD
| Params: Stripe Customer ID, Card Data Object
|-----------------------------------------------
*/
public function add_card($cus_id,$card_data) {
$request = array(
'source' => $card_data
);
$process_url = self::PROCESS_URL.'/customers/'.$cus_id.'/sources';
$card_json = $this->post_request($process_url,$request);
$card_obj = json_decode($card_json);
// successful?
if(isset($card_obj->id)) {
$ret_arr = array(
'status' => true,
'response' => $card_obj
);
} else {
$ret_arr = array(
'status' => false,
'error' => $card_obj->error->message
);
}
return $ret_arr;
}
/*
|-------------------------------------------
| STRIPE | ADD CARD
|-------------------------------------------
*/
public function subscribe_customer($cus_id,$plan_id,$source) {
$request['plan'] = $plan_id;
if($source) { $request['source'] = $source; }
$process_url = self::PROCESS_URL.'/customers/'.$cus_id.'/subscriptions';
$plan_json = $this->post_request($process_url,$request);
$plan_obj = json_decode($plan_json);
// successful?
if(isset($plan_obj->id)) {
$ret_arr = array(
'status' => true,
'response' => $plan_obj
);
} else {
$ret_arr = array(
'status' => false,
'error' => $plan_obj->error->message
);
}
return $ret_arr;
}
/*
|-------------------------------------------
| STRIPE | DECODE JSON RESPONSE
|-------------------------------------------
*/
public function decode_response($response) {
return json_decode($response);
}
/*
|-------------------------------------------
| STRIPE | POST REQUESTS
|-------------------------------------------
*/
public function post_request($url, $data = NULL)
{
$ch = curl_init($url);
if (is_array($data))
{
$data = http_build_query($data);
}
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
return $this->do_curl_request($ch);
}
/*
|-------------------------------------------
| STRIPE | CURL REQUEST
|-------------------------------------------
*/
private function do_curl_request($ch)
{
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_USERPWD, self::API_KEY);
$response = curl_exec($ch);
$error = curl_error($ch);
if($response) {
} else {
var_dump($error);
}
curl_close($ch);
return $response;
}
}
创建客户,添加卡,并将客户订阅到给定计划。
加载库
$this->load->library('stripe');
创建我的客户
// Create Stripe Customer
$request = array(
"description" => {CUSTOMER-NAME},
"metadata" => array(
'First Name' => {CONTACT-FIRST-NAME},
'Last Name' => {CONTACT-LAST-NAME},
'Company Name' => {COMPANY-NAME}
),
"shipping" => array(
"name" => {CUSTOMER-NAME},
"address" => array (
"city" => {CITY},
"state" => {STATE},
"postal_code" => {ZIP},
"line1" => {STREET-1},
"line2" => {STREET-2},
)
),
"email" => $this->input->post('vendor_contact_email')
);
// Create the Customer
$stipe_response = $this->stripe->create_customer($request);
$response_arr = $this->stripe->decode_response($stipe_response);
$stripe_customer_id = $response_arr->id;
添加卡
$data = array(
"object" => 'card',
"number" => {CARD-NUMBER},
"address_city" => {BILLING-CITY},
"address_state" => {BILLING-STATE},
"address_zip" => {BILLING-ZIP},
"address_line1" => {BILLING-ADDRESS},
"address_line2" => {BILLING-ADDRESS-2},
"name" => {BILLING-NAME},
"exp_month" => {EXPIRATION-MONTH},
"exp_year" => {EXPIRATION-YEAR},
);
// Add the Card
$stripe_response = $this->stripe->add_card($stripe_customer_id,$data);
$response_arr = $this->stripe->decode_response($stipe_response);
订阅客户
$stripe_response = $this->stripe->subscribe_customer($stripe_customer_id,{PLAN-ID},{SOURCE-ID});
$response_arr = $this->stripe->decode_response($stipe_response);
如果您遇到问题,可以使用$ response_arr调试从Stripe获得的任何回复。希望这会有所帮助。我发现libarary比Stripe SDK更容易使用。