make stripe functional
This commit is contained in:
parent
177dd78592
commit
f7a3e76967
@ -8,6 +8,8 @@ import com.stripe.param.PriceCreateParams;
|
|||||||
import com.stripe.param.checkout.SessionCreateParams;
|
import com.stripe.param.checkout.SessionCreateParams;
|
||||||
import de.szut.casino.deposit.dto.AmountDto;
|
import de.szut.casino.deposit.dto.AmountDto;
|
||||||
import de.szut.casino.deposit.dto.SessionIdDto;
|
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.boot.autoconfigure.cassandra.CassandraProperties;
|
||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.web.bind.annotation.PostMapping;
|
import org.springframework.web.bind.annotation.PostMapping;
|
||||||
@ -17,32 +19,28 @@ import org.springframework.web.bind.annotation.RestController;
|
|||||||
|
|
||||||
@RestController
|
@RestController
|
||||||
public class DepositController {
|
public class DepositController {
|
||||||
|
|
||||||
|
@Value("${stripe.secret.key}")
|
||||||
|
private String stripeKey;
|
||||||
|
|
||||||
@PostMapping("/deposit/checkout")
|
@PostMapping("/deposit/checkout")
|
||||||
public ResponseEntity<? extends Object> checkout(@RequestBody AmountDto amountDto) {
|
public ResponseEntity<SessionIdDto> checkout(@RequestBody @Valid AmountDto amountDto) throws StripeException {
|
||||||
Stripe.apiKey = "sk_test_51QrePYIvCfqz7ANgqam8rEwWcMeKiLOof3j6SCMgu2sl4sESP45DJxca16mWcYo1sQaiBv32CMR6Z4AAAGQPCJo300ubuZKO8I";
|
Stripe.apiKey = stripeKey;
|
||||||
try {
|
|
||||||
SessionCreateParams params = SessionCreateParams.builder()
|
SessionCreateParams params = SessionCreateParams.builder()
|
||||||
.addLineItem(SessionCreateParams.LineItem.builder()
|
.addLineItem(SessionCreateParams.LineItem.builder()
|
||||||
.setPriceData(InvoiceItemCreateParams.PriceData.builder()
|
.setAmount((long) amountDto.getAmount() * 100)
|
||||||
.setCurrency("EUR")
|
.setCurrency("EUR")
|
||||||
.setUnitAmount(1L)
|
.setQuantity(1L)
|
||||||
.build()
|
.setName("Einzahlung")
|
||||||
)
|
|
||||||
.build())
|
.build())
|
||||||
.setSuccessUrl("http://localhost:8080/deposit/success")
|
.setSuccessUrl("http://localhost:8080/deposit/success")
|
||||||
.setMode(SessionCreateParams.Mode.PAYMENT)
|
.setMode(SessionCreateParams.Mode.PAYMENT)
|
||||||
.build();
|
.build();
|
||||||
try {
|
|
||||||
Session session = Session.create(params);
|
Session session = Session.create(params);
|
||||||
|
|
||||||
return ResponseEntity.ok(new SessionIdDto(session.getId()));
|
return ResponseEntity.ok(new SessionIdDto(session.getId()));
|
||||||
} catch (StripeException e) {
|
|
||||||
return ResponseEntity.ok(e);
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
|
||||||
return ResponseEntity.ok(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,21 +1,17 @@
|
|||||||
package de.szut.casino.deposit.dto;
|
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 {
|
public class AmountDto {
|
||||||
|
@Min(50)
|
||||||
private double amount;
|
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
|
spring.datasource.password=postgres_pass
|
||||||
server.port=8080
|
server.port=8080
|
||||||
spring.jpa.hibernate.ddl-auto=create-drop
|
spring.jpa.hibernate.ddl-auto=create-drop
|
||||||
|
stripe.secret.key=sk_test_51QrePYIvCfqz7ANgqam8rEwWcMeKiLOof3j6SCMgu2sl4sESP45DJxca16mWcYo1sQaiBv32CMR6Z4AAAGQPCJo300ubuZKO8I
|
||||||
|
|
||||||
spring.application.name=lf12_starter
|
spring.application.name=lf12_starter
|
||||||
#client registration configuration
|
#client registration configuration
|
||||||
|
@ -1,14 +1,5 @@
|
|||||||
<form [formGroup]="form">
|
<form [formGroup]="form">
|
||||||
<input type="number" formControlName="amount">€
|
<input type="number" formControlName="amount">€
|
||||||
<br>
|
<br>
|
||||||
<br>
|
<button type="button" class="" (click)="submit()">Einzahlen</button>
|
||||||
<br>
|
|
||||||
<br>
|
|
||||||
<br>
|
|
||||||
<br>
|
|
||||||
<br>
|
|
||||||
<br>
|
|
||||||
<br>
|
|
||||||
<br>
|
|
||||||
<button type="button" (click)="submit()">Einzahlen</button>
|
|
||||||
</form>
|
</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 { FormControl, FormGroup, ReactiveFormsModule, Validators } from '@angular/forms';
|
||||||
import { loadStripe, Stripe } from '@stripe/stripe-js';
|
import { loadStripe, Stripe } from '@stripe/stripe-js';
|
||||||
|
import { DepositService } from '../service/deposit.service';
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'app-deposit',
|
selector: 'app-deposit',
|
||||||
@ -13,19 +14,18 @@ import { loadStripe, Stripe } from '@stripe/stripe-js';
|
|||||||
})
|
})
|
||||||
export class DepositComponent implements OnInit{
|
export class DepositComponent implements OnInit{
|
||||||
protected form = new FormGroup({amount: new FormControl(50, [Validators.min(50)])});
|
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() {
|
async ngOnInit() {
|
||||||
this.stripe = await loadStripe('pk_test_51');
|
this.stripe = await loadStripe('pk_test_51QrePYIvCfqz7ANgMizBorPpVjJ8S6gcaL4yvcMQnVaKyReqcQ6jqaQEF7aDZbDu8rNVsTZrw8ABek4ToxQX7KZe00jpGh8naG');
|
||||||
}
|
}
|
||||||
|
|
||||||
submit() {
|
submit() {
|
||||||
if (this.stripe) {
|
if (this.stripe) {
|
||||||
fetch('/backend/deposit/checkout', {
|
console.log(JSON.stringify(this.form.value.amount as number));
|
||||||
method: 'POST',
|
this.service.handleDeposit(this.form.value.amount as number).subscribe(({sessionId}) => {
|
||||||
body: JSON.stringify(this.form.value),
|
this.stripe?.redirectToCheckout({sessionId});
|
||||||
}).then(response => response.json()).then(response => {
|
|
||||||
this.stripe?.redirectToCheckout({sessionId: response.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});
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user