Configuring In-App Payments library
Below you can see step-by-step instructions on how to include In-App payments library to your mobile application.
Download
After creating a service, download FortumoPayAndroid.jar from your service setup page and include it to the Android application project build path.
Additional configuration
Define an additional activity and an additional service in the AndroidManifest.xml of the application, and provide minimum application permissions:
<!-- Activity --> <activity android:name="com.fortumo.android.FortumoActivity" android:theme="@android:style/Theme.Translucent.NoTitleBar"/> <!-- Service --> <service android:name="com.fortumo.android.FortumoService" /> <!-- Permissions --> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.RECEIVE_SMS" /> <uses-permission android:name="android.permission.SEND_SMS" />
NB: Those who are migrating from 6.X or earlier library - do not forget to add the service to your manifest.
Making a payment
For making a payment you need to extend PaymentActivity class and call makePayment() from somewhere in that activity's code. See the following code example. (In this example we use a button's onclicklistener to start the payment).
package com.fortumo.android.testapp;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import com.fortumo.android.PaymentActivity;
import com.fortumo.android.PaymentRequestBuilder;
import com.fortumo.android.PaymentResponse;
public class TestActivity extends PaymentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button payButton = (Button) findViewById(R.id.payButton);
payButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
PaymentRequestBuilder builder = new PaymentRequestBuilder();
builder.setConsumable(true);
builder.setDisplayString("You are about to buy 100 crystals");
makePayment(builder.build());
}
});
}
@Override
protected void onPaymentCanceled(PaymentResponse response) {
Toast.makeText(this, "Payment canceled by user", Toast.LENGTH_SHORT).show();
}
@Override
protected void onPaymentFailed(PaymentResponse response) {
Toast.makeText(this, "Payment failed", Toast.LENGTH_SHORT).show();
}
@Override
protected void onPaymentPending(PaymentResponse response) {
Toast.makeText(this, "Payment not confirmed", Toast.LENGTH_SHORT).show();
}
@Override
protected void onPaymentSuccess(PaymentResponse response) {
Toast.makeText(this, "Payment received", Toast.LENGTH_SHORT).show();
}
}
Recently (beginning with 6.X library) we changed the way apps communicate with the in-app payment library.
The new system is built upon the idea of a PaymentRequest - PaymentResponse pair.
PaymentRequest object contains all the necessary information for making payment - if the payment is consumable or not, display string for the dialog, etc.
PaymentResponse is the response from Fortumo library and has the billing status, payment ID, price and currency information, etc.
(Note: the Intent-based API is now deprecated since the Request-Response supports the same features.)
Here are the old makePayment calls in PaymentActivity and their equivalents in API v6.X and greater:
// makePayment();
PaymentRequestBuilder builder = new PaymentRequestBuilder();
builder.setConsumable(true); // setConsumable(true) can be omitted, true by default
makePayment(builder.build());
// makePayment(String productName);
PaymentRequestBuilder builder = new PaymentRequestBuilder();
builder.setConsumable(true).setDisplayString(productName);
makePayment(builder.build());
// makePayment(String displayString, String productName);
PaymentRequestBuilder builder = new PaymentRequestBuilder();
builder.setConsumable(true).setDisplayString(displayString).setProductName(productName);
makePayment(builder.build());
// makeConsumablePayment();
PaymentRequestBuilder builder = new PaymentRequestBuilder();
builder.setConsumable(true);
makePayment(builder.build());
// makeConsumablePayment(String productName);
PaymentRequestBuilder builder = new PaymentRequestBuilder();
builder.setConsumable(true).setDisplayString(displayString);
makePayment(builder.build());
// makeConsumablePayment(String displayString, String productName);
PaymentRequestBuilder builder = new PaymentRequestBuilder();
builder.setConsumable(true).setDisplayString(displayString).setProductName(productName);
makePayment(builder.build());
// makeNonConsumablePayment(String displayString, String productName);
PaymentRequestBuilder builder = new PaymentRequestBuilder();
builder.setConsumable(false).setDisplayString(displayString).setProductName(productName);
makePayment(builder.build());
Here are the old callbacks and the matching callbacks in the new API:
// protected void onPaymentCanceled(String product)
protected void onPaymentCanceled(PaymentResponse response) {
String product = response.getProductName();
}
// protected void onPaymentSuccess(String product, String priceAmount, String priceCurrency, String creditAmount, String creditName)
protected void onPaymentSuccess(PaymentResponse response) {
String product = response.getProductName();
String priceAmount = response.getPriceAmount();
String priceCurrency = response.getPriceCurrency();
String creditAmount = response.getCreditAmount();
String creditName = response.getCreditName();
}
// protected void onPaymentFailed(String product)
protected void onPaymentFailed(PaymentResponse response) {
String product = response.getProductName();
}
// protected void onPaymentPending(long messageId, String product)
protected void onPaymentPending(PaymentResponse response) {
long messageId = response.getMessageId();
String product = response.getProductName();
}
The way you can check the status of pending payments has also changed:
// int status = Fortumo.getPaymentStatus(this, messageId) PaymentResponse response = Fortumo.getPaymentResponse(this, messageId); int status = response.getBillingStatus(); /* additionally you can access any other paymentresponse information like productName or priceCurrency/priceAmount for example. */
PS: those who are migration from 6.X to 7.X are already using the new system and do not need to change their makePayment calls.