Skip to content

Commit

Permalink
CreditCard Payment backend
Browse files Browse the repository at this point in the history
  • Loading branch information
Gebreyowhans committed Jan 19, 2022
1 parent 04448d7 commit 5f09b7c
Show file tree
Hide file tree
Showing 12 changed files with 204 additions and 14 deletions.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@
<version>LATEST</version>
</dependency>

<dependency>
<groupId>com.stripe</groupId>
<artifactId>stripe-java</artifactId>
<version>20.94.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/org.springframework.security/spring-security-core -->
<dependency>
<groupId>org.springframework.security</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package asd.vinted.Controller;

import asd.vinted.data.dto.OrderDto;
import asd.vinted.data.entity.Order;
import org.springframework.beans.factory.annotation.Autowired;
import asd.vinted.data.payment.CreditCardPaymentResponse;
import asd.vinted.data.service.CreditCardPaymentService;
import asd.vinted.data.service.PaypalPaymentService;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/")
@CrossOrigin(origins = "http://localhost:4200")
public class CreditCardPaymentController{
@Autowired
CreditCardPaymentService ccPaymentService;

//CommonConstant.PAYMENT_CREATE_STRIPE
@PostMapping("/CreditCard")
public ResponseEntity<CreditCardPaymentResponse> stripePaymentCreate(@RequestBody OrderDto createPayment){
return ResponseEntity.ok().body(ccPaymentService.CreatCreditCardPayment(createPayment));
}
}
11 changes: 4 additions & 7 deletions src/main/java/asd/vinted/Controller/PayPalPaymentController.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
package asd.vinted.Controller;

import asd.vinted.data.dto.OrderDto;
import asd.vinted.data.entity.Order;
import asd.vinted.data.payment.PayPalConfirmPaymentRequest;
import asd.vinted.data.payment.PayPalConfirmPaymentResponse;
import asd.vinted.data.payment.PayPalPaymentResponse;
import asd.vinted.data.service.PaypalOrderService;
import asd.vinted.data.service.PaypalPaymentService;
import com.paypal.api.payments.Links;
import com.paypal.api.payments.Payment;
import com.paypal.base.rest.PayPalRESTException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

