I am integrating Stripe in my android application.
I have found two issues and I am stuck.
First (and major) issue is -
I have got token key from the card information. But when I tried to make my first charge, it shows error in following line.
Stripe.apiKey = "sk_test_0000000000000";
I have googled also but couldn't find any solution.
Creating Stripe Customer - cannot resolve symbol apiKey
I have even seen this kind of question too. But I failed to understand its solution.
Following is my code for making charge:
final String publishableApiKey = BuildConfig.DEBUG ?
"pk_test_000000000000000000" :
//"sk_test_00000000000000000000000" :
getString(R.string.com_stripe_publishable_key);
final TextView cardNumberField = (TextView) findViewById(R.id.cardNumber);
final TextView monthField = (TextView) findViewById(R.id.month);
final TextView yearField = (TextView) findViewById(R.id.year);
TextView cvcField = (TextView) findViewById(R.id.cvc);
Card card = new Card(cardNumberField.getText().toString(),
Integer.valueOf(monthField.getText().toString()),
Integer.valueOf(yearField.getText().toString()),
cvcField.getText().toString());
Stripe stripe = new Stripe();
stripe.createToken(card, publishableApiKey, new TokenCallback() {
public void onSuccess(Token token) {
// TODO: Send Token information to your backend to initiate a charge
Toast.makeText(
getApplicationContext(),
"Charge Token created: " + token.getId(),
Toast.LENGTH_LONG).show();
/*make a charge starts*/
// Set your secret key: remember to change this to your live secret key in production
// Create the charge on Stripe's servers - this will charge the user's card
Stripe.apiKey = "sk_test_0000000000000000000000";
try {
Map<String, Object> chargeParams = new HashMap<String, Object>();
chargeParams.put("amount", 100); // amount in cents, again
chargeParams.put("currency", "usd");
chargeParams.put("source", token.getId());
chargeParams.put("description", "Example charge");
Charge charge = Charge.create(chargeParams);
System.out.println("Charge Log :" + charge);
} catch (CardException e) {
// The card has been declined
} catch (APIException e) {
e.printStackTrace();
} catch (AuthenticationException e) {
e.printStackTrace();
} catch (InvalidRequestException e) {
e.printStackTrace();
} catch (APIConnectionException e) {
e.printStackTrace();
}
/*charge ends*/
}
I have tried these code from different examples. I followed Stripe doc too.
But I got this error:
com.stripe.exception.AuthenticationException: No API key provided. (HINT: set your API key using 'Stripe.apiKey = '.
Second Issue is - about validation.
If I am entering my own card details, and if I write wrong cvv number. It still generates token key.
I have already implemented validation of fields using Stripe's official doc. I don't know how to validate it with real time data.
Solution for the First Issue:
com.stripe.Stripe.apiKey = "sk_test_xxxxxxxxxxxxxxxxxxx";
try {
final Map<String, Object> chargeParams = new HashMap<String, Object>();
chargeParams.put("amount", 500); // amount in cents, again
chargeParams.put("currency", "usd");
chargeParams.put("source", token.getId());
chargeParams.put("description", "Example charge");
new Thread(new Runnable() {
#Override
public void run() {
Charge charge = null;
try {
charge = Charge.create(chargeParams);
} catch (AuthenticationException e) {
e.printStackTrace();
} catch (InvalidRequestException e) {
e.printStackTrace();
} catch (APIConnectionException e) {
e.printStackTrace();
} catch (CardException e) {
e.printStackTrace();
} catch (APIException e) {
e.printStackTrace();
}
System.out.println("Charge Log :" + charge);
}
}).start();
} catch (Exception e) {
e.printStackTrace();
}
Stripe's Android bindings only let you tokenize card information. Once the token has been created, it must be sent to an external server where you can use it in API requests.
You cannot use the token directly from the app as the app must never have access to your secret key, where it could easily be extracted by an attacker who would then have access to your account.
Re. your second question, the card isn't validated with the bank when the token is created (there are still some basic sanity checks, such as checking the number of digits, the fact that the expiry date is in the future, etc.). It is only when you use the token in a server-side API request that the card will be checked with the bank.
Related
I'm new to blockchain development. Currently, I'm learning the Ethereum platform and it sounds a very good environment to start with. I tested the web3j library on my Android application and it works fine. I used the following code to connect to my testrpc node:
Web3j web3 = Web3jFactory.build(new HttpService("http://192.168.1.108:8545"));
BigInteger gasPrice = BigInteger.valueOf(20000000000L);
BigInteger gasLimit = BigInteger.valueOf(500000L);
BigInteger nonce = null;
String contractAddress="0x0dd3b0efbce5c4eba2dc9b8500ecafb0b1cec28f";
String from = "0x2d6fcee3c3435ebda9184bdddf8481a87b7d1948";
List<Type> inputParameters = new ArrayList<>();
String hash ="232131231232141231231231231232123123123";
byte[] b =Arrays.copyOfRange(new BigInteger(hash, 16).toByteArray(), 1, 33);
Type _telNumber = new Bytes32(b);
Type _publicKey = new Bytes32(b);
inputParameters.add(_telNumber);
inputParameters.add(_publicKey);
Function function = new Function("addPerson",
inputParameters,
Collections.<TypeReference<?>>emptyList());
String functionEncoder = FunctionEncoder.encode(function);
Transaction transaction = Transaction.createFunctionCallTransaction(from, nonce,gasPrice,gasLimit,contractAddress,new BigInteger("0"), functionEncoder);
try {
EthSendTransaction transactionResponse = web3.ethSendTransaction(transaction).sendAsync().get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
The above code worked and I was able to interact with a smart contract (call a function).
The main problem is that the from parameter is hardcoded (I got it from the testrpc list of accounts).
What I want to achieve: I want to create an application in which the users can create new wallets (accounts), and use them to transact with the network. I created the wallets succesfully using the following code:
String filePath = Environment.getExternalStorageDirectory().toString() + "/Pictures";
Web3j web3 = Web3jFactory.build(new HttpService("http://192.168.1.108:8545"));
Web3ClientVersion web3ClientVersion = null;
try {
String fileName = WalletUtils.generateNewWalletFile("your password",new File(filePath),false);
Log.d("FILENAME",fileName);
Credentials credentials = WalletUtils.loadCredentials(
"your password",
filePath+"/"+fileName);
myAddress = credentials.getAddress();
Log.d("My address",credentials.getAddress());
} catch (CipherException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InvalidAlgorithmParameterException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (NoSuchProviderException e) {
e.printStackTrace();
}
What's the next step? Should I broadcast my address to the network?
The main problem is that the from parameter is hardcoded (I got it from the testrpc list of accounts).
You can call your file and decrypt it to get your address, every time you create a new account you generate a file with your keys.
val credentialsOne = WalletUtils.loadCredentials("your password", "your path")
When you connect with your node, it will automatically detect the address
I am new to Openfire and smack, therefore I have questions regarding pubsub feature. Actually, I have created a node with setAccessModel as authorize, shown below.
PubSubManager mgr = new PubSubManager(xmpp.getConnection());
try {
LeafNode leaf = mgr.createNode("testNode");
ConfigureForm form = new ConfigureForm(DataForm.Type.submit);
form.setAccessModel(AccessModel.authorize);
form.setDeliverPayloads(true);
form.setNotifyRetract(true);
form.setPersistentItems(true);
form.setPublishModel(PublishModel.open);
leaf.sendConfigurationForm(form);
} catch (SmackException.NoResponseException e) {
e.printStackTrace();
} catch (XMPPException.XMPPErrorException e) {
e.printStackTrace();
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
}
My question is that when somebody wants to subscribe to above node, how the owner of this node can handle the subscription request? Subscription part is as follows:
PubSubManager mgr = new PubSubManager(xmpp.getConnection());
// Get the node
LeafNode node = null;
try {
node = mgr.getNode("testNode");
node.addItemEventListener(new ItemEventCoordinator());
node.subscribe(senderUser+"#desi.loc");
} catch (SmackException.NoResponseException e) {
e.printStackTrace();
} catch (XMPPException.XMPPErrorException e) {
e.printStackTrace();
} catch (SmackException.NotConnectedException e) {
e.printStackTrace();
}
class ItemEventCoordinator implements ItemEventListener {
#Override
public void handlePublishedItems(ItemPublishEvent items) {
final ItemPublishEvent itemstemp=items;
runOnUiThread(new Runnable() {
#Override
public void run() {
//stuff that updates ui
dspySub.setText("Item: " + itemstemp.getItems());
}
});
}
}
When I set form.setAccessModel(AccessModel.open) every thing works fine. Users can publish and subscribe easily but when its AccessModel is authorize, owner don't listen, or might be I don't know how to handle subscription request at owner side with above piece of code. Kindly guide me.
Jawad, I've just replied another guy' question about listening subscription requests. Please take a look:
How can i listen incoming subscription request in smack openfire android
I really hope that it can help you.
Good luck!
PS.: Sorry, but I haven't enough reputation to do a comment :(
I want to take out the GoogleAuthToken, of the user using following method:
private class RetrieveTokenTask extends AsyncTask {
#Override
protected String doInBackground(String... params) {
String accountName = params[0];
String scopes = "oauth2:openid";
String token = null;
try {
token = GoogleAuthUtil.getToken(getApplicationContext(), accountName, scopes);
} catch (IOException e) {
Log.e(TAG, e.getMessage());
} catch (UserRecoverableAuthException e) {
Log.e(TAG, e.getMessage());
startActivityForResult(e.getIntent(), REQ_SIGN_IN_REQUIRED);
} catch (GoogleAuthException e) {
Log.e(TAG, e.getMessage());
}
return token;
}
#Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
if (s != null) {
Log.e("AccessToken", s);
preferences.edit().putString(PreferencesConstants.GOOGLE_AUTH_TOKEN, s).commit();
}
}
}
Note: This method successfully gives me the token, BUT THE PROBLEM IS: It asks for user's permission with Allow and Deny for the first time. (which I dont want). Is there any way I can take out the token, without user's permission. ?
This is due to API access given to the application. For each developer machine SHA1 should be given to API and create separate Android API key for the each SHA1.
Go to console.developers.google.com -> select your project
API & AUTH -> credentials.
Create new client Key (though it has other client keys).
select installed app -> android.
Give your package name and SHA1 correctly select OK.
I'm using app billing v3 and Iabhelper interface, my implementation is ok (hope that) , but lately I'm getting this 2 problems with some registers:
Some orderId that I'm seding to my server are not in the normal format "orderId" : "GPA.1234-5678-9012-34567", I'm getting something like "556515565155651". Documentation say:
"For transactions dated previous to 5 December 2012, you get "556515565155651".."
but this is a 2015 app, and this register is not showed in my google merchant account and finnancial reports.
My code is ..
IabHelper.OnIabPurchaseFinishedListener mPurchaseFinishedListener= new IabHelper.OnIabPurchaseFinishedListener() {
public void onIabPurchaseFinished(IabResult result,Purchase purchase)
{
if (result.isFailure()) {
if (purchase!=null){
try {
String price = Helper.getPriceSKU("mypackage", "mysku, "subs");
String currency = mHelper.getCurrencySKU("mypackage","mysku", "subs" );
} catch (RemoteException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
String orderId = purchase.getOrderId();
send_to_server(price, currency, orderId, "rejected");
}
} else {
try {
String price = Helper.getPriceSKU("mypackage", "mysku, "subs");
String currency = mHelper.getCurrencySKU("mypackage","mysku", "subs" );
} catch (RemoteException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
String orderId = purchase.getOrderId();
send_to_server(price, currency, orderId, "Aproved");
}
}
In some registers I'm getting the currency different. For example, my google account default has a PEN currency. One user is in Colombia and when I got his payment currency register it was PEN.. I know this is possible only if his google account and credit card is PEN, but strange thing is the prices is showed colombia local format... is possible a google error getting the currency? my code above.. thank you in advance!!
My goal is getting user's basic profile information (first name, last name etc) without any server-side work. And I don't want to use a Google+ sign-in button on the UI.
I could get user's email without any problems. So the next step is to get an access token to fetch the profile, here is my code
String token = null;
try {
token = GoogleAuthUtil.getToken(getApplicationContext(), email, SCOPE);
} catch (final UserRecoverableAuthException e) {
startActivityForResult(e.getIntent(), REQUEST_CODE_AUTH_GOOGLE_ACCOUNT);
} catch (IOException e) {
Log.e(TAG, e.toString());
} catch (GoogleAuthException e) {
Log.e(TAG, e.toString());
}
and I've defined SCOPE
private static final String SCOPE = "oauth2:https://www.googleapis.com/auth/userinfo.profile";
When I run my app, I got "com.google.android.gms.auth.GoogleAuthException: Unknown" exception. I've also tried other scopes like Scope.PLUS_LOGIN, Scope.PLUG_PROFILE. But all of them gave me the same exception.
Btw, in Google API Console, I've already setup a client ID.
I've been stuck on this for 2 days. Any help would be really appreciated.
I think you are doing it wrong. That's the way I do it:
scopes.add(AnalyticsScopes.ANALYTICS_READONLY);
credential = GoogleAccountCredential.usingOAuth2(this, scopes);
if (TextUtils.isEmpty(appPreferences.getUserName()))
{
try
{
startActivityForResult(credential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);
}
catch (ActivityNotFoundException e)
{
Toast.makeText(this, getString(R.string.gps_missing), Toast.LENGTH_LONG).show();
return;
}
}
Take a look at the my source file here:
https://github.com/madhur/GAnalytics/blob/develop/src/in/co/madhur/ganalyticsdashclock/MainActivity.java