Updated 14 December 2016
This is the chapter-2 of how to integrate the PayPal standard payment gateway in your android app.
In the previous chapter, we had known how to get the API Credentials after that we to implement it in the app.
Step 1: Get the Client_id and serect(which will be used in future) by creating the app in PayPal developer account.
For knowing, how to get the client_id and secret go to this knowledgebase.
Step 2: Make a credential class file to enter the PayPal app credentials
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public class PaypalCredentials { public static final String CONFIG_ENVIRONMENT = PayPalConfiguration.ENVIRONMENT_SANDBOX; // note that these credentials will differ between live & sandbox environments. public static final String CONFIG_CLIENT_ID = "MERCHANT_CLIENT_ID"; public static final int REQUEST_CODE_PAYMENT = 1; public static PayPalConfiguration config = new PayPalConfiguration() .environment(CONFIG_ENVIRONMENT) //For enable direct card payment. .acceptCreditCards(true) .clientId(CONFIG_CLIENT_ID) // The following are only used in PayPalFuturePaymentActivity. .merchantName("MERCHANT_NAME") .merchantPrivacyPolicyUri(Uri.parse("https://www.example.com/privacy")) .merchantUserAgreementUri(Uri.parse("https://www.example.com/legal")); } |
Step 3: Configure the PayPal service add it to your payment activity,
1 2 3 |
Intent paypalIntent = new Intent(this, PayPalService.class); paypalIntent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, PaypalCredentials.config); startService(paypalIntent); |
Step 4: Prepared checkout data with function getStuffToBuy,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
private PayPalPayment getStuffToBuy(String paymentIntent) { //--- include an item list, payment amount details BigDecimal shipping = null, subtotal = null, amount = null, discount; try { JSONArray orderReviewDataArray = mainObjectForOrderReview.getJSONObject("continue").getJSONArray("products"); if (mainObjectForOrderReview.getJSONObject("continue").has("discount")) { items = new PayPalItem[orderReviewDataArray.length() + 1]; discount = new BigDecimal(mainObjectForOrderReview.getJSONObject("orderReviewData").getJSONObject("discount").getString("unformatedValue")); int i; for (i = 0; i < orderReviewDataArray.length(); i++) { JSONObject paypalDetails = orderReviewDataArray.getJSONObject(i); items[i] = new PayPalItem(paypalDetails.getString("name"), Integer.parseInt(paypalDetails.getString("quantity")), new BigDecimal(paypalDetails.getString("uf_price")), currencyCode, paypalDetails.getString("model")); } items[i] = new PayPalItem("Discount", 1, discount, currencyCode, "dicountsku"); subtotal = PayPalItem.getItemTotal(items); subtotal = subtotal.add(discount); } amount = subtotal; if (mainObjectForOrderReview.getJSONObject("continue").getJSONObject("paypal_data").has("shipping")) { shipping = new BigDecimal(mainObjectForOrderReview.getJSONObject("continue").getJSONObject("paypal_data").getString("shipping")); amount = amount.add(shipping); } if (mainObjectForOrderReview.getJSONObject("continue").getJSONObject("paypal_data").has("discount")) { discount = new BigDecimal(mainObjectForOrderReview.getJSONObject("continue").getJSONObject("paypal_data").getString("discount")); amount = amount.add(discount); } // if (mainObjectForOrderReview.getJSONObject("continue").getJSONObject("paypal_data").has("tax")) { // tax = new BigDecimal(mainObjectForOrderReview.getJSONObject("continue").getJSONObject("paypal_data").getString("tax")); // amount = amount.add(tax); // } } catch (JSONException e) { Log.d("paypal Exception", e + ""); e.printStackTrace(); } PayPalPaymentDetails paymentDetails = new PayPalPaymentDetails(shipping, subtotal, null); payment = new PayPalPayment(amount, currencyCode, incrementId, paymentIntent); payment.items(items).paymentDetails(paymentDetails); //--- set other optional fields like invoice_number, custom field, and soft_descriptor payment.custom(getResources().getString(R.string.paypal_custom)); return payment; } |
Step 5: getStuffToBuy function return an object,
1 |
PayPalPayment thingToBuy = getStuffToBuy(PayPalPayment.PAYMENT_INTENT_SALE); |
Step 6: After preparing the data send it to the PaymentActivity.class
1 2 3 4 5 |
Intent intent = new Intent(Checkout.this, PaymentActivity.class); // send the same configuration for restart resiliency intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, PaypalCredentials.config); intent.putExtra(PaymentActivity.EXTRA_PAYMENT, thingToBuy); startActivityForResult(intent, PaypalCredentials.REQUEST_CODE_PAYMENT); |
Step 7: And At last get the result,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == PaypalCredentials.REQUEST_CODE_PAYMENT) { if (resultCode == Activity.RESULT_OK) { confirm = data.getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION); if (confirm != null) { try { Log.d(TAG, confirm.toJSONObject().toString(4)); Log.d(TAG, confirm.toJSONObject().toString(4)); // receive your data } catch (JSONException e) { Log.e(TAG, "an extremely unlikely failure occurred: ", e); } } } else if (resultCode == Activity.RESULT_CANCELED) { status = 1; Log.i(TAG, "The user canceled."); } else if (resultCode == PaymentActivity.RESULT_EXTRAS_INVALID) { status = 2; Log.i(TAG, "An invalid Payment or PayPalConfiguration was submitted. Please see the docs."); } } |
In the response, you have received a pay_id, Which is used to getting the full transaction details of payment by calling simply two API calls.
For getting the full transaction details by the pay_id in PayPal mobile payment visit our this blog.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.