feat: Save balance after successful payment
	
		
			
	
		
	
	
		
	
		
			Some checks failed
		
		
	
	
	
		
	
		
			Some checks failed
		
		
	
	
This commit is contained in:
		
					parent
					
						
							
								ce7c30b9ae
							
						
					
				
			
			
				commit
				
					
						d25b894f38
					
				
			
		
					 11 changed files with 242 additions and 9 deletions
				
			
		|  | @ -0,0 +1,64 @@ | |||
| package de.szut.casino.deposit; | ||||
| 
 | ||||
| import com.fasterxml.jackson.core.JsonProcessingException; | ||||
| 
 | ||||
| import com.stripe.Stripe; | ||||
| import com.stripe.exception.SignatureVerificationException; | ||||
| import com.stripe.exception.StripeException; | ||||
| import com.stripe.model.*; | ||||
| import com.stripe.model.checkout.Session; | ||||
| import com.stripe.net.Webhook; | ||||
| import com.stripe.param.checkout.SessionRetrieveParams; | ||||
| import de.szut.casino.user.UserEntity; | ||||
| import de.szut.casino.user.UserRepository; | ||||
| import jakarta.annotation.PostConstruct; | ||||
| import org.slf4j.Logger; | ||||
| import org.slf4j.LoggerFactory; | ||||
| import org.springframework.beans.factory.annotation.Value; | ||||
| import org.springframework.http.ResponseEntity; | ||||
| import org.springframework.web.bind.annotation.*; | ||||
| 
 | ||||
| import java.math.BigDecimal; | ||||
| import java.util.Objects; | ||||
| import java.util.Optional; | ||||
| 
 | ||||
| @RestController | ||||
| public class WebhookController { | ||||
|     private static final Logger logger = LoggerFactory.getLogger(WebhookController.class); | ||||
|     @Value("${stripe.secret.key}") | ||||
|     private String stripeSecretKey; | ||||
| 
 | ||||
|     @Value("${stripe.webhook.secret}") | ||||
|     private String webhookSecret; | ||||
| 
 | ||||
|     private final TransactionService transactionService; | ||||
| 
 | ||||
|     public WebhookController(TransactionService transactionService) { | ||||
|         this.transactionService = transactionService; | ||||
|     } | ||||
| 
 | ||||
|     @PostConstruct | ||||
|     public void init() { | ||||
|         Stripe.apiKey = stripeSecretKey; | ||||
|     } | ||||
| 
 | ||||
|     @PostMapping("/webhook") | ||||
|     public ResponseEntity<String> webhook(@RequestBody String payload, @RequestHeader("Stripe-Signature") String sigHeader) throws StripeException { | ||||
|         Event event = Webhook.constructEvent(payload, sigHeader, webhookSecret); | ||||
| 
 | ||||
|         System.out.println(event.getType()); | ||||
| 
 | ||||
|         switch (event.getType()) { | ||||
|             case "checkout.session.completed": | ||||
|             case "checkout.session.async_payment_succeeded": | ||||
|                 Session session = (Session) event.getData().getObject(); | ||||
| 
 | ||||
|                 this.transactionService.fulfillCheckout(session.getId()); | ||||
|                 break; | ||||
|             default: | ||||
| 
 | ||||
|         } | ||||
| 
 | ||||
|         return ResponseEntity.ok().body(null); | ||||
|     } | ||||
| } | ||||
		Reference in a new issue