stripe-java

Java library for the Stripe API.

  • 所有者: stripe/stripe-java
  • 平台:
  • 許可證: MIT License
  • 分類:
  • 主題:
  • 喜歡:
    0
      比較:

Github星跟蹤圖

Stripe Java client library

Maven Central
JavaDoc
Build Status

The official Stripe Java client library.

Installation

Requirements

  • Java 1.8 or later

Gradle users

Add this dependency to your project's build file:

implementation "com.stripe:stripe-java:17.11.0"

Maven users

Add this dependency to your project's POM:

<dependency>
  <groupId>com.stripe</groupId>
  <artifactId>stripe-java</artifactId>
  <version>17.11.0</version>
</dependency>

Others

You'll need to manually install the following JARs:

ProGuard

If you're planning on using ProGuard, make sure that you exclude the Stripe
client library. You can do this by adding the following to your proguard.cfg
file:

-keep class com.stripe.** { *; }

Documentation

Please see the Java API docs for the most
up-to-date documentation.

You can also refer to the online Javadoc.

Usage

StripeExample.java

import java.util.HashMap;
import java.util.Map;

import com.stripe.Stripe;
import com.stripe.exception.StripeException;
import com.stripe.model.Customer;
import com.stripe.net.RequestOptions;

public class StripeExample {

    public static void main(String[] args) {
        Stripe.apiKey = "sk_test_...";

        Map<String, Object> customerMap = new HashMap<String, Object>();
        customerMap.put("description", "Example descriptipn");
        customerMap.put("email", "test@example.com");
        customerMap.put("payment_method", "pm_card_visa"); // obtained via Stripe.js

        try {
            Customer customer = Customer.create(customerMap);
            System.out.println(customer);
        } catch (StripeException e) {
            e.printStackTrace();
        }
    }
}

See the project's functional tests for more examples.

Per-request Configuration

All of the request methods accept an optional RequestOptions object. This is
used if you want to set an idempotency key, if you are
using Stripe Connect, or if you want to pass the secret API
key on each method.

RequestOptions requestOptions = new RequestOptionsBuilder()
    .setApiKey("sk_test_...")
    .setIdempotencyKey("a1b2c3...")
    .setStripeAccount("acct_...")
    .build();

Customer.list(null, requestOptions);

Customer.retrieve("cus_123456789", requestOptions);

Configuring automatic retries

The library can be configured to automatically retry requests that fail due to
an intermittent network problem or other knowingly non-deterministic errors.
This can be enabled globally:

Stripe.setMaxNetworkRetries(2);

Or on a finer grain level using RequestOptions:

RequestOptions options = RequestOptions.builder()
    .setMaxNetworkRetries(2)
    .build();
Customer.create(params, options);

Idempotency keys are added to requests to guarantee that
retries are safe.

Configuring Timeouts

Connect and read timeouts can be configured globally:

Stripe.setConnectTimeout(30 * 1000); // in milliseconds
Stripe.setReadTimeout(80 * 1000);

Or on a finer grain level using RequestOptions:

RequestOptions options = RequestOptions.builder()
    .setConnectTimeout(30 * 1000) // in milliseconds
    .setReadTimeout(80 * 1000)
    .build();
Customer.create(params, options);

Please take care to set conservative read timeouts. Some API requests can take
some time, and a short timeout increases the likelihood of a problem within our
servers.

Writing a plugin

If you're writing a plugin that uses the library, we'd appreciate it if you
identified using Stripe.setAppInfo():

Stripe.setAppInfo("MyAwesomePlugin", "1.2.34", "https://myawesomeplugin.info");

This information is passed along when the library makes calls to the Stripe
API.

Request latency telemetry

By default, the library sends request latency telemetry to Stripe. These
numbers help Stripe improve the overall latency of its API for all users.

You can disable this behavior if you prefer:

Stripe.enableTelemetry = false;

Development

The test suite depends on stripe-mock, so make sure to fetch and run it from a
background terminal (stripe-mock's README also contains
instructions for installing via Homebrew and other methods):

go get -u github.com/stripe/stripe-mock
stripe-mock

To run all checks (tests and code formatting):

./gradlew check

To run the tests:

./gradlew test

You can run particular tests by passing --tests Class#method. Make sure you
use the fully qualified class name. For example:

./gradlew test --tests com.stripe.model.AccountTest
./gradlew test --tests com.stripe.functional.CustomerTest
./gradlew test --tests com.stripe.functional.CustomerTest.testCustomerCreate

The library uses Spotless along with
google-java-format for code formatting. Code must be
formatted before PRs are submitted, otherwise CI will fail. Run the formatter
with:

./gradlew spotlessApply

The library uses Project Lombok. While it is not a requirement, you
might want to install a plugin for your favorite IDE to
facilitate development.

主要指標

概覽
名稱與所有者stripe/stripe-java
主編程語言Java
編程語言Java (語言數: 3)
平台
許可證MIT License
所有者活动
創建於2011-08-09 00:06:31
推送於2025-04-22 18:43:42
最后一次提交2025-04-21 08:32:13
發布數811
最新版本名稱v29.1.0-beta.3 (發布於 2025-04-17 17:36:12)
第一版名稱v1.0.0-SNAPSHOT (發布於 )
用户参与
星數860
關注者數49
派生數368
提交數2.9k
已啟用問題?
問題數435
打開的問題數19
拉請求數1336
打開的拉請求數3
關閉的拉請求數201
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?