use of com.google.api.server.spi.config.ApiMethod in project cryptonomica by Cryptonomica.
the class StripePaymentsAPI method processStripePayment.
@ApiMethod(name = "processStripePayment", path = "processStripePayment", httpMethod = ApiMethod.HttpMethod.POST)
@SuppressWarnings("unused")
public StripePaymentReturn processStripePayment(final HttpServletRequest httpServletRequest, final User googleUser, final StripePaymentForm stripePaymentForm) throws Exception {
// Set your secret key: remember to change this to your live secret key in production
// See your keys here: https://dashboard.stripe.com/account/apikeys
// final String STRIPE_SECRET_KEY = ApiKeysUtils.getApiKey("StripeTestSecretKey");
final String STRIPE_SECRET_KEY = ApiKeysUtils.getApiKey("StripeLiveSecretKey");
/* --- Ensure cryptonomica registered user */
final CryptonomicaUser cryptonomicaUser = UserTools.ensureCryptonomicaRegisteredUser(googleUser);
/* --- record user login */
Login login = UserTools.registerLogin(httpServletRequest, googleUser);
LOG.warning(GSON.toJson(new LoginView(login)));
// log info:
LOG.warning("cryptonomicaUser: " + cryptonomicaUser.getEmail().getEmail());
LOG.warning("key fingerpint: " + stripePaymentForm.getFingerprint());
/* --- log form: */
/* !!!!!!!!!!!!!!!!!!! comment in production !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
// LOG.warning(GSON.toJson(stripePaymentForm)); // <<<<<<<<<<<<<<<<<<<<<<<!!!!!!!!!!!!!!!!!!!!!!!
// Do not save card data in DB or to logs when working with real (non-test) cards numbers !!!!!!!
/* --- create return object */
StripePaymentReturn stripePaymentReturn = new StripePaymentReturn();
/* --- get OpenPGP key data */
final String fingerprint = stripePaymentForm.getFingerprint();
PGPPublicKeyData pgpPublicKeyData = null;
pgpPublicKeyData = ofy().load().type(PGPPublicKeyData.class).filter("fingerprintStr", fingerprint).first().now();
//
if (pgpPublicKeyData == null) {
throw new Exception("OpenPGP public key certificate not found in our database for " + fingerprint);
}
if (pgpPublicKeyData.getPaid() != null && pgpPublicKeyData.getPaid()) {
// <<<
throw new Exception("Verification of key " + fingerprint + " already paid");
}
//
String nameOnCard;
if (pgpPublicKeyData.getNameOnCard() != null) {
nameOnCard = pgpPublicKeyData.getNameOnCard();
} else if (!stripePaymentForm.getCardHolderFirstName().equalsIgnoreCase(pgpPublicKeyData.getFirstName()) || !stripePaymentForm.getCardHolderLastName().equalsIgnoreCase(pgpPublicKeyData.getLastName())) {
throw new Exception("You have to pay with credit card with the same first and last name as in your OpenPGP key." + " If your card has different spelling of your name than passport," + " please write to support@cryptonomica.net " + "(include spelling of your name in passport and on the card," + " but do not send card number or CVC via email)");
} else {
nameOnCard = pgpPublicKeyData.getFirstName() + " " + pgpPublicKeyData.getLastName();
}
// make code for payment:
final String chargeCode = RandomStringUtils.randomNumeric(7);
//
LOG.warning("chargeCode: " + chargeCode);
// calculate price for key verification
Integer price = calculatePriceForKeyVerification(pgpPublicKeyData);
/* --- process payment */
// see example on:
// https://github.com/stripe/stripe-java#usage
RequestOptions requestOptions = (new RequestOptions.RequestOptionsBuilder()).setApiKey(STRIPE_SECRET_KEY).build();
// --- cardMap
Map<String, Object> cardMap = new HashMap<>();
// String
cardMap.put("number", stripePaymentForm.getCardNumber());
// Integer
cardMap.put("exp_month", stripePaymentForm.getCardExpMonth());
// Integer
cardMap.put("exp_year", stripePaymentForm.getCardExpYear());
cardMap.put("name", nameOnCard);
// --- chargeMap
Map<String, Object> chargeMap = new HashMap<>();
chargeMap.put("card", cardMap);
// amount - a positive integer in the smallest currency unit (e.g., 100 cents to charge $1.00
//
chargeMap.put("amount", price);
//
chargeMap.put("currency", "usd");
// https://stripe.com/docs/api/java#create_charge-statement_descriptor
// An arbitrary string to be displayed on your customer's credit card statement.
// This may be up to 22 characters. As an example, if your website is RunClub and the item you're charging for
// is a race ticket, you may want to specify a statement_descriptor of RunClub 5K race ticket.
// The statement description may not include <>"' characters, and will appear on your customer's statement in
// capital letters. Non-ASCII characters are automatically stripped.
// While most banks display this information consistently, some may display it incorrectly or not at all.
chargeMap.put("statement_descriptor", // 13 characters
"CRYPTONOMICA " + // 7 characters
chargeCode);
// https://stripe.com/docs/api/java#create_charge-description
// An arbitrary string which you can attach to a charge object.
// It is displayed when in the web interface alongside the charge.
// Note that if you use Stripe to send automatic email receipts to your customers,
// your receipt emails will include the description of the charge(s) that they are describing.
chargeMap.put("description", "Key " + pgpPublicKeyData.getKeyID() + " verification");
// https://stripe.com/docs/api/java#create_charge-receipt_email
// The email address to send this charge's receipt to.
// The receipt will not be sent until the charge is paid.
// If this charge is for a customer, the email address specified here will override the customer's email address.
// Receipts will not be sent for test mode charges.
// If receipt_email is specified for a charge in live mode, a receipt will be sent regardless of your email settings.
chargeMap.put("receipt_email", cryptonomicaUser.getEmail().getEmail());
// -- get Charge object:
Charge charge = Charge.create(chargeMap, requestOptions);
// Charge obj has custom toString()
String chargeStr = charge.toString();
LOG.warning("chargeStr: " + chargeStr);
String chargeJsonStr = GSON.toJson(charge);
LOG.warning("chargeJsonStr: " + chargeJsonStr);
if (charge.getStatus().equalsIgnoreCase("succeeded")) {
stripePaymentReturn.setResult(true);
stripePaymentReturn.setMessageToUser("Payment succeeded");
} else {
stripePaymentReturn.setResult(false);
stripePaymentReturn.setMessageToUser("Payment not succeeded");
}
// create and store payment information
StripePaymentForKeyVerification stripePaymentForKeyVerification = new StripePaymentForKeyVerification();
stripePaymentForKeyVerification.setChargeId(charge.getId());
stripePaymentForKeyVerification.setChargeJsonStr(chargeJsonStr);
stripePaymentForKeyVerification.setChargeStr(chargeStr);
stripePaymentForKeyVerification.setFingerprint(fingerprint);
stripePaymentForKeyVerification.setCryptonomicaUserId(cryptonomicaUser.getUserId());
stripePaymentForKeyVerification.setUserEmail(cryptonomicaUser.getEmail());
stripePaymentForKeyVerification.setPaymentVerificationCode(chargeCode);
Key<CryptonomicaUser> cryptonomicaUserKey = Key.create(CryptonomicaUser.class, googleUser.getUserId());
Key<Login> loginKey = Key.create(cryptonomicaUserKey, Login.class, login.getId());
stripePaymentForKeyVerification.setChargeLogin(loginKey);
Key<StripePaymentForKeyVerification> entityKey = ofy().save().entity(stripePaymentForKeyVerification).now();
LOG.warning("StripePaymentForKeyVerification saved: " + // has custom toString()
entityKey.toString());
// add data to OnlineVerification entity:
OnlineVerification onlineVerification = ofy().load().key(Key.create(OnlineVerification.class, fingerprint)).now();
if (onlineVerification == null) {
LOG.severe("OnlineVerification record not found in data base");
}
try {
// onlineVerification.setPaimentMade(Boolean.TRUE);
onlineVerification.setPaymentMade(Boolean.TRUE);
onlineVerification.setStripePaymentForKeyVerificationId(stripePaymentForKeyVerification.getId());
ofy().save().entity(onlineVerification).now();
} catch (Exception e) {
e.printStackTrace();
}
return stripePaymentReturn;
}
use of com.google.api.server.spi.config.ApiMethod in project cryptonomica by Cryptonomica.
the class StripePaymentsAPI method checkPaymentVerificationCode.
// end of processStripePayment
@ApiMethod(name = "checkPaymentVerificationCode", path = "checkPaymentVerificationCode", httpMethod = ApiMethod.HttpMethod.POST)
@SuppressWarnings("unused")
public StringWrapperObject checkPaymentVerificationCode(final HttpServletRequest httpServletRequest, final User googleUser, @Named("fingerprint") final String fingerprint, @Named("paymentVerificationCode") final String paymentVerificationCode) throws Exception {
/* --- Ensure cryptonomica registered user */
final CryptonomicaUser cryptonomicaUser = UserTools.ensureCryptonomicaRegisteredUser(googleUser);
/* record login: */
Login login = UserTools.registerLogin(httpServletRequest, googleUser);
/* --- Check form: */
LOG.warning("paymentVerificationCode from user: " + paymentVerificationCode);
/* --- create return object */
StringWrapperObject result = new StringWrapperObject();
/* --- get OpenPGP key data */
PGPPublicKeyData pgpPublicKeyData = null;
pgpPublicKeyData = ofy().load().type(PGPPublicKeyData.class).filter("fingerprintStr", fingerprint).first().now();
//
if (pgpPublicKeyData == null) {
throw new Exception("OpenPGP public key certificate not found in our database for " + fingerprint);
}
//
if (!pgpPublicKeyData.getUserEmail().getEmail().equalsIgnoreCase(cryptonomicaUser.getEmail().getEmail())) {
throw new Exception("Google Account email and email in key certificate does not much");
}
// first check for null:
if (pgpPublicKeyData.getPaid() != null && pgpPublicKeyData.getPaid()) {
throw new Exception("Verification of key " + fingerprint + " already paid");
}
//
StripePaymentForKeyVerification stripePaymentForKeyVerification = null;
List<StripePaymentForKeyVerification> paymentForKeyVerificationList = ofy().load().type(StripePaymentForKeyVerification.class).filter("fingerprint", fingerprint).list();
if (paymentForKeyVerificationList == null || paymentForKeyVerificationList.size() < 1) {
throw new Exception("No payments for verification of the key " + fingerprint + "found");
} else if (paymentForKeyVerificationList.size() > 1) {
throw new Exception("Multiple payments exist for the key: " + fingerprint + ", please write to support@cryptonomica.net");
} else {
stripePaymentForKeyVerification = paymentForKeyVerificationList.get(0);
}
OnlineVerification onlineVerification = ofy().load().key(Key.create(OnlineVerification.class, fingerprint)).now();
if (onlineVerification == null) {
throw new NotFoundException("OnlineVerification record not found in data base");
}
if (stripePaymentForKeyVerification.getPaymentVerificationCode().equalsIgnoreCase(paymentVerificationCode)) {
stripePaymentForKeyVerification.setVerified(true);
Key<CryptonomicaUser> cryptonomicaUserKey = Key.create(CryptonomicaUser.class, googleUser.getUserId());
Key<Login> loginKey = Key.create(cryptonomicaUserKey, Login.class, login.getId());
stripePaymentForKeyVerification.setCheckPaymentVerificationCodeLogin(loginKey);
// async
ofy().save().entity(stripePaymentForKeyVerification);
// add data to PGP Public Key and save:
pgpPublicKeyData.setPaid(true);
ofy().save().entity(pgpPublicKeyData).now();
// add data to OnlineVerification and save:
// onlineVerification.setPaimentVerified(Boolean.TRUE);
onlineVerification.setPaymentVerified(Boolean.TRUE);
// <<<<<<<<<< set as ready to manual review
onlineVerification.setOnlineVerificationFinished(Boolean.TRUE);
// async
ofy().save().entity(onlineVerification);
result.setMessage("Code verified!");
} else {
stripePaymentForKeyVerification.setFailedVerificationAttemps(stripePaymentForKeyVerification.getFailedVerificationAttemps() + 1);
if (stripePaymentForKeyVerification.getFailedVerificationAttemps() >= 5) {
throw new Exception("The number of attempts is exhausted. Please, write to support@cryptonomica.net");
} else {
throw new Exception("Code does not much. It was attempt # " + stripePaymentForKeyVerification.getFailedVerificationAttemps());
}
}
// On this step user completed entering verification data, send email to compliance:
ArrayList<VerificationDocument> verificationDocumentArrayList = new ArrayList<>();
int verificationDocumentsListSize = ofy().load().type(VerificationDocument.class).filter("fingerprint", fingerprint).filter("hidden", false).list().size();
LOG.warning("verificationDocumentsListSize: " + verificationDocumentsListSize);
if (verificationDocumentsListSize > 0) {
List<VerificationDocument> verificationDocumentList = ofy().load().type(VerificationDocument.class).filter("fingerprint", fingerprint).filter("hidden", false).list();
verificationDocumentArrayList.addAll(verificationDocumentList);
LOG.warning(GSON.toJson(verificationDocumentArrayList));
}
OnlineVerificationView onlineVerificationView = new OnlineVerificationView(onlineVerification, verificationDocumentArrayList);
Gson prettyGson = new GsonBuilder().setPrettyPrinting().create();
final Queue queue = QueueFactory.getDefaultQueue();
queue.add(TaskOptions.Builder.withUrl("/_ah/SendGridServlet").param("email", "verification@cryptonomica.net").param("messageSubject", "[verification] Request for online verification").param("messageText", "New request for online verification received: \n\n" + "see entered data on:\n" + "https://cryptonomica.net/#/onlineVerificationView/" + fingerprint + "\n\n" + "verification request data in JSON format: \n\n" + prettyGson.toJson(onlineVerificationView)));
return result;
}
use of com.google.api.server.spi.config.ApiMethod in project cryptonomica by Cryptonomica.
the class UserSearchAndViewAPI method getMyProfile.
// end of getUserProfileById @ApiMethod
@// <<< --- not used, use getMyUserData in CryptonomicaUserAPI
ApiMethod(name = "getMyProfile", path = "getMyProfile", httpMethod = ApiMethod.HttpMethod.POST)
@SuppressWarnings("unused")
public // returns list of user profiles
UserProfileGeneralView getMyProfile(final User googleUser) throws Exception {
// ensure authorization
UserTools.ensureCryptonomicaRegisteredUser(googleUser);
//
Key<CryptonomicaUser> myKey = Key.create(CryptonomicaUser.class, googleUser.getUserId());
CryptonomicaUser profile = ofy().load().key(myKey).now();
UserProfileGeneralView userProfileGeneralView = new UserProfileGeneralView(profile);
List<PGPPublicKeyData> pgpPublicKeyDataList = ofy().load().type(PGPPublicKeyData.class).ancestor(myKey).list();
ArrayList<PGPPublicKeyGeneralView> pgpPublicKeyGeneralViews = new ArrayList<>();
for (PGPPublicKeyData pgpPublicKeyData : pgpPublicKeyDataList) {
pgpPublicKeyGeneralViews.add(new PGPPublicKeyGeneralView(pgpPublicKeyData));
}
userProfileGeneralView.setPgpPublicKeyGeneralViews(pgpPublicKeyGeneralViews);
return userProfileGeneralView;
}
use of com.google.api.server.spi.config.ApiMethod in project cryptonomica by Cryptonomica.
the class UserSearchAndViewAPI method getUserProfileById.
// end of generalSearchUserProfiles @ApiMethod
@ApiMethod(name = "getUserProfileById", path = "getUserProfileById", httpMethod = ApiMethod.HttpMethod.GET)
@SuppressWarnings("unused")
public // returns list of user profiles
UserProfileGeneralView getUserProfileById(final User googleUser, @Named("id") final String id) throws Exception {
/* Check authorization: */
UserTools.ensureCryptonomicaRegisteredUser(googleUser);
/* Validate form: */
LOG.warning("@Named(\"id\"): " + id);
if (id == null || id.length() < 1) {
throw new Exception("User id is empty");
}
/* Load CryptonomicaUser from DB */
// create key
Key<CryptonomicaUser> cryptonomicaUserKey = Key.create(CryptonomicaUser.class, id);
// get user with given Id from DB:
CryptonomicaUser profile = ofy().load().key(cryptonomicaUserKey).now();
// check result:
if (profile == null) {
LOG.warning("User with not found");
throw new Exception("User not found");
}
/* Create UserProfileGeneralView object to return */
// find his public keys:
List<PGPPublicKeyData> pgpPublicKeyDataList = ofy().load().type(PGPPublicKeyData.class).filter("cryptonomicaUserId", id).list();
// check keys found, if no webSafeString add and store in DB :
LOG.info("pgpPublicKeyDataList: " + new Gson().toJson(pgpPublicKeyDataList));
for (PGPPublicKeyData k : pgpPublicKeyDataList) {
if (k.getWebSafeString() == null) {
if (k.getCryptonomicaUserKey() != null) {
k.setWebSafeString(Key.create(k.getCryptonomicaUserKey(), PGPPublicKeyData.class, k.getFingerprint()).toWebSafeString());
} else {
// for old keys without parent
k.setWebSafeString(Key.create(PGPPublicKeyData.class, k.getFingerprint()).toWebSafeString());
}
// async !
ofy().save().entity(k);
}
}
// create obj to return:
UserProfileGeneralView userProfileGeneralView = new UserProfileGeneralView(profile, pgpPublicKeyDataList);
LOG.warning("userProfileGeneralView: " + userProfileGeneralView.toJson());
return userProfileGeneralView;
}
use of com.google.api.server.spi.config.ApiMethod in project cryptonomica by Cryptonomica.
the class UserSearchAndViewAPI method generalSearchUserProfiles.
@ApiMethod(name = "generalSearchUserProfiles", path = "generalSearchUserProfiles", httpMethod = ApiMethod.HttpMethod.POST)
@SuppressWarnings("unused")
public // returns list of user profiles
UserSearchAndViewReturn generalSearchUserProfiles(final HttpServletRequest httpServletRequest, final User googleUser, final GeneralSearchUserProfilesForm generalSearchUserProfilesForm) throws Exception {
LOG.info("serch request: " + new Gson().toJson(generalSearchUserProfilesForm));
// authorization
UserTools.ensureCryptonomicaRegisteredUser(googleUser);
// initialize object for search result:
List<CryptonomicaUser> cryptonomicaUsersList;
// set "" value to be null
if (generalSearchUserProfilesForm.getFirstName() != null && generalSearchUserProfilesForm.getFirstName().equals("")) {
generalSearchUserProfilesForm.setFirstName(null);
}
if (generalSearchUserProfilesForm.getLastName() != null && generalSearchUserProfilesForm.getLastName().equals("")) {
generalSearchUserProfilesForm.setLastName(null);
}
if (generalSearchUserProfilesForm.getEmail() != null && generalSearchUserProfilesForm.getEmail().equals("")) {
generalSearchUserProfilesForm.setEmail(null);
}
// search: first name + last name
if (generalSearchUserProfilesForm.getFirstName() != null && generalSearchUserProfilesForm.getLastName() != null && generalSearchUserProfilesForm.getEmail() == null) {
cryptonomicaUsersList = ofy().load().type(CryptonomicaUser.class).filter("firstName", generalSearchUserProfilesForm.getFirstName().toLowerCase()).filter("lastName", generalSearchUserProfilesForm.getLastName().toLowerCase()).list();
} else // first name + last name + email
if (generalSearchUserProfilesForm.getFirstName() != null && generalSearchUserProfilesForm.getLastName() != null && generalSearchUserProfilesForm.getEmail() != null) {
cryptonomicaUsersList = ofy().load().type(CryptonomicaUser.class).filter("firstName", generalSearchUserProfilesForm.getFirstName().toLowerCase()).filter("lastName", generalSearchUserProfilesForm.getLastName().toLowerCase()).filter("email", new Email(generalSearchUserProfilesForm.getEmail().toLowerCase())).list();
} else // first name + email
if (generalSearchUserProfilesForm.getFirstName() != null && generalSearchUserProfilesForm.getLastName() == null && generalSearchUserProfilesForm.getEmail() != null) {
cryptonomicaUsersList = ofy().load().type(CryptonomicaUser.class).filter("firstName", generalSearchUserProfilesForm.getFirstName().toLowerCase()).filter("email", new Email(generalSearchUserProfilesForm.getEmail().toLowerCase())).list();
} else // last name + email
if (generalSearchUserProfilesForm.getFirstName() == null && generalSearchUserProfilesForm.getLastName() != null && generalSearchUserProfilesForm.getEmail() != null) {
cryptonomicaUsersList = ofy().load().type(CryptonomicaUser.class).filter("lastName", generalSearchUserProfilesForm.getLastName().toLowerCase()).filter("email", new Email(generalSearchUserProfilesForm.getEmail().toLowerCase())).list();
} else // email only
if (generalSearchUserProfilesForm.getFirstName() == null && generalSearchUserProfilesForm.getLastName() == null && generalSearchUserProfilesForm.getEmail() != null) {
cryptonomicaUsersList = ofy().load().type(CryptonomicaUser.class).filter("email", new Email(generalSearchUserProfilesForm.getEmail().toLowerCase())).list();
} else // first name only
if (generalSearchUserProfilesForm.getFirstName() != null && generalSearchUserProfilesForm.getLastName() == null && generalSearchUserProfilesForm.getEmail() == null) {
cryptonomicaUsersList = ofy().load().type(CryptonomicaUser.class).filter("firstName", generalSearchUserProfilesForm.getFirstName().toLowerCase()).list();
} else // last name
if (generalSearchUserProfilesForm.getFirstName() == null && generalSearchUserProfilesForm.getLastName() != null && generalSearchUserProfilesForm.getEmail() == null) {
cryptonomicaUsersList = ofy().load().type(CryptonomicaUser.class).filter("lastName", generalSearchUserProfilesForm.getLastName().toLowerCase()).list();
} else {
throw new Exception("Search query is incorrect or empty");
}
//
UserSearchAndViewReturn userSearchAndViewReturn;
if (cryptonomicaUsersList != null && !cryptonomicaUsersList.isEmpty()) {
userSearchAndViewReturn = new UserSearchAndViewReturn("results found", new ArrayList<>(cryptonomicaUsersList));
} else {
userSearchAndViewReturn = new UserSearchAndViewReturn("no results found");
}
LOG.info("search result: " + new Gson().toJson(userSearchAndViewReturn));
return userSearchAndViewReturn;
}
Aggregations