#Estela
1 messages · Page 1 of 1 (latest)
Hi there, no I don't think you can create a checkout session with line_items that contains prices belongs to different connected accounts.
And I don't see you specify a Stripe-Header in your request, so this request will be made on platform, not connected account.
I found it
Sorry
Let me explain our idea and I'll pass you some code snippets
We are developing a portal where several sellers offer their products for sale.
A buyer can put in his cart several products from different sellers.
When it is time to pay, the buyer user will make a single payment even if he buys from different vendors.
When the platform receives the payment, it distributes to each of the sellers the amount paid for each of their products, minus a percentage that it keeps as a commission.
Code to get the URL to make a payment
Stripe.apiKey = STRIPE_KEY;
// CREATE LINES
List<LineItem> lineItemsList = new ArrayList<>();
for (OrderLine line : order.getOrderLines()) {
Long quantity = Math.round(line.getQuantity());
Long price = Math.round((line.getUnitPrice()) * 100); /// decimales
lineItemsList.add(LineItem.builder()
.setPriceData(PriceData.builder()
.setProduct(line.getProductId().getStripeId())
.setUnitAmount(price)
.setCurrency("eur")
.setTaxBehavior(SessionCreateParams.LineItem.PriceData.TaxBehavior.EXCLUSIVE)
.build())
.setQuantity(quantity)
.build());
}
// CREATE ORDER
String transferGroupId = TRANSFRER_GROUP + order.getId();
SessionCreateParams params = SessionCreateParams
.builder()
.setMode(SessionCreateParams.Mode.PAYMENT)
.addAllLineItem(lineItemsList)
.setAutomaticTax(
SessionCreateParams.AutomaticTax.builder()
.setEnabled(true)
.build())
.setSuccessUrl("https://example.com/success?session_id={CHECKOUT_SESSION_ID}")
.setCancelUrl("https://example.com/cancel")
.setPaymentIntentData(
SessionCreateParams.PaymentIntentData.builder().setTransferGroup(transferGroupId).build())
.build();
try {
Session session = Session.create(params);
return session.getUrl();
} catch (StripeException e) {
LOG.error(e.getMessage());
e.printStackTrace();
}
return null;
}```
OK, so this checkout session is just to collect payment to platform, and you'll create transfers to connected account laters, am I right?
We create webhook to listen for checkout.session.completed events and after making changes in our DB we split the money with the vendors. Below is just the snippet of code that distributes payments to the connected accounts to which the order was placed.
Stripe.apiKey = STRIPE_KEY;
List<OrderLine> orderLines = order.getOrderLines();
// Creamos Transferencias al grupo
for (OrderLine orderLine : orderLines) {
Double total = orderLine.getUnitPrice() * orderLine.getQuantity();
Double pagoForestalSinComision = (total - (total * (STRIPE_AIRCO2_COMMISSION / 100))) * 100;
try {
PaymentIntent pi = PaymentIntent.retrieve(order.getPaymentIntentStripe());
TransferCreateParams transferParams = TransferCreateParams.builder()
.setAmount(pagoForestalSinComision.longValue())
.setCurrency("eur")
.setDestination(orderLine.getProductId().getProvider().getStripeId())
.setTransferGroup(order.getIdTransferGroup())
.setSourceTransaction(pi.getCharges().getData().get(0).getId())
.build();
try {
Transfer transfer = Transfer.create(transferParams);
transfer.getId();
} catch (StripeException e) {
LOG.error(e.getMessage());
e.printStackTrace();
}
} catch (StripeException e1) {
LOG.error(e1.getMessage());
e1.printStackTrace();
}
}
}
Yes, that is correct
OK, is the Orderline your own data type? where you can get the stripe account ID from orderLine.getProductId().getProvider().getStripeId() ?
orderLine.getProductId().getProvider().getStripeId() is from our data base. We save de Stripe ID from de Connected account
OK. This code looks good to me. Additionally you might want to specify a transfer_group to identify the resulting payment as part of a group. Details in https://stripe.com/docs/connect/charges-transfers
Yes, we create a transfer group called "order+idOrderBD"
.setTransferGroup(order.getIdTransferGroup()) in the tranfer
.setPaymentIntentData(SessionCreateParams.PaymentIntentData.builder().setTransferGroup(transferGroupId).build()) in the checkout session
I see. I must miss this part
So the logic and the functions we are using are the right ones, right?
It looks good to me!
Thank you so much for your help Jack ! 🙂