Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package org.airpenthouse.GoTel.controllers;

import com.stripe.exception.StripeException;
import com.stripe.model.Customer;
import org.airpenthouse.GoTel.dtos.sponsors.SponsorsDonateRequest;
import org.airpenthouse.GoTel.dtos.sponsors.SponsorsRequest;
import org.airpenthouse.GoTel.services.sponsors.SponsorsService;
import org.airpenthouse.GoTel.util.PropertiesUtilManager;
import org.airpenthouse.GoTel.util.executors.SponsorsExecutors;
import org.airpenthouse.GoTel.util.mappers.SponsorsMapper;
import org.airpenthouse.GoTel.util.payments.SponsorsDonateUsingStripe;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
Expand All @@ -15,28 +18,49 @@

@RequestMapping("/api/sponsors")
@RestController
@CrossOrigin(origins="http://localhost:4200")
@CrossOrigin(origins = "http://localhost:4200")
public class SponsorsController {

@Autowired
public SponsorsMapper mapper;
@Autowired
public SponsorsService service;
@Autowired
public SponsorsDonateUsingStripe stripeApi;

@PostMapping("/sponsorDonote")
@PostMapping("/sponsorDonate")
public ResponseEntity<List<SponsorsRequest>> sponsorsDonate(@RequestBody SponsorsDonateRequest request, UriComponentsBuilder builder) {
var entity = mapper.toEntity(request);
SponsorsExecutors.setMapper(mapper);
SponsorsService.serviceHandle = "DONATE";
Customer customerObj;
try {
customerObj = stripeApi.setCustomer(entity.getSponsorName(), entity.getSponsorSurname(), entity.getSponsorEmailAddress(), entity.getSponsorCellphoneNumber());
entity.setId(customerObj.getId());
} catch (StripeException e) {
throw new RuntimeException(e);
}

var entity2 = service.initializeSponsorsService(false, entity);
if (entity2 == null) {
return ResponseEntity.badRequest().build();
} else if (entity2.isEmpty()) {
return ResponseEntity.notFound().build();
} else {
var uri = builder.path("/api/sponsors/getSponsorByName/{sponsorName}").buildAndExpand(entity.getSponsorName()).toUri();
return ResponseEntity.created(uri).body(entity2);
try {
stripeApi.setPaymentMethod(customerObj, entity.getCardNumber(), entity.getCvv(), Long.parseLong(entity.getCardDate()), Long.parseLong(entity.getCvv()));
var payment = stripeApi.sponsorDonate(customerObj, entity.getDononationAmount(), "used");

if (payment) {
var uri = builder.path("/api/sponsors/getSponsorByName/{sponsorName}").buildAndExpand(entity.getSponsorName()).toUri();
return ResponseEntity.created(uri).body(entity2);
}else {
throw new RuntimeException();
}
} catch (StripeException | RuntimeException e) {
throw new RuntimeException(e);
}

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.airpenthouse.GoTel.util.PropertiesUtilManager;
import org.airpenthouse.GoTel.util.executors.SponsorsExecutors;

Expand All @@ -13,17 +15,19 @@
import java.util.List;
import java.util.concurrent.*;

@ToString
@AllArgsConstructor
public class SponsorEntity extends SponsorsExecutors implements Callable<List<SponsorEntity>> {

@Getter
private String sponsorName, SponsorSurname, sponsorEmailAddress, sponsorCellphoneNumber,
sponsorReasonToDonate;

@Setter
private String id;
@Getter
private String cardNumber, cvv, cardDate;
@Getter
private Double dononationAmount;
private Long dononationAmount;

@Getter
private LocalDateTime paymentDate;
Expand All @@ -49,7 +53,7 @@ private SponsorEntity(String sponsorName
, String cardNumber
, String cvv
, String cardDate
, Double dononationAmount
, Long dononationAmount
, LocalDateTime paymentDate) {
this.sponsorName = sponsorName;
SponsorSurname = sponsorSurname;
Expand Down Expand Up @@ -132,7 +136,7 @@ private List<SponsorEntity> resultSetData(ResultSet set) throws SQLException {
, set.getString(6)
, set.getString(7)
, set.getString(8)
, set.getDouble(9)
, set.getLong(9)
, LocalDateTime.parse(set.getString(10))));
}
return entities;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package org.airpenthouse.GoTel.util.payments;

import com.stripe.exception.StripeException;
import com.stripe.model.Charge;
import com.stripe.model.Customer;
import com.stripe.model.PaymentMethod;
import com.stripe.net.RequestOptions;
import com.stripe.param.ChargeCreateParams;
import com.stripe.param.CustomerCreateParams;
import com.stripe.param.PaymentMethodCreateParams;
import org.springframework.stereotype.Component;

@Component
public class SponsorsDonateUsingStripe {

RequestOptions requestOptions;
public SponsorsDonateUsingStripe() {
requestOptions = RequestOptions.builder()
.setApiKey("key")
.build();
}

public Customer setCustomer(String sponsorName
, String sponsorSurname
, String sponsorEmailAddress
, String sponsorCellphoneNumber)
throws StripeException {
CustomerCreateParams params =
CustomerCreateParams.builder()
.setName(sponsorName + " " + sponsorSurname)
.setEmail(sponsorEmailAddress)
.setPhone(sponsorCellphoneNumber)
.build();
return Customer.create(params,requestOptions);
}

public PaymentMethod setPaymentMethod(Customer customer
, String sponsorCardNumber
, String sponsorCvc
, Long expMonth
, Long expYear)
throws StripeException {

PaymentMethodCreateParams params =
PaymentMethodCreateParams.builder()
.setType(PaymentMethodCreateParams.Type.CARD)
.setCustomer(customer.getId())
.setCard(PaymentMethodCreateParams
.CardDetails
.builder()
.setNumber(sponsorCardNumber)
.setCvc(sponsorCvc)
.setExpMonth(expMonth)
.setExpYear(expYear)
.build())
.setBillingDetails(
PaymentMethodCreateParams.BillingDetails.builder().setName("John Doe").build()
)
.build();

return PaymentMethod.create(params,requestOptions);
}

public boolean sponsorDonate(Customer customer
, Long amount
, String currency)
throws StripeException {
ChargeCreateParams params =
ChargeCreateParams.builder()
.setCustomer(customer.getId())
.setAmount(amount)
.setCurrency(currency)
.setSource("tok_visa")
.build();
Charge charge = Charge.create(params,requestOptions);
return charge.getPaid();
}


}
Loading