php - 在 PHP 中执行 Curl 以进行 Stripe 订阅

标签 php curl stripe-payments

Stripe API 允许进行 Curl 调用。例如命令:

curl https://api.stripe.com//v1/customers/cus_5ucsCmNxF3jsSY/subscriptions    -u sk_test_REDACTED:

返回客户 cus_5ucsCmNxF3jsSY 的订阅。

如何使用 PHP 调用此 curl 命令(我试图避免使用 PHP Stripe 库)。

我正在尝试以下操作:

<?php 
        // create curl resource 
        $ch = curl_init(); 

        // set url 
        curl_setopt($ch, CURLOPT_URL, "https://api.stripe.com//v1/customers/cus_5ucsCmNxF3jsSY/subscriptions    -u sk_test_REDACTED:"); 

        //return the transfer as a string 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

        // $output contains the output string 
        $output = curl_exec($ch); 
        print($output);

        // close curl resource to free up system resources 
        curl_close($ch);      
?>

不过curl好像没有带url的-u参数。我收到以下错误:

{ "error": { "type": "invalid_request_error", "message": "You did not provide an API key. You need to provide your API key in the Authorization header, using Bearer auth (e.g. 'Authorization: Bearer YOUR_SECRET_KEY'). See https://stripe.com/docs/api#authentication for details, or we can help at https://support.stripe.com/." } 

如何将 -u sk_test_REDACTED: 参数传递给我的 curl 调用?

最佳答案

我遇到了同样的问题。我想使用 PHP 的 CURL 函数而不是使用官方的 stripe API,因为单例让我感到恶心。

我编写了我自己的非常简单的 Stripe 类,它通过 PHP 和 CURL 使用他们的 API。

class Stripe {
    public $headers;
    public $url = 'https://api.stripe.com/v1/';
    public $method = null;
    public $fields = array();
    
    function __construct () {
        $this->headers = array('Authorization: Bearer '.STRIPE_API_KEY); // STRIPE_API_KEY = your stripe api key
    }
    
    function call () {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);

        switch ($this->method){
           case "POST":
              curl_setopt($ch, CURLOPT_POST, 1);
              if ($this->fields)
                 curl_setopt($ch, CURLOPT_POSTFIELDS, $this->fields);
              break;
           case "PUT":
              curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
              if ($this->fields)
                 curl_setopt($ch, CURLOPT_POSTFIELDS, $this->fields);
              break;
           default:
              if ($this->fields)
                 $this->url = sprintf("%s?%s", $this->url, http_build_query($this->fields));
        }

        curl_setopt($ch, CURLOPT_URL, $this->url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        $output = curl_exec($ch);
        curl_close($ch);

        return json_decode($output, true); // return php array with api response
    }
}

// create customer and use email to identify them in stripe
$s = new Stripe();
$s->url .= 'customers';
$s->method = "POST";
$s->fields['email'] = $_POST['email'];
$customer = $s->call();

// create customer subscription with credit card and plan
$s = new Stripe();
$s->url .= 'customers/'.$customer['id'].'/subscriptions';
$s->method = "POST";
$s->fields['plan'] = $_POST['plan']; // name of the stripe plan i.e. my_stripe_plan
// credit card details
$s->fields['source'] = array(
    'object' => 'card',
    'exp_month' => $_POST['card_exp_month'],
    'exp_year' => $_POST['card_exp_year'],
    'number' => $_POST['card_number'],
    'cvc' => $_POST['card_cvc']
);
$subscription = $s->call();

如果您想进一步操作数据,可以通过 print_r 转储 $customer 和 $subscription 以查看响应数组。

关于php - 在 PHP 中执行 Curl 以进行 Stripe 订阅,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29318707/

相关文章:

ruby - 每隔 x 秒单击一次按钮的网站脚本

stripe-payments - Apple Pay 和 Stripe 如何共存?

javascript - 无法向 Stripe Connect 银行账户添加额外的验证文件以启用付款

php - 以前通过 TWILIO 发送 SMS 消息的工作调用现在失败了... "Unsupported SSL context options are set."

php - FB SDK 和 cURL : Unknown SSL protocol error in connection to graph. facebook.com:443

php - 如何使用 php 跳过安全身份验证 - MySQL 5.6.15?

curl - 启用 OAuth Drupal 7 模块时出错

javascript - 第一次单击后禁用提交按钮

nginx - PHP-FPM 将堆栈跟踪日志分解为单独的事件

javascript - 从 value 属性获取 <li> 元素的值