-
-
Notifications
You must be signed in to change notification settings - Fork 356
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
233 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package alfio.manager; | ||
|
||
import alfio.manager.support.OrderSummary; | ||
import alfio.manager.support.SummaryRow; | ||
import alfio.manager.system.ConfigurationManager; | ||
import alfio.model.Event; | ||
import alfio.model.system.Configuration; | ||
import alfio.model.system.ConfigurationKeys; | ||
import com.paypal.api.payments.*; | ||
import com.paypal.base.rest.APIContext; | ||
|
||
import com.paypal.base.rest.PayPalRESTException; | ||
import lombok.extern.log4j.Log4j2; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.*; | ||
import java.util.stream.Collectors; | ||
|
||
@Component | ||
@Log4j2 | ||
public class PaypalManager { | ||
|
||
private final ConfigurationManager configurationManager; | ||
|
||
@Autowired | ||
public PaypalManager(ConfigurationManager configurationManager) { | ||
this.configurationManager = configurationManager; | ||
} | ||
|
||
private APIContext getApiContext(Event event) { | ||
int orgId = event.getOrganizationId(); | ||
boolean isLive = configurationManager.getBooleanConfigValue(Configuration.from(orgId, ConfigurationKeys.PAYPAL_LIVE_MODE), false); | ||
String clientId = configurationManager.getRequiredValue(Configuration.from(orgId, ConfigurationKeys.PAYPAL_CLIENT_ID)); | ||
String clientSecret = configurationManager.getRequiredValue(Configuration.from(orgId, ConfigurationKeys.PAYPAL_CLIENT_SECRET)); | ||
return new APIContext(clientId, clientSecret, isLive ? "live" : "sandbox"); | ||
} | ||
|
||
private List<Transaction> buildPaymentDetails(Event event, OrderSummary orderSummary) { | ||
|
||
|
||
Amount amount = new Amount(); | ||
amount.setCurrency(event.getCurrency()); | ||
amount.setTotal(orderSummary.getTotalPrice()); | ||
|
||
Transaction transaction = new Transaction(); | ||
transaction.setDescription("creating a payment"); | ||
transaction.setAmount(amount); | ||
|
||
List<Item> items = orderSummary.getSummary().stream().map(summaryRow -> new Item(summaryRow.getName(), Integer.toString(summaryRow.getAmount()), summaryRow.getType() == SummaryRow.SummaryType.PROMOTION_CODE ? summaryRow.getSubTotal() : summaryRow.getPrice(), event.getCurrency())).collect(Collectors.toList()); | ||
|
||
if(!event.isVatIncluded()) { | ||
items.add(new Item("VAT", "1", orderSummary.getTotalVAT(), event.getCurrency())); | ||
} | ||
|
||
transaction.setItemList(new ItemList().setItems(items)); | ||
|
||
|
||
|
||
List<Transaction> transactions = new ArrayList<>(); | ||
transactions.add(transaction); | ||
return transactions; | ||
} | ||
|
||
public String createCheckoutRequest(Event event, String reservationId, OrderSummary orderSummary, Locale locale) throws PayPalRESTException { | ||
|
||
List<Transaction> transactions = buildPaymentDetails(event, orderSummary); | ||
String eventName = event.getShortName(); | ||
|
||
|
||
Payer payer = new Payer(); | ||
payer.setPaymentMethod("paypal"); | ||
|
||
Payment payment = new Payment(); | ||
payment.setIntent("sale"); | ||
payment.setPayer(payer); | ||
payment.setTransactions(transactions); | ||
RedirectUrls redirectUrls = new RedirectUrls(); | ||
redirectUrls.setCancelUrl("http://localhost:8080/event/" + eventName + "/reservation/" + reservationId + "/book?paypal-cancel=true"); | ||
redirectUrls.setReturnUrl("http://localhost:8080/event/" + eventName + "/reservation/" + reservationId + "/book?paypal-success=true"); | ||
payment.setRedirectUrls(redirectUrls); | ||
|
||
Payment createdPayment = payment.create(getApiContext(event)); | ||
|
||
//extract url for approval | ||
return createdPayment.getLinks().stream().filter(l -> "approval_url".equals(l.getRel())).findFirst().map(Links::getHref).orElseThrow(IllegalStateException::new); | ||
|
||
} | ||
|
||
public String commitPayment(String token, String payerId, Event event) throws Exception { | ||
|
||
Payment payment = new Payment(); | ||
payment.setId(token); | ||
PaymentExecution paymentExecute = new PaymentExecution(); | ||
paymentExecute.setPayerId(payerId); | ||
Payment result = payment.execute(getApiContext(event), paymentExecute); | ||
|
||
//return result.getTransactions().get(0).getRelatedResources().get(0).getSale().getId(); | ||
//FIXME | ||
return result.getId(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,7 +18,7 @@ | |
INSERT INTO organization(name, description, email) VALUES ('demo', 'demo organization', '[email protected]'); | ||
|
||
insert into event(short_name, website_url, website_t_c_url, location, latitude, longitude, start_ts, end_ts, regular_price_cts, currency, available_seats, vat_included, vat, allowed_payment_proxies, private_key, org_id, time_zone, image_url) | ||
values('eventname', 'http://localhost:8080', 'http://localhost:8080', 'demo location', '0', '0', '2016-10-10 04:00:00' , '2016-10-11 03:59:00' , 1000, 'CHF', 20, 'true', 8, 'STRIPE,ON_SITE,OFFLINE', 'alfio-uberall', 0, 'America/New_York', 'http://localhost:8080/resources/images/sample-logo.png'); | ||
values('eventname', 'http://localhost:8080', 'http://localhost:8080', 'demo location', '0', '0', '2016-10-10 04:00:00' , '2016-10-11 03:59:00' , 1000, 'CHF', 20, 'true', 8, 'STRIPE,ON_SITE,OFFLINE,PAYPAL', 'alfio-uberall', 0, 'America/New_York', 'http://localhost:8080/resources/images/sample-logo.png'); | ||
|
||
insert into ticket_category(inception, expiration, name, max_tickets, price_cts, access_restricted, tc_status, event_id, bounded) values | ||
('2014-01-10 00:00:00', '2016-10-10 00:00:00', 'Normal', 2, 0, false, 'ACTIVE', 0, true), | ||
|
@@ -77,7 +77,10 @@ insert into configuration (c_key, c_value, description) values | |
('STRIPE_SECRET_KEY', 'sk_test_cayJOFUUYF9cWOoMXemJd61Z', 'Stripe''s secret key'), | ||
('STRIPE_PUBLIC_KEY', 'pk_test_gY3X0UiTgKCeStUG67i2kEFq', 'Stripe''s public key'), | ||
('BASE_URL', 'http://localhost:8080/', 'Base application url'), | ||
('SUPPORTED_LANGUAGES', '7', 'supported languages'); | ||
('SUPPORTED_LANGUAGES', '7', 'supported languages'), | ||
('PAYPAL_CLIENT_ID', 'AQkquBDf1zctJOWGKWUEtKXm6qVhueUEMvXO_-MCI4DQQ4-LWvkDLIN2fGsd', 'Paypal REST API client ID'), | ||
('PAYPAL_CLIENT_SECRET','EL1tVxAjhT7cJimnz5-Nsx9k2reTKSVfErNQF-CmrwJgxRtylkGTKlU4RvrX', 'Paypal REST API client secret'), | ||
('PAYPAL_LIVE_MODE', 'false', 'Enable live mode for Paypal'); | ||
|
||
|
||
-- create fields configuration | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.