Within Stripe I'm creating a customer, a session and a subscription.
Create customer :
public Customer createCustomer() {
Stripe.apiKey = key;
System.out.println("API KEY : " + Stripe.apiKey);
Customer customer = null;
CustomerCreateParams params =
CustomerCreateParams
.builder()
.setEmail(this.person.getEmail())
.setPaymentMethod("pm_card_visa")
.setInvoiceSettings(
CustomerCreateParams.InvoiceSettings
.builder()
.setDefaultPaymentMethod("pm_card_visa")
.build()
).build();
try {
customer = Customer.create(params);
person.setStripeId(customer.getId());
this.personService.update(customer.getId(), person);
subscribe(customer, createStripSession());
}catch (StripeException ex) {
System.out.println(ex.getMessage());
}
return customer;
}
Create session :
public Session createStripSession() {
Stripe.apiKey = key;
System.out.println("API KEY : " + Stripe.apiKey);
String priceId = "price_1KYX8OCLVCGN5jA7JNtDRaAx";
Map<String, Object> params = new HashMap<>();
params.put("limit", 3);
Session last = null;
try {
SessionCollection sessions = Session.list(params);
List<Session> list = sessions.getData();
last = list.get(list.size() - 1);
System.out.println(last.getId());
}catch (StripeException ex) {
System.out.println(ex.getMessage());
}
SessionCreateParams params2 = new SessionCreateParams.Builder()
.setSuccessUrl("https://example.com/success.html?session_id=" + last.getId())
.setCancelUrl("https://example.com/canceled.html")
.setMode(SessionCreateParams.Mode.SUBSCRIPTION)
.addLineItem(new SessionCreateParams.LineItem.Builder()
// For metered billing, do not pass quantity
.setQuantity(1L)
.setPrice("price_1KYX8OCLVCGN5jA7JNtDRaAx")
.build()
)
.build();
Session session = null;
try {
session = Session.create(params2);
}catch (StripeException ex) {
System.out.println(ex.getMessage());
}
return session;
}
create subscription :
public void subscribe(Customer customer, Session session) throws StripeException {
Stripe.apiKey = key;
System.out.println("API KEY : " + Stripe.apiKey);
List<Object> phases = new ArrayList<>();
List<Object> items = new ArrayList<>();
Map<String, Object> item1 = new HashMap<>();
item1.put(
"price",
"price_1KYX8OCLVCGN5jA7JNtDRaAx"
);
item1.put("quantity", 1);
items.add(item1);
Map<String, Object> phase1 = new HashMap<>();
phase1.put("items", items);
phase1.put("iterations", 12);
// String priceId = request.queryParams("priceId");
String priceId = "price_1KYX8OCLVCGN5jA7JNtDRaAx";
SessionCreateParams params = new SessionCreateParams.Builder()
.setSuccessUrl("https://example.com/success.html?session_id={CHECKOUT_SESSION_ID}")
.setCancelUrl("https://example.com/canceled.html")
.setMode(SessionCreateParams.Mode.SUBSCRIPTION)
.addLineItem(new SessionCreateParams.LineItem.Builder()
// For metered billing, do not pass quantity
.setQuantity(1L)
.setPrice(priceId)
.build()
)
.build();
session = Session.create(params);
phases.add(phase1);
Map<String, Object> params2 = new HashMap<>();
params2.put("customer", customer.getId());
params2.put("start_date", 1646485214);
params2.put("end_behavior", "release");
params2.put("phases", phases);
SubscriptionSchedule subscriptionSchedule = SubscriptionSchedule.create(params2);
}
But when I test my application locally I'm been getting an error :
No such price: 'price_1KYX8OCLVCGN5jA7JNtDRaAx'; a similar object exists in live mode, but a test mode key was used to make this request.; code: resource_missing; request-id: req_a2cHzmevb7n9pZ
I'm using dashboard in test mode and using the test key but I don't know how to solve this,