Expand All @@ -20,7 +17,7 @@
public class PayPalPaymentController {

@Autowired
PaypalOrderService paypalOrderService;
PaypalPaymentService paypalService;

public static final String SUCCESS_URL = "pay/success";
public static final String CANCEL_URL = "pay/cancel";
Expand All @@ -37,7 +34,7 @@ public ResponseEntity<PayPalPaymentResponse> payment(@RequestBody OrderDto _orde
String message ="";
//@RequestBody ProfileDetailsDto profileDetail
try {
Payment payment = paypalOrderService.createPayment(_order, "http://localhost:8080/" + CANCEL_URL,
Payment payment = paypalService.createPayment(_order, "http://localhost:8080/" + CANCEL_URL,
"http://localhost:8080/" + SUCCESS_URL);

// System.out.println(payment);
Expand Down Expand Up @@ -71,7 +68,7 @@ public ResponseEntity<PayPalConfirmPaymentResponse> successPay(@RequestParam("pa
double[] amount = {0.0};
PayPalConfirmPaymentResponse response=null;
try {
Payment payment = paypalOrderService.executePayment(paymentId,payerId);
Payment payment = paypalService.executePayment(paymentId,payerId);
System.out.println(payment.toJSON());
if (payment.getState().equals("approved")) {
response = new PayPalConfirmPaymentResponse();
Expand Down
30 changes: 28 additions & 2 deletions src/main/java/asd/vinted/data/dto/OrderDto.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ public class OrderDto {
private String intent;
private String cancelURL;
private String successURL;
@Column
private String stripeToken;
@Column
private String stripeEmail;


public String getCancelURL() {
return cancelURL;
Expand Down Expand Up @@ -64,17 +69,33 @@ public OrderDto(Long id, double price, String currency, String method, String de

public String getIntent() {return intent;}public void setIntent(String intent) {this.intent = intent;}

public String getStripeToken() {
return stripeToken;
}

public void setStripeToken(String stripeToken) {
this.stripeToken = stripeToken;
}

public String getStripeEmail() {
return stripeEmail;
}

public void setStripeEmail(String stripeEmail) {
this.stripeEmail = stripeEmail;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
OrderDto orderDto = (OrderDto) o;
return Double.compare(orderDto.price, price) == 0 && Objects.equals(id, orderDto.id) && Objects.equals(currency, orderDto.currency) && Objects.equals(method, orderDto.method) && Objects.equals(description, orderDto.description) && Objects.equals(intent, orderDto.intent);
return Double.compare(orderDto.price, price) == 0 && Objects.equals(id, orderDto.id) && Objects.equals(currency, orderDto.currency) && Objects.equals(method, orderDto.method) && Objects.equals(description, orderDto.description) && Objects.equals(intent, orderDto.intent) && Objects.equals(cancelURL, orderDto.cancelURL) && Objects.equals(successURL, orderDto.successURL) && Objects.equals(stripeToken, orderDto.stripeToken) && Objects.equals(stripeEmail, orderDto.stripeEmail);
}

@Override
public int hashCode() {
return Objects.hash(id, price, currency, method, description, intent);
return Objects.hash(id, price, currency, method, description, intent, cancelURL, successURL, stripeToken, stripeEmail);
}

@Override
Expand All @@ -86,6 +107,11 @@ public String toString() {
", method='" + method + '\'' +
", description='" + description + '\'' +
", intent='" + intent + '\'' +
", cancelURL='" + cancelURL + '\'' +
", successURL='" + successURL + '\'' +
", stripeToken='" + stripeToken + '\'' +
", stripeEmail='" + stripeEmail + '\'' +
'}';
}

}
23 changes: 23 additions & 0 deletions src/main/java/asd/vinted/data/entity/Order.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ public class Order {
@Column
private String intent;

@Column
private String stripeToken;
@Column
private String stripeEmail;


public Order() {
}

Expand Down Expand Up @@ -97,4 +103,21 @@ public boolean equals(Object o) {
public int hashCode() {
return Objects.hash(id, price, currency, method, description, intent);
}


public String getStripeToken() {
return stripeToken;
}

public void setStripeToken(String stripeToken) {
this.stripeToken = stripeToken;
}

public String getStripeEmail() {
return stripeEmail;
}

public void setStripeEmail(String stripeEmail) {
this.stripeEmail = stripeEmail;
}
}
19 changes: 19 additions & 0 deletions src/main/java/asd/vinted/data/payment/CreditCardConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package asd.vinted.data.payment;

import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

@Configuration
public class CreditCardConfig {
@Value("${Stripe.apiKey}")
private String apiKey;

public String getApiKey() {
return apiKey;
}

public void setApiKey(String apiKey) {
this.apiKey = apiKey;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package asd.vinted.data.payment;

public class CreditCardPaymentResponse {
private String status;
private Long amount;
private String paymentID;

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}

public Long getAmount() {
return amount;
}

public void setAmount(Long amount) {
this.amount = amount;
}

public String getPaymentID() {
return paymentID;
}

public void setPaymentID(String paymentID) {
this.paymentID = paymentID;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package asd.vinted.data.service;

import asd.vinted.data.dto.OrderDto;
import asd.vinted.data.payment.CreditCardPaymentResponse;
import org.springframework.stereotype.Service;

@Service
public interface CreditCardPaymentService {

public CreditCardPaymentResponse CreatCreditCardPayment(OrderDto createPayment);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.springframework.stereotype.Service;

@Service
public interface PaypalOrderService {
public interface PaypalPaymentService {

public Payment createPayment(OrderDto _order, String cancelUrl, String successUrl) throws PayPalRESTException;
public Payment executePayment(String paymentId, String payerId) throws PayPalRESTException;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package asd.vinted.data.service.impl;

import asd.vinted.data.dto.OrderDto;
import asd.vinted.data.payment.CreditCardConfig;
import asd.vinted.data.payment.CreditCardPaymentResponse;
import asd.vinted.data.service.CreditCardPaymentService;
import com.stripe.Stripe;
import com.stripe.model.Charge;
import com.stripe.param.ChargeCreateParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

@Service
public class CreditCardPaymentServiceImpl implements CreditCardPaymentService {
@Autowired
private CreditCardConfig config;

@Override
public CreditCardPaymentResponse CreatCreditCardPayment(OrderDto createPayment) {

CreditCardPaymentResponse response = null;
try {
//Stripe.apiKey = config.getApiKey();
Stripe.apiKey = "sk_test_51KJQYrGYh3JcLLFyylP8w7TExdqsgiuPvCHt3LUi5m1ZTpotdGHQZpLLkmddpl1QEOOuE2uXvFkFRfZmE087K7uS00oAYjE0Ef";

ChargeCreateParams params =
ChargeCreateParams.builder()
.setAmount((long) (createPayment.getPrice()*100L))
.setCurrency("EUR")
.setDescription(createPayment.getDescription())
.setSource(createPayment.getStripeToken())
.build();

Charge charge = Charge.create(params);
if (charge.getStatus().equals("succeeded")){
response = new CreditCardPaymentResponse();
response.setStatus(charge.getStatus());
response.setAmount(charge.getAmount()/100);
response.setPaymentID(charge.getId());
return response;
}

}catch (Exception ex){
System.out.println(ex.getMessage());
}
return response;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package asd.vinted.data.service.impl;

import asd.vinted.data.dto.OrderDto;
import asd.vinted.data.service.PaypalOrderService;
import asd.vinted.data.service.PaypalPaymentService;
import com.paypal.api.payments.*;
import com.paypal.base.rest.APIContext;
import com.paypal.base.rest.PayPalRESTException;
Expand All @@ -14,7 +14,7 @@
import java.util.List;

@Service
public class paypalOrderServiceImpl implements PaypalOrderService {
public class PaypalPaymentServiceImpl implements PaypalPaymentService {
@Autowired
private APIContext apiContext;

Expand Down
7 changes: 5 additions & 2 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

spring.datasource.url=jdbc:mysql://localhost:3306/vinted?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=01123
spring.datasource.password=

# spring.datasource.url=jdbc:postgresql://localhost:5432/vinted
# # ?serverTimezone=UTC
Expand All @@ -18,4 +18,7 @@ spring.jpa.properties..hibernate.format_sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
paypal.mode=sandbox
paypal.client.id=ASIYmAxKN_jCVLhQ3VmHo-90Ai9p9YlQh3HISF-7ysHbwLLD0_dOqI5Ddho3DcPX1GC9WOf126ziqkHu
paypal.client.secret=ENUG37r3zbV9ByIBGCHSMvwToa29vjSuwjq8T9S68DrDWYg_pIQ5UPaVshT2Zb8EDiYM9ASuU55elYCd
paypal.client.secret=ENUG37r3zbV9ByIBGCHSMvwToa29vjSuwjq8T9S68DrDWYg_pIQ5UPaVshT2Zb8EDiYM9ASuU55elYCd

Stripe.apiKey = "sk_test_51KJQYrGYh3JcLLFyylP8w7TExdqsgiuPvCHt3LUi5m1ZTpotdGHQZpLLkmddpl1QEOOuE2uXvFkFRfZmE087K7uS00oAYjE0Ef";

0 comments on commit 5f09b7c

Please sign in to comment.