• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

stripe/stripe-java: Java library for the Stripe API.

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

stripe/stripe-java

开源软件地址:

https://github.com/stripe/stripe-java

开源编程语言:

Java 100.0%

开源软件介绍:

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:20.129.0"

Maven users

Add this dependency to your project's POM:

<dependency>
  <groupId>com.stripe</groupId>
  <artifactId>stripe-java</artifactId>
  <version>20.129.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.

See video demonstrations covering how to use the library.

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;
import com.stripe.param.CustomerCreateParams;

public class StripeExample {

    public static void main(String[] args) {
        Stripe.apiKey = "sk_test_...";
        
        CustomerCreateParams params =
            CustomerCreateParams
                .builder()
                .setDescription("Example description")
                .setEmail("[email protected]")
                .setPaymentMethod("pm_card_visa")  // obtained via Stripe.js
                .build();

        try {
            Customer customer = Customer.create(params);
            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 = RequestOptions.builder()
    .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.

Configuring DNS Cache TTL

We cannot guarantee that the IP address of the Stripe API will be static. Commonly, default JVM configurations can have their DNS cache TTL set to forever. If Stripe's IP address changes, your application's requests to Stripe will all fail until the JVM restarts. Therefore we recommend that you modify the JVM's networkaddress.cache.ttl property to 60 seconds.

How to use undocumented parameters and properties

stripe-java is a typed library and it supports all public properties or parameters.

Stripe sometimes has beta which introduces new properties or parameters that are not immediately public. The library does not support these properties or parameters until they are public but there is still an approach that allows you to use them.

Parameters

To pass undocumented parameters to Stripe using stripe-java you need to use the putExtraParam() method, as shown below:

CustomerCreateParams params =
  CustomerCreateParams.builder()
    .setEmail("[email protected]")
    .putExtraParam("secret_feature_enabled", "true")
    .putExtraParam("secret_parameter[primary]", "primary value")
    .putExtraParam("secret_parameter[secondary]", "secondary value")
    .build();

Customer customer = customer.Create(params);

Properties

To retrieve undocumented properties from Stripe using Java you can use an option in the library to return the raw JSON object and return the property as a native type. An example of this is shown below:

final Customer customer = Customer.retrieve("cus_1234");
Boolean featureEnabled =
  customer.getRawJsonObject()
    .getAsJsonPrimitive("secret_feature_enabled")
    .getAsBoolean();
String primaryValue =
  customer.getRawJsonObject()
    .getAsJsonObject("secret_parameter")
    .getAsJsonPrimitive("primary")
    .getAsString();
String secondaryValue =
  customer.getRawJsonObject()
    .getAsJsonObject("secret_parameter")
    .getAsJsonPrimitive("secondary")
    .getAsString();

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

JDK 18 is required to build the Stripe Java library. By default, tests use the same Java runtime as the build. To use a custom version of Java runtime for tests set the JAVA_TEST_HOME environment variable to runtime's home directory.

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.




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Hacker233/JavaScript: JavaScript学习发布时间:2022-06-23
下一篇:
jfaster/mango: Distributed ORM Framework for Java发布时间:2022-06-23
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap