Integrate Notch Pay into your mobile applications
build.gradle
file:dependencies {
implementation 'co.notchpay:notchpay-android:1.2.0'
}
// In your Application class or main activity
import co.notchpay.android.NotchPay;
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
// Initialize with your public key
NotchPay.initialize("YOUR_PUBLIC_KEY");
// For test mode
NotchPay.setTestMode(true); // Set to false for production
}
}
// First, create a payment on your server and get the payment ID
String paymentId = "pay_123456789"; // Get this from your server
// Then, launch the checkout
NotchPay.checkout(
this, // Activity context
paymentId,
new NotchPayCheckoutCallback() {
@Override
public void onSuccess(NotchPayTransaction transaction) {
// Payment successful
String reference = transaction.getReference();
String status = transaction.getStatus();
double amount = transaction.getAmount();
String currency = transaction.getCurrency();
// Update your UI or navigate to a success screen
showSuccessScreen(reference, amount, currency);
}
@Override
public void onError(NotchPayException error) {
// Payment failed
String errorMessage = error.getMessage();
// Show error message to user
showErrorMessage(errorMessage);
}
@Override
public void onCancel() {
// User canceled the payment
// Handle cancellation
showCancellationMessage();
}
}
);
NotchPayCheckoutConfig config = new NotchPayCheckoutConfig.Builder()
.setTitle("Payment for Order #123")
.setDescription("Your order from Example Store")
.setLogoUrl("https://example.com/logo.png")
.setPrimaryColor("#4F46E5")
.setShowReceiptPage(true)
.build();
NotchPay.checkout(this, paymentId, config, callback);
AndroidManifest.xml
:<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!-- Deep link handling -->
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:scheme="yourapp"
android:host="payment" />
</intent-filter>
</activity>
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Handle deep link
Intent intent = getIntent();
if (Intent.ACTION_VIEW.equals(intent.getAction())) {
Uri uri = intent.getData();
if (uri != null) {
String reference = uri.getQueryParameter("reference");
if (reference != null) {
// Verify payment status with your server
verifyPayment(reference);
}
}
}
}
public class PaymentActivity extends AppCompatActivity {
private Button payButton;
private TextView statusText;
private ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_payment);
payButton = findViewById(R.id.pay_button);
statusText = findViewById(R.id.status_text);
progressBar = findViewById(R.id.progress_bar);
payButton.setOnClickListener(v -> initiatePayment());
}
private void initiatePayment() {
progressBar.setVisibility(View.VISIBLE);
statusText.setText("Creating payment...");
// Make API call to your server to create a payment
ApiClient.createPayment(
5000, // Amount
"XAF", // Currency
"customer@example.com", // Customer email
new ApiCallback<PaymentResponse>() {
@Override
public void onSuccess(PaymentResponse response) {
progressBar.setVisibility(View.GONE);
launchCheckout(response.getPaymentId());
}
@Override
public void onError(Exception e) {
progressBar.setVisibility(View.GONE);
statusText.setText("Error: " + e.getMessage());
}
}
);
}
private void launchCheckout(String paymentId) {
NotchPayCheckoutConfig config = new NotchPayCheckoutConfig.Builder()
.setTitle("Payment for Order #123")
.setDescription("Your order from Example Store")
.setPrimaryColor("#4F46E5")
.build();
NotchPay.checkout(
this,
paymentId,
config,
new NotchPayCheckoutCallback() {
@Override
public void onSuccess(NotchPayTransaction transaction) {
statusText.setText("Payment successful!\nReference: " + transaction.getReference());
}
@Override
public void onError(NotchPayException error) {
statusText.setText("Payment failed: " + error.getMessage());
}
@Override
public void onCancel() {
statusText.setText("Payment canceled");
}
}
);
}
private void verifyPayment(String reference) {
progressBar.setVisibility(View.VISIBLE);
statusText.setText("Verifying payment...");
// Make API call to your server to verify the payment
ApiClient.verifyPayment(
reference,
new ApiCallback<PaymentVerificationResponse>() {
@Override
public void onSuccess(PaymentVerificationResponse response) {
progressBar.setVisibility(View.GONE);
if (response.isSuccessful()) {
statusText.setText("Payment verified!\nAmount: " + response.getAmount() + " " + response.getCurrency());
} else {
statusText.setText("Payment verification failed: " + response.getMessage());
}
}
@Override
public void onError(Exception e) {
progressBar.setVisibility(View.GONE);
statusText.setText("Error verifying payment: " + e.getMessage());
}
}
);
}
}
Was this page helpful?