我一直在尝试将条带库集成到我的Codeigniter应用程序
我尝试将已下载的库置于application/libraries/Stripe
& system/libraries/Stripe
下载链接:https://github.com/stripe/stripe-php ..
关于如何加载图书馆的信息不多。
以下是直接来自Stripe的PHP文档的代码 https://stripe.com/docs/tutorials/charges
// Set your secret key: remember to change this to your live secret key in production
// See your keys here https://dashboard.stripe.com/account/apikeys
\Stripe\Stripe::setApiKey("sk_test_xxxxxxxxxxxxxxxxxx");
// Get the credit card details submitted by the form
$token = $_POST['stripeToken'];
// Create the charge on Stripe's servers - this will charge the user's card
try {
$charge = \Stripe\Charge::create(array(
"amount" => 1000, // amount in cents, again
"currency" => "usd",
"source" => $token,
"description" => "Example charge"
));
} catch(\Stripe\Error\Card $e) {
// The card has been declined
}
如何加载此库,因为从现在开始我在尝试使用上述代码加载时出现错误
任何帮助?
修改
这就是我将Lib包含在控制器中的方式
require_once(APPPATH.'libraries/stripe/init.php');
答案 0 :(得分:1)
Stripe文档可以稍微简单一点,直到您习惯它为止。我发现端点PHP文档要好得多。我选择编写自己的库来处理Stripe,而不是使用他们的下载SDK。如果你将下面的一些内容与他们的文档进行比较,它可能会让你指出正确的方向来阐述。
<?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
| (In case you need access to the CI Instance
|--------------------------------------------
*/
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;
}
}
// Create Stripe Customer
$request = array(
"description" => "{DESCRIPTION}",
"metadata" => array(
'First Name' => "{FIRST NAME}",
'Last Name' => "{LAST NAME}",
'Company Name' => "{COMPANY NAME}"
),
"shipping" => array(
"name" => "{NAME}",
"address" => array (
"city" => "{CITY}",
"state" => "{STATE}",
"postal_code" => "{POSTAL CODE}",
"line1" => "{STREET ADDRES}",
"line2" => "{STREET ADDRESS 2}",
)
),
"email" => "{SOME EMAIL ADDRESS}"
);
$this->load->library( 'stripe' );
$response = $this->stripe->create_customer( $request );
$response_arr = $this->stripe->decode_response( $response );
$data['stripe_customer_id'] = $response_arr->id;
$card_object = array(
"object" => 'card',
"address_city" => "{BILLING CITY}",
"address_state" => "{BILLING STATE}",
"address_zip" => "{BILLING ZIP}",
"address_line1" => "{BILLING ADDRESS}",
"address_line2" => "{BILLING ADDRESS 2}",
"name" => "{NAME ON CARD}",
"number" => "{CARD NUMBER}",
"exp_month" => "{EXPIRATION MONTH}",
"exp_year" => "{EXPIRATION YEAR}",
);
$this->load->library( 'stripe' );
$return = $this->stripe->add_card({STRIPE_CUSTOMER_ID},$card_object);
希望这有助于理解条带API的工作原理。