• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

stripe / stripe-java / #16493

03 Oct 2024 07:15PM UTC coverage: 12.942% (+0.08%) from 12.864%
#16493

push

github

web-flow
Merge Stripe-java v27.0.0 to beta branch (#1888)

409 of 1651 new or added lines in 88 files covered. (24.77%)

31 existing lines in 7 files now uncovered.

18773 of 145050 relevant lines covered (12.94%)

0.13 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/main/java/com/stripe/examples/MeterEventManager.java
1
package com.stripe.examples;
2

3
import com.stripe.StripeClient;
4
import com.stripe.model.v2.billing.MeterEventSession;
5
import com.stripe.param.v2.billing.MeterEventStreamCreateParams;
6
import java.time.Instant;
7

8
public class MeterEventManager {
9

10
  private String apiKey;
11
  private MeterEventSession meterEventSession;
12

NEW
13
  public MeterEventManager(String apiKey) {
×
NEW
14
    this.apiKey = apiKey;
×
NEW
15
  }
×
16

17
  @SuppressWarnings("CatchAndPrintStackTrace")
18
  private void refreshMeterEventSession() {
NEW
19
    if (meterEventSession == null || meterEventSession.getExpiresAt().isBefore(Instant.now())) {
×
20
      // Create a new meter event session in case the existing session expired
21
      try {
NEW
22
        StripeClient client = new StripeClient(apiKey);
×
NEW
23
        meterEventSession = client.v2().billing().meterEventSession().create();
×
NEW
24
      } catch (Exception e) {
×
NEW
25
        e.printStackTrace();
×
NEW
26
      }
×
27
    }
NEW
28
  }
×
29

30
  @SuppressWarnings("CatchAndPrintStackTrace")
31
  public void sendMeterEvent(String eventName, String stripeCustomerId, String value) {
32
    // Refresh the meter event session, if necessary
NEW
33
    refreshMeterEventSession();
×
34

35
    // Create a meter event
36

37
    MeterEventStreamCreateParams.Event eventParams =
NEW
38
        MeterEventStreamCreateParams.Event.builder()
×
NEW
39
            .setEventName(eventName)
×
NEW
40
            .putPayload("stripe_customer_id", stripeCustomerId)
×
NEW
41
            .putPayload("value", value)
×
NEW
42
            .build();
×
43
    MeterEventStreamCreateParams params =
NEW
44
        MeterEventStreamCreateParams.builder().addEvent(eventParams).build();
×
45

46
    try {
NEW
47
      StripeClient client = new StripeClient(meterEventSession.getAuthenticationToken());
×
NEW
48
      client.v2().billing().meterEventStream().create(params);
×
NEW
49
    } catch (Exception e) {
×
NEW
50
      e.printStackTrace();
×
NEW
51
    }
×
NEW
52
  }
×
53

54
  public static void main(String[] args) {
NEW
55
    String apiKey = "{{API_KEY}}";
×
NEW
56
    String customerId = "{{CUSTOMER_ID}}"; // Replace with actual customer ID
×
57

NEW
58
    MeterEventManager manager = new MeterEventManager(apiKey);
×
NEW
59
    manager.sendMeterEvent("alpaca_ai_tokens", customerId, "28");
×
NEW
60
  }
×
61
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc