Design to manage paid subscription to services in an application? - java

Let's say I have a hypothetical paid application like stackoverflow. The application allows a user to do two things:
Post a question
Post an answer
This application charges $20 to buy a plan which allows posting 10 questions and 10 answers.
Technically, both the actions are exposed through completely different interfaces
interface Questions {
postQuestion(String question);
}
interface Answers {
postAnswer(int questionId, String answer);
}
Both the services are accessible over web through different controllers. I need to keep track of how many questions and answers the user has posted, so that I can ask them to make a new purchase when the limit is reached, without delegating this task to the services.
For instance in Java, I need something like a map like Map<Method,Integer>, which keeps track of how many times each allowed method was successfully invoked. And, maintain this map by intercepting the calls to these services. This Map will also require database storage.
Thanks!

In your Customer DB you can add a answerCounter and a questionCounter.
When the customer registered set those value to 10.
Each time a service is called first check if the customer is allowed to call it by checking if answerCounter or questionCounter (depending on the request) is higher than 0.
If the customer is allowed to post the answer/question then decrement the answerCounter or questionCounter by 1.
If the customer paid a new pack increase those value by or to 10.

Related

Java - onlineUsers() method that returns an ArrayList of all Users that are currently online?

I created a class called VideoExchangeCenter which represents the company website that users log in and out of in order to download videos. The class contains the attribute: users - an ArrayList of all registered Users (which may be either logged on or not logged on).
Additionally, I am required to create an onlineUsers() method that returns an ArrayList of all Users that are currently online. This method essentially creates and returns a new ArrayList each time it is called.
I'm struggling to figure out some way to determine if a specific user is online or not. I tried looking up similar problems, but most of the results I found were too complex for my programming level or were either unrelated to my problem. I would really appreciate some help or a push in the right direction.
import java.util.ArrayList;
public class VideoExchangeCenter {
ArrayList<User> users;
public VideoExchangeCenter() {
users = new ArrayList<User>();
}
public onlineUsers(){
}
}
Basically you have to define what "being online" means.
Obviously, users that are currently up/downloading content are online. Obviously, users that logged in, but later logged out are offline. Users that are currently logged in but didn't do anything for X hours, hmm, what about them?
So, the point is: there are multiple approaches to go about this. When users need to login to use your service, the server can keep track of that information. You can also store time stamps each time a user interacts with a system, and assume "when a user is inactive for 1 hour, he is considered offline".
So, in the real world, such a service has plenty of ways to determine who is "currently online", some being heuristics. What you do in your homework assignment depends on the broader context of it.

FirebaseUser - how to use PhoneAuthCredential parameter for FirebaseUser.updatePhoneNumber(PhoneAuthCredential)

I'm in the middle of developing an Android app using Firebase, and have Phone Number Authentication enabled as a method of user sign-in. This all works fine.
Inside my app, I have an Account Details page that allows the user to edit their information and update their record in the FirebaseDatabase (in a separate node that I have created and called users). I'm able to update this table with no problems, but I need to update the table that Firebase keeps when users register, so that phone numbers don't get out of sync.
In theory, it should be really easy to do. I've done some reading up and seen that a method exists in the FirebaseUser class called updatePhoneNumber(PhoneAuthCredential). The only problem is that I have no idea how the PhoneAuthCredential class works and, after a couple of hours of Googling, haven't been able to find a single example, or many other forms of support for the method.
An example for the FirebaseUser.updateEmail(String) method can be seen here, so I'd guess that it can't be all that different, and should work fine if I can get the PhoneAuthCredential object set up correctly.
Update:
I tried to create an instance using new PhoneAuthCredential(...), but the suggested parameters aren't much help...
Android Studio displays the constructor as public PhoneAuthCredential(String s, String s1, boolean b, String s2, boolean b2, String s3), so I am very much none-the-wiser.
If I can figure out how to use this constructor, I might well be off the ground, hopefully.
If anyone can advise on how to use PhoneAuthCredential, or how the FirebaseUser.updatePhoneNumber() method should be implemented, that would be a huge help!
Thanks in advance,
Mark
Phone auth is quite complicated, so I'd recommend reading this guide. This section is especially relevant to your case. Basically, you'll have to go through the entire phone auth flow again to get a credential and set the user's new phone number.
If you're thinking of the phone number as a part of the user's profile, that's incorrect as you can see from the profile request. The phone number is considered to be a sort of user identifier, like the email which also requires a credential if the user's sign-in action is 5 mins old. Hope this helps!

Braintree - Android SDK: new credit card or existing one?

