use of com.stripe.Stripe in project alf.io by alfio-event.
the class BaseStripeManager method refund.
// https://stripe.com/docs/api#create_refund
boolean refund(Transaction transaction, PurchaseContext purchaseContext, Integer amountToRefund) {
Optional<Integer> amount = Optional.ofNullable(amountToRefund);
String chargeId = transaction.getTransactionId();
try {
String amountOrFull = amount.map(p -> MonetaryUtil.formatCents(p, transaction.getCurrency())).orElse("full");
log.info("Stripe: trying to do a refund for payment {} with amount: {}", chargeId, amountOrFull);
Map<String, Object> params = new HashMap<>();
params.put("charge", chargeId);
amount.ifPresent(a -> params.put("amount", a));
if (transaction.getPlatformFee() > 0 && isConnectEnabled(new PaymentContext(purchaseContext))) {
params.put("refund_application_fee", true);
}
Optional<RequestOptions> requestOptionsOptional = options(purchaseContext);
if (requestOptionsOptional.isPresent()) {
RequestOptions options = requestOptionsOptional.get();
Refund r = Refund.create(params, options);
boolean pending = PENDING.equals(r.getStatus());
if (SUCCEEDED.equals(r.getStatus()) || pending) {
log.info("Stripe: refund for payment {} {} for amount: {}", chargeId, pending ? "registered" : "executed with success", amountOrFull);
return true;
} else {
log.warn("Stripe: was not able to refund payment with id {}, returned status is not 'succeded' but {}", chargeId, r.getStatus());
return false;
}
}
return false;
} catch (StripeException e) {
log.warn("Stripe: was not able to refund payment with id " + chargeId, e);
return false;
}
}
use of com.stripe.Stripe in project alf.io by alfio-event.
the class StripeWebhookPaymentManager method parseTransactionPayload.
@Override
public Optional<TransactionWebhookPayload> parseTransactionPayload(String body, String signature, Map<String, String> additionalInfo, PaymentContext paymentContext) {
try {
var stripeEvent = Webhook.constructEvent(body, signature, getWebhookSignatureKey(paymentContext.getConfigurationLevel()));
String eventType = stripeEvent.getType();
if (eventType.startsWith("charge.")) {
return deserializeObject(stripeEvent).map(obj -> new StripeChargeTransactionWebhookPayload(eventType, (Charge) obj));
} else if (eventType.startsWith("payment_intent.")) {
return deserializeObject(stripeEvent).map(obj -> new StripePaymentIntentWebhookPayload(eventType, (PaymentIntent) obj));
}
return Optional.empty();
} catch (Exception e) {
log.error("got exception while handling stripe webhook", e);
return Optional.empty();
}
}
Aggregations