feat: make stripe functional
This commit is contained in:
parent
177dd78592
commit
1d0162d250
6 changed files with 56 additions and 56 deletions
|
@ -8,6 +8,8 @@ import com.stripe.param.PriceCreateParams;
|
|||
import com.stripe.param.checkout.SessionCreateParams;
|
||||
import de.szut.casino.deposit.dto.AmountDto;
|
||||
import de.szut.casino.deposit.dto.SessionIdDto;
|
||||
import jakarta.validation.Valid;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.autoconfigure.cassandra.CassandraProperties;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
|
@ -17,32 +19,28 @@ import org.springframework.web.bind.annotation.RestController;
|
|||
|
||||
@RestController
|
||||
public class DepositController {
|
||||
|
||||
@Value("${stripe.secret.key}")
|
||||
private String stripeKey;
|
||||
|
||||
@PostMapping("/deposit/checkout")
|
||||
public ResponseEntity<? extends Object> checkout(@RequestBody AmountDto amountDto) {
|
||||
Stripe.apiKey = "sk_test_51QrePYIvCfqz7ANgqam8rEwWcMeKiLOof3j6SCMgu2sl4sESP45DJxca16mWcYo1sQaiBv32CMR6Z4AAAGQPCJo300ubuZKO8I";
|
||||
try {
|
||||
SessionCreateParams params = SessionCreateParams.builder()
|
||||
.addLineItem(SessionCreateParams.LineItem.builder()
|
||||
.setPriceData(InvoiceItemCreateParams.PriceData.builder()
|
||||
.setCurrency("EUR")
|
||||
.setUnitAmount(1L)
|
||||
.build()
|
||||
)
|
||||
.build())
|
||||
.setSuccessUrl("http://localhost:8080/deposit/success")
|
||||
.setMode(SessionCreateParams.Mode.PAYMENT)
|
||||
.build();
|
||||
try {
|
||||
Session session = Session.create(params);
|
||||
public ResponseEntity<SessionIdDto> checkout(@RequestBody @Valid AmountDto amountDto) throws StripeException {
|
||||
Stripe.apiKey = stripeKey;
|
||||
|
||||
return ResponseEntity.ok(new SessionIdDto(session.getId()));
|
||||
} catch (StripeException e) {
|
||||
return ResponseEntity.ok(e);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
return ResponseEntity.ok(e);
|
||||
}
|
||||
SessionCreateParams params = SessionCreateParams.builder()
|
||||
.addLineItem(SessionCreateParams.LineItem.builder()
|
||||
.setAmount((long) amountDto.getAmount() * 100)
|
||||
.setCurrency("EUR")
|
||||
.setQuantity(1L)
|
||||
.setName("Einzahlung")
|
||||
.build())
|
||||
.setSuccessUrl("http://localhost:8080/deposit/success")
|
||||
.setMode(SessionCreateParams.Mode.PAYMENT)
|
||||
.build();
|
||||
|
||||
Session session = Session.create(params);
|
||||
|
||||
return ResponseEntity.ok(new SessionIdDto(session.getId()));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,21 +1,17 @@
|
|||
package de.szut.casino.deposit.dto;
|
||||
|
||||
import jakarta.validation.constraints.Min;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
@Setter
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
public class AmountDto {
|
||||
@Min(50)
|
||||
private double amount;
|
||||
|
||||
public AmountDto() {
|
||||
}
|
||||
|
||||
public AmountDto(double amount) {
|
||||
this.amount = amount;
|
||||
}
|
||||
|
||||
public double getAmount() {
|
||||
return amount;
|
||||
}
|
||||
|
||||
public void setAmount(double amount) {
|
||||
this.amount = amount;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -3,7 +3,7 @@ spring.datasource.username=postgres_user
|
|||
spring.datasource.password=postgres_pass
|
||||
server.port=8080
|
||||
spring.jpa.hibernate.ddl-auto=create-drop
|
||||
|
||||
stripe.secret.key=sk_test_51QrePYIvCfqz7ANgqam8rEwWcMeKiLOof3j6SCMgu2sl4sESP45DJxca16mWcYo1sQaiBv32CMR6Z4AAAGQPCJo300ubuZKO8I
|
||||
|
||||
spring.application.name=lf12_starter
|
||||
#client registration configuration
|
||||
|
|
|
@ -1,14 +1,5 @@
|
|||
<form [formGroup]="form">
|
||||
<input type="number" formControlName="amount">€
|
||||
<br>
|
||||
<br>
|
||||
<br>
|
||||
<br>
|
||||
<br>
|
||||
<br>
|
||||
<br>
|
||||
<br>
|
||||
<br>
|
||||
<br>
|
||||
<button type="button" (click)="submit()">Einzahlen</button>
|
||||
</form>
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import { Component, OnInit } from '@angular/core';
|
||||
import { Component, inject, OnInit } from '@angular/core';
|
||||
import { FormControl, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
|
||||
import { loadStripe, Stripe } from '@stripe/stripe-js';
|
||||
import { DepositService } from '../service/deposit.service';
|
||||
|
||||
@Component({
|
||||
selector: 'app-deposit',
|
||||
|
@ -13,19 +14,18 @@ import { loadStripe, Stripe } from '@stripe/stripe-js';
|
|||
})
|
||||
export class DepositComponent implements OnInit{
|
||||
protected form = new FormGroup({amount: new FormControl(50, [Validators.min(50)])});
|
||||
private stripe: Stripe | null;
|
||||
private stripe: Stripe | null = null;
|
||||
private service: DepositService = inject(DepositService);
|
||||
|
||||
async ngOnInit() {
|
||||
this.stripe = await loadStripe('pk_test_51');
|
||||
this.stripe = await loadStripe('pk_test_51QrePYIvCfqz7ANgMizBorPpVjJ8S6gcaL4yvcMQnVaKyReqcQ6jqaQEF7aDZbDu8rNVsTZrw8ABek4ToxQX7KZe00jpGh8naG');
|
||||
}
|
||||
|
||||
submit() {
|
||||
if (this.stripe) {
|
||||
fetch('/backend/deposit/checkout', {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(this.form.value),
|
||||
}).then(response => response.json()).then(response => {
|
||||
this.stripe?.redirectToCheckout({sessionId: response.sessionId});
|
||||
console.log(JSON.stringify(this.form.value.amount as number));
|
||||
this.service.handleDeposit(this.form.value.amount as number).subscribe(({sessionId}) => {
|
||||
this.stripe?.redirectToCheckout({sessionId});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
15
frontend/src/app/service/deposit.service.ts
Normal file
15
frontend/src/app/service/deposit.service.ts
Normal file
|
@ -0,0 +1,15 @@
|
|||
import { inject, Injectable } from '@angular/core';
|
||||
import { HttpClient } from '@angular/common/http';
|
||||
import { Observable } from 'rxjs';
|
||||
|
||||
|
||||
@Injectable({
|
||||
providedIn: 'root'
|
||||
})
|
||||
export class DepositService {
|
||||
private http: HttpClient = inject(HttpClient);
|
||||
|
||||
handleDeposit(amount: number): Observable<{ sessionId: string }> {
|
||||
return this.http.post<{sessionId: string}>('/backend/deposit/checkout', {amount});
|
||||
}
|
||||
}
|
Reference in a new issue