I'm new to the Android+Braintree world. At the moment I'm using the Drop-In interface.
What I want to do is the following
"If the user is creating/adding a new card, ask him if he wants to save
credit card information".
I need this info because later I will set the storeInVaultOnSuccess option in the transaction's params accordingly.
Now, it seems that there is no way to insert an element in the drop-in GUI to understand if the user wants to save these data or not.
Fine, so I'm going to add an additional step just after the drop-in interface in which I'm only going to ask the user if he wants to save the credit card data or not.
But the problem is that I don't want to ask this question if he choose an already existent credit card, so I need to know if he created a new card or if he selected an existent one.
Is there a way to perform this task (or is there a better alternative)?
WHAT I'VE ALREADY TRIED:
I've tried to see all the information given in the onActivityResult method, more specifically the content of the Intent given as the 3rd parameter.
What I have is, of course, the EXTRA_PAYMENT_METHOD_NONCE, and then the EXTRA_PAYMENT_METHOD (type com.braintreepayments.api.models.Card), but looking at the code of Card.java I'm not able to understand how to retrieve (if possible) this kind of information.
Thanks in advance.
Full disclosure: I work at Braintree. If you have any further questions, feel free to contact support.
The Drop-In UI doesn't support that workflow because at the time of entry the Drop-In automatically vaults the credit card and just provides the nonce (which doesn't contain any information about the card to the client for PCI reasons). In other words, storeInVaultOnSuccess won't apply since it's already there.
As you say, the way to get around this - while still using the Drop-In - would be to keep a reference to the payment method used in the transaction and ask after the transaction goes through. You can get this by examining the response object:
String payment_method_token = "";
TransactionRequest request = new TransactionRequest()
.amount(new BigDecimal("100.00"));
.paymentMethodNonce(nonceFromTheClient);
Result<Transaction> result = gateway.transaction().sale(request);
Transaction transaction = result.getTarget();
if (transaction.getPaymentInstrumentType().equals(PaymentInstrumentType.CREDIT_CARD){
payment_method_token = transaction.getCreditCard.getToken();
}
// ... other payment method types that you support
However, as you say, this is more complicated. You would have to make some selection based on:
a) when the payment method was created (using createdAt), or
a) comparing the arrays of payment methods before and after the checkout experience
This sort of flow really pushes the limits of the Drop-In, and these sorts of constraints often move developers to switch to a custom integration. In that case you would collect their preference - whether they want it stored - as they enter their credit card information.

PayPal-Java SDK/Credit card payment issue [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
My question is about the way how credit card payment is realized by PayPal API
Particularly I'd like to ask about this code snippet from GitHub:
https://github.com/paypal/PayPal-Java-SDK/blob/master/rest-api-sample/src/main/java/com/paypal/api/payments/servlet/PaymentWithCreditCardServlet.java
Credit card object is prepared, all necessary credentials are typed in, including the amount to be payed, sender's billing address and name, etc.
The only thing I cannot understand from this, is why there is no receiver specified ?
That's basically all
Am not a Java dev -
The "receiver" is "your" (merchant) REST application (which is tied to your PayPal Account) you setup in the Developer Console. You authenticate when making requests - in this specific case, you are sending a credit card for auth/capture in one step (aka sale).
So "you" (the authenticated App) are/is the "receiver".
Think of this process as the API Credentials in the PayPal Classic APIs. Hth....
Update:
so you mean that the token belongs to "receiver", not "sender" ? as well as those client ID and client secret ? i thought they belong to sender
"You" - your app, is the sender (in Paypal's context). Your app sends payment information to Paypal for processing. To do that, PayPal must know "who" you are (which Paypal/merchant account/app) is sending the request.
What exactly are you referring to as "receiver" - maybe it's just terminology that's getting in the way?
Update 2:
by receiver I mean the party that gets the charged amount
"Receiver" == funds: In this specific example/code you referenced.Your app/you merchant account that made/sent the request (your app is sender and "receiver" per this definition).
This is a standard "business"/merchant payment processing flow. Forget the tech/API, think about a POS in a restaurant. That POS (aka "app") will send card data (from swiping a physical credit card) to some processor it has an account at, using whatever protocols it needs to communicate with that processor.
If you're actually looking for some "send money to someone" flow (not the code you referenced), this is probably what you're looking for. This has a different meaning for "receiver" - aka "recipient(s) of funds". I don't actually use it, but it seems straightforward...
Hth..

how to get the Fee Amount for paypal

I am currently looking at a system that implements the PayPal api. As a part of this I need to get the feeAmt() which is the fee that is paid to paypal for processing the payment.
From the documentation that I have looked at it appears that I have to implement the getExpressCheckoutDetailsReq() method in order to get the information that I want however no matter what I have tried I am struggling to do this. I should also let you know that I am currently developing my application using Java so using this is going to be best.
If any more explanation is needed please don't hesitate to ask and I will do my best to amend the post :)
GetExpressCheckoutDetails does not include the fee because at that point no payment has been made yet. That's the 2nd of 3 calls for Express Checkout, and until the final call is made there is no fee.
The fee amount would actually come in that final call's response: DoExpressCheckoutPayment. It will come in the PAYMENTINFO_n_FEEAMT parameter, where n is the number of the payment (0,1,2,etc.) Most likely it'll be 0 unless you're working with parallel payments.
Alternatively, you can use Instant Payment Notification (IPN) to get details about transactions, including the fee, in real-time when transactions are completed on your PayPal account.
Yet another option would be to use the GetTransactionDetails API to pull data for an individual transaction which would include the fee in a FEEAMT parameter. Maybe that's the one you were initially thinking of..??

Categories