課金サービス Stripe で テスト用APIを利用する ( #Ruby や curl の例 ) ( Python PHP Java Node Go .NET も対応してる )

# 開発者用ドキュメント

[Development Quickstart | Stripe](https://stripe.com/docs/development#api-keys)

# Stripeにアカウント作成する

しておく。

# テスト用キーを取得

https://dashboard.stripe.com/test/apikeys からゲット可能

![image](https://user-images.githubusercontent.com/13635059/69001411-25256580-0921-11ea-8f53-0bbc861f36ee.png)


![image](https://user-images.githubusercontent.com/13635059/69001414-2d7da080-0921-11ea-8bbd-6df529082eea.png)

# curl を叩く

- ドキュメントのまま
- curl の -u オプションは --user の意味
- -u の指定は自分のテスト用API key に置き換える

![image](https://user-images.githubusercontent.com/13635059/69001442-91a06480-0921-11ea-845b-51b8174aa24f.png)

```
curl https://api.stripe.com/v1/charges \
  -u sk_test_4eC39HqLyjWDarjtT1zdp7dc: \
  -d amount=1000 \
  -d currency=usd \
  -d source=tok_visa \
  -d receipt_email="jenny.rosen@example.com"
```

```json
$ curl https://api.stripe.com/v1/charges -u sk_test_4eC39HqLyjWDarjtT1zdp7dc -d amount=1000 -d currency=usd -d source=tok_visa -d receipt_email="jenny.rosen@example.com"
{
  "id": "ch_1FfcA2Cmti5jpytUacPNktRA",
  "object": "charge",
  "amount": 1000,
  "amount_refunded": 0,
  "application": null,
  "application_fee": null,
  "application_fee_amount": null,
  "balance_transaction": "txn_1FfcA2Cmti5jpytUCdYjhDCH",
  "billing_details": {
    "address": {
      "city": null,
      "country": null,
      "line1": null,
      "line2": null,
      "postal_code": null,
      "state": null
    },
    "email": null,
    "name": null,
    "phone": null
  },
  "captured": true,
  "created": 1573952562,
  "currency": "usd",
  "customer": null,
  "description": null,
  "destination": null,
  "dispute": null,
  "disputed": false,
  "failure_code": null,
  "failure_message": null,
  "fraud_details": {
  },
  "invoice": null,
  "livemode": false,
  "metadata": {
  },
  "on_behalf_of": null,
  "order": null,
  "outcome": {
    "network_status": "approved_by_network",
    "reason": null,
    "risk_level": "normal",
    "risk_score": 22,
    "seller_message": "Payment complete.",
    "type": "authorized"
  },
  "paid": true,
  "payment_intent": null,
  "payment_method": "card_1FfcA2Cmti5jpytUB1a5Wjmx",
  "payment_method_details": {
    "card": {
      "brand": "visa",
      "checks": {
        "address_line1_check": null,
        "address_postal_code_check": null,
        "cvc_check": null
      },
      "country": "US",
      "exp_month": 11,
      "exp_year": 2020,
      "fingerprint": "3Dnj9E30BUfyFTcl",
      "funding": "credit",
      "installments": null,
      "last4": "4242",
      "network": "visa",
      "three_d_secure": null,
      "wallet": null
    },
    "type": "card"
  },
  "receipt_email": "jenny.rosen@example.com",
  "receipt_number": null,
  "receipt_url": "https://pay.stripe.com/receipts/acct_1Ffc6rCmti5jpytU/ch_1FfcA2Cmti5jpytUacPNktRA/rcpt_GC0AZ5uuLQ94ft0VAN3y3xT8oVmM7Di",
  "refunded": false,
  "refunds": {
    "object": "list",
    "data": [

    ],
    "has_more": false,
    "total_count": 0,
    "url": "/v1/charges/ch_1FfcA2Cmti5jpytUacPNktRA/refunds"
  },
  "review": null,
  "shipping": null,
  "source": {
    "id": "card_1FfcA2Cmti5jpytUB1a5Wjmx",
    "object": "card",
    "address_city": null,
    "address_country": null,
    "address_line1": null,
    "address_line1_check": null,
    "address_line2": null,
    "address_state": null,
    "address_zip": null,
    "address_zip_check": null,
    "brand": "Visa",
    "country": "US",
    "customer": null,
    "cvc_check": null,
    "dynamic_last4": null,
    "exp_month": 11,
    "exp_year": 2020,
    "fingerprint": "3Dnj9E30BUfyFTcl",
    "funding": "credit",
    "last4": "4242",
    "metadata": {
    },
    "name": null,
    "tokenization_method": null
  },
  "source_transfer": null,
  "statement_descriptor": null,
  "statement_descriptor_suffix": null,
  "status": "succeeded",
  "transfer_data": null,
  "transfer_group": null
}
```

# Rubyの例

```
gem install stripe
```

```rb
require 'stripe'

# 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.api_key = 'sk_test_4eC39HqLyjWDarjtT1zdp7dc'

charge = Stripe::Charge.create({
    amount: 1000,
    currency: 'usd',
    source: 'tok_visa',
    receipt_email: 'jenny.rosen@example.com',
})

p charge
```

```
$ ruby /tmp/stripe.rb
#<Stripe::Charge:0x3ffefd0955d8 id=ch_1FfcFO2eZvKYlo2CFlYmmLyP> JSON: {
  "id": "ch_1FfcFO2eZvKYlo2CFlYmmLyP",
  "object": "charge",
  "amount": 1000,
  "amount_refunded": 0,
  "application": null,
  "application_fee": null,
  "application_fee_amount": null,
  "balance_transaction": "txn_1FfcFO2eZvKYlo2CgaRI4YVN",
  "billing_details": {"address":{"city":null,"country":null,"line1":null,"line2":null,"postal_code":null,"state":null},"email":null,"name":null,"phone":null},
  "captured": true,
  "created": 1573952894,
  "currency": "usd",
  "customer": null,
  "description": null,
  "destination": null,
  "dispute": null,
  "disputed": false,
  "failure_code": null,
  "failure_message": null,
  "fraud_details": {},
  "invoice": null,
  "livemode": false,
  "metadata": {},
  "on_behalf_of": null,
  "order": null,
  "outcome": {"network_status":"approved_by_network","reason":null,"risk_level":"normal","risk_score":19,"seller_message":"Payment complete.","type":"authorized"},
  "paid": true,
  "payment_intent": null,
  "payment_method": "card_1FfcFO2eZvKYlo2C3qPN3leg",
  "payment_method_details": {"card":{"brand":"visa","checks":{"address_line1_check":null,"address_postal_code_check":null,"cvc_check":null},"country":"US","exp_month":11,"exp_year":2020,"fingerprint":"Xt5EWLLDS7FJjR1c","funding":"credit","installments":null,"last4":"4242","network":"visa","three_d_secure":null,"wallet":null},"type":"card"},
  "receipt_email": "jenny.rosen@example.com",
  "receipt_number": null,
  "receipt_url": "https://pay.stripe.com/receipts/acct_1032D82eZvKYlo2C/ch_1FfcFO2eZvKYlo2CFlYmmLyP/rcpt_GC0FYGOKk95bODBCulYZ5nbLBW0bBwa",
  "refunded": false,
  "refunds": {"object":"list","data":[],"has_more":false,"total_count":0,"url":"/v1/charges/ch_1FfcFO2eZvKYlo2CFlYmmLyP/refunds"},
  "review": null,
  "shipping": null,
  "source": {"id":"card_1FfcFO2eZvKYlo2C3qPN3leg","object":"card","address_city":null,"address_country":null,"address_line1":null,"address_line1_check":null,"address_line2":null,"address_state":null,"address_zip":null,"address_zip_check":null,"brand":"Visa","country":"US","customer":null,"cvc_check":null,"dynamic_last4":null,"exp_month":11,"exp_year":2020,"fingerprint":"Xt5EWLLDS7FJjR1c","funding":"credit","last4":"4242","metadata":{},"name":null,"tokenization_method":null},
  "source_transfer": null,
  "statement_descriptor": null,
  "statement_descriptor_suffix": null,
  "status": "succeeded",
  "transfer_data": null,
  "transfer_group": null
}
```

# 簡単だった

ありがとう Stripe

# 他のAPIを叩く

API Reference に各言語でのコードの例も載ってるので、何でも出来そう。

[Stripe API Reference](https://stripe.com/docs/api)


# Original by Github issue

https://github.com/YumaInaura/YumaInaura/issues/2730








<!-- Update From Qiita API -->

# チャットメンバー募集


何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

https://line.me/ti/g2/eEPltQ6Tzh3pYAZV8JXKZqc7PJ6L0rpm573dcQ





# Twitter


https://twitter.com/YumaInaura


<!-- Update From Qiita API -->