{"id":16830,"date":"2015-01-22T22:14:32","date_gmt":"2015-01-22T16:44:32","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=16830"},"modified":"2016-12-19T14:54:49","modified_gmt":"2016-12-19T09:24:49","slug":"paypal-sdk-integration-in-android-applications","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/paypal-sdk-integration-in-android-applications\/","title":{"rendered":"PayPal SDK Integration in Android Applications"},"content":{"rendered":"<p>I recently had to integrate a PayPal payment mechanism in one of my <a title=\"android application development services\" href=\"http:\/\/www.tothenew.com\/mobile-android-application-development-services\" target=\"_blank\">Android applications<\/a>.<\/p>\n<p>So I thought to share my knowledge with the community that you can easily integrate payment mechanism in your application.<\/p>\n<h3><strong>What is PayPal?<\/strong><\/h3>\n<p>Established in 1998, PayPal is an American company allowing payments and money transfers to be made through the internet. PayPal is one of the world&#8217;s largest internet payment companies. The company performing payment processing for online vendors, auction sites and other commercial users. It supports 26 currencies across 193 nations.<\/p>\n<h3><strong>PayPal Android SDK:<\/strong><\/h3>\n<p class=\"p1\">PayPal provides its Android SDK to incorporate in-app payment mechanism for any service user avails or any purchase the user do using your application. The Android SDK makes it easy to add PayPal or Credit Card payments to mobile applications. The payments will directly be credited to registered account on PayPal.<\/p>\n<h3><strong>Steps to integrate it in your\u00a0Android application:<\/strong><\/h3>\n<p><strong>Step 1.\u00a0<\/strong><\/p>\n<p>Go to <a href=\"https:\/\/developer.paypal.com\/\">PayPal developer website<\/a>\u00a0and create a PayPal account by signing up.<\/p>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2015\/01\/Screen-Shot-2015-01-21-at-5.43.59-pm.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-16855\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/01\/Screen-Shot-2015-01-21-at-5.43.59-pm.png\" alt=\"Screen Shot 2015-01-21 at 5.43.59 pm\" width=\"741\" height=\"510\" \/><\/a><\/p>\n<p>After clicking &#8220;Sign Up&#8221; button, the registration form will open. Fill up the registration form. Note it takes only five digit zip code and probably only USA addresses. When you enter a valid zip code of USA, it automatically fills up the country code and city name fields.<\/p>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2015\/01\/reg_partially_filled.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-16857\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/01\/reg_partially_filled.png\" alt=\"reg_partially_filled\" width=\"496\" height=\"661\" \/><\/a><\/p>\n<p>After creating a PayPal account, \u00a0sign in into PayPal developer website\u00a0by clicking &#8220;Sign In&#8221; button on the top right corner. \u00a0After Sign In, go to &#8220;Dashboard&#8221; tab and click &#8220;Create App&#8221; button to create an application.<\/p>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2015\/01\/create_app_page1.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-16859\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/01\/create_app_page1.png\" alt=\"create_app_page\" width=\"967\" height=\"455\" \/><\/a><\/p>\n<p>Fill up the create app form and click &#8220;Create App&#8221; button. It will create your application and by default it also creates a Sandbox developer account for you and this default account can not be edited, however you can create new accounts for testing your application. This account will be used to test your application during development phase. Since it is used for testing, it is called a sandbox developer account. This account later can be upgraded to production account for live application.<\/p>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2015\/01\/createapp_form.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-16860\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/01\/createapp_form.png\" alt=\"createapp_form\" width=\"971\" height=\"604\" \/><\/a><\/p>\n<p class=\"p1\">When you will press &#8220;Create app&#8221; button you will get an application created for you with &#8220;Client Id&#8221; and a &#8220;Secret&#8221; key. &#8220;Client Id&#8221; will be used by your <a title=\"android app development services\" href=\"http:\/\/www.tothenew.com\/mobile-android-application-development-services\" target=\"_blank\">Android application<\/a> to perform transaction and &#8220;Secret&#8221; key will be used to verify a transaction, or getting user permission for direct future transactions or getting permission from user to get user profile data from user&#8217;s PayPal account such as date of birth, email address etc.<\/p>\n<p class=\"p1\">You can see your all applications \u00a0under &#8220;My apps&#8221; tab.<\/p>\n<p class=\"p1\"><a href=\"\/blog\/wp-ttn-blog\/uploads\/2015\/01\/carrentalapp.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-16863\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/01\/carrentalapp.png\" alt=\"carrentalapp\" width=\"800\" height=\"533\" \/><\/a><\/p>\n<p class=\"p1\"><strong>Step 2.<\/strong><\/p>\n<p class=\"p1\">Download PayPal Android SDK form <a href=\"https:\/\/github.com\/paypal\/PayPal-Android-SDK\">Github<\/a>.<\/p>\n<p class=\"p1\">Unzip PayPal-Android-SDK-master.zip file and from\u00a0the lib folder copy all the contents and paste it in your Android project&#8217;s lib folder. Note if you don&#8217;t have a lib folder already in your project create one and if you already have lib folder in your project just paste the SDK&#8217;s lib folder&#8217;s contents in it. If you already have &#8220;armeabi&#8221; and other such\u00a0folders in your project&#8217;s lib folder, then manually copy paste the contents of the these folders in your projects respective folders inside the lib folder.<\/p>\n<p class=\"p1\">The SDK also has a sample Android application to understand the code implementation. Run the application and see its flow and try to understand its \u00a0code. It has proper\u00a0description in the form of comments to understand it easily.<\/p>\n<p class=\"p1\">Now open the Android.manifest file of your Android project and declare the following attributes in it:<\/p>\n<p>&lt;!\u2013 for card.io card scanning \u2013&gt; \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &lt;uses-permissionandroid:name=\u201candroid.permission.CAMERA\u201d \/&gt; \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &lt;uses-permissionandroid:name=\u201candroid.permission.VIBRATE\u201d \/&gt;<\/p>\n<p>&lt;uses-featureandroid:name=\u201candroid.hardware.camera\u201d android:required=\u201cfalse\/&gt;<br \/>\n&lt;uses-featureandroid:name=\u201candroid.hardware.camera.autofocus\u201d\u00a0android:required=\u201cfalse\u201d \/&gt;<\/p>\n<p>&lt;!\u2013 for most things, including card.io &amp; paypal \u2013&gt; \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &lt;uses-permissionandroid:name=\u201candroid.permission.ACCESS_NETWORK_STATE\u201d\/&gt; \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 &lt;uses-permissionandroid:name=\u201candroid.permission.INTERNET\u201d\/&gt;<\/p>\n<p class=\"p1\"><strong>Step 3.<\/strong><\/p>\n<p class=\"p1\">There are three use cases of this SDK.<\/p>\n<p class=\"p1\">1. Single Payment \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 2. Future Payment \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a03. Profile Sharing<\/p>\n<p class=\"p1\"><strong>1. Single Payment:<\/strong><\/p>\n<p class=\"p1\">Receive a single immediate payment from your application either via PayPal account or via Debit or Credit Card.<\/p>\n<p class=\"p1\">When you initiate a single payment, the SDK presents a UI to gather user PayPal account or Credit or Debit card info \u00a0and perform transaction and returns a proof of payment with payment id using which user can verify the payment transaction. For payment verification PayPal provides web apis which can be used by setting up our own server for verification.<\/p>\n<p class=\"p1\"><a href=\"\/blog\/wp-ttn-blog\/uploads\/2015\/01\/paypal_paymentui.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-16872\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/01\/paypal_paymentui.png\" alt=\"paypal_paymentui\" width=\"284\" height=\"505\" \/><\/a><\/p>\n<p class=\"p1\">User can make payment either using PayPal account or using credit or debit card.<\/p>\n<p class=\"p1\"><strong>Inside the activity:<\/strong><\/p>\n<p class=\"p1\">1. Create a <span style=\"color: #ff0000;\"><em>PayPalConfigration<\/em><\/span> Object :<\/p>\n<p>[sourcecode language=&#8221;java&#8221; wraplines=&#8221;false&#8221; collapse=&#8221;false&#8221;]<br \/>\nprivate static PayPalConfiguration config = new PayPalConfiguration()<\/p>\n<p>        \/\/ Start with mock environment.  When ready, switch to sandbox (ENVIRONMENT_SANDBOX)<br \/>\n        \/\/ or live (ENVIRONMENT_PRODUCTION)<br \/>\n        .environment(PayPalConfiguration.ENVIRONMENT_NO_NETWORK)<\/p>\n<p>        .clientId(&quot;&lt;YOUR_CLIENT_ID&gt;&quot;);<br \/>\n[\/sourcecode]<\/p>\n<p>2. Start <span style=\"color: #ff0000;\"><em><span class=\"s1\">PayPalService<\/span><\/em><\/span> when your activity is created and stop it upon destruction:<\/p>\n<p>[sourcecode language=&#8221;java&#8221; wraplines=&#8221;false&#8221; collapse=&#8221;false&#8221;]<br \/>\n@Override<br \/>\nprotected void onCreate(Bundle savedInstanceState) {<br \/>\n    super.onCreate(savedInstanceState);<br \/>\n    setContentView(R.layout.activity_main);<\/p>\n<p>    Intent intent = new Intent(this, PayPalService.class);<\/p>\n<p>    intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);<\/p>\n<p>    startService(intent);<br \/>\n}<\/p>\n<p>@Override<br \/>\npublic void onDestroy() {<br \/>\n    stopService(new Intent(this, PayPalService.class));<br \/>\n    super.onDestroy();<br \/>\n}<br \/>\n[\/sourcecode]<\/p>\n<p>3. Create the <em><span style=\"color: #ff0000;\">PayPalPayment<\/span><\/em> object and launch the <em><span style=\"color: #ff0000;\">PaymentActivity<\/span><\/em> intent, for example, when a button is pressed:<\/p>\n<p>[sourcecode language=&#8221;java&#8221; wraplines=&#8221;false&#8221; collapse=&#8221;false&#8221;]<br \/>\npublic void onBuyPressed(View pressed) {<\/p>\n<p>    \/\/ PAYMENT_INTENT_SALE will cause the payment to complete immediately.<br \/>\n    \/\/ Change PAYMENT_INTENT_SALE to<br \/>\n    \/\/   &#8211; PAYMENT_INTENT_AUTHORIZE to only authorize payment and capture funds later.<br \/>\n    \/\/   &#8211; PAYMENT_INTENT_ORDER to create a payment for authorization and capture<br \/>\n    \/\/     later via calls from your server.<\/p>\n<p>    PayPalPayment payment = new PayPalPayment(new BigDecimal(&quot;1.75&quot;), &quot;USD&quot;, &quot;hipster jeans&quot;,<br \/>\n            PayPalPayment.PAYMENT_INTENT_SALE);<\/p>\n<p>    Intent intent = new Intent(this, PaymentActivity.class);<\/p>\n<p>    \/\/ send the same configuration for restart resiliency<br \/>\n    intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);<\/p>\n<p>    intent.putExtra(PaymentActivity.EXTRA_PAYMENT, payment);<\/p>\n<p>    startActivityForResult(intent, 0);<br \/>\n}<br \/>\n[\/sourcecode]<\/p>\n<p>4. Implement <span style=\"color: #ff0000;\"><em>onActivityResult()<\/em><\/span>:<\/p>\n<p>[sourcecode language=&#8221;java&#8221; wraplines=&#8221;false&#8221; collapse=&#8221;false&#8221;]<br \/>\n@Override<br \/>\nprotected void onActivityResult (int requestCode, int resultCode, Intent data) {<br \/>\n    if (resultCode == Activity.RESULT_OK) {<br \/>\n        PaymentConfirmation confirm = data.getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION);<br \/>\n        if (confirm != null) {<br \/>\n            try {<br \/>\n                Log.i(&quot;paymentExample&quot;, confirm.toJSONObject().toString(4));<\/p>\n<p>                \/\/ TODO: send &#8216;confirm&#8217; to your server for verification.<br \/>\n                \/\/ see https:\/\/developer.paypal.com\/webapps\/developer\/docs\/integration\/mobile\/verify-mobile-payment\/<br \/>\n                \/\/ for more details.<\/p>\n<p>            } catch (JSONException e) {<br \/>\n                Log.e(&quot;paymentExample&quot;, &quot;an extremely unlikely failure occurred: &quot;, e);<br \/>\n            }<br \/>\n        }<br \/>\n    }<br \/>\n    else if (resultCode == Activity.RESULT_CANCELED) {<br \/>\n        Log.i(&quot;paymentExample&quot;, &quot;The user canceled.&quot;);<br \/>\n    }<br \/>\n    else if (resultCode == PaymentActivity.RESULT_EXTRAS_INVALID) {<br \/>\n        Log.i(&quot;paymentExample&quot;, &quot;An invalid Payment or PayPalConfiguration was submitted. Please see the docs.&quot;);<br \/>\n    }<br \/>\n}<br \/>\n[\/sourcecode]<\/p>\n<p>5. <a href=\"https:\/\/developer.paypal.com\/webapps\/developer\/docs\/integration\/mobile\/verify-mobile-payment\/\">Send the proof of payment to your servers for verification<\/a>, as well as any other processing required for your business fulfilment.<\/p>\n<p>Tip: At this point, the payment has been completed, and the user has been charged. If you can&#8217;t reach your server, it is important that you save the proof of payment and try again later.<\/p>\n<p><strong>2. Future Payments:<\/strong><\/p>\n<p>This section will show how to obtain a user&#8217;s authorized consent for future payments using their PayPal account.<\/p>\n<p>First, you must <a href=\"https:\/\/github.com\/paypal\/PayPal-Android-SDK\/blob\/master\/docs\/future_payments_mobile.md#obtain-customer-consent\">obtain customer&#8217;s consent<\/a> to take payments from their PayPal account. PayPal Android SDK presents a UI for your user to authenticate using their PayPal account and asks user to consent to <a href=\"http:\/\/tools.ietf.org\/html\/rfc6749#page-23\">OAuth access token scope<\/a> to use PayPal for future payments.<\/p>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2015\/01\/future_consent.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-16883\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/01\/future_consent.png\" alt=\"future_consent\" width=\"272\" height=\"485\" \/><\/a><\/p>\n<p>It returns an OAuth2 authorization code to your app. Send the authorisation code to your server, which then <a href=\"https:\/\/github.com\/paypal\/PayPal-Android-SDK\/blob\/master\/docs\/future_payments_server.md#obtain-oauth2-tokens\">exchanges the code for OAuth2 access and refresh tokens<\/a>.<\/p>\n<p>Later, when initiating a pre-consented payment, you must <a href=\"https:\/\/github.com\/paypal\/PayPal-Android-SDK\/blob\/master\/docs\/future_payments_mobile.md#obtain-an-application-correlation-id\">obtain a Client Metadata ID<\/a>.\u00a0The PayPal Android SDK provides a Cient Metadata ID. Sends the client metadata ID and transaction information to your server.\u00a0Your server then <a href=\"https:\/\/github.com\/paypal\/PayPal-Android-SDK\/blob\/master\/docs\/future_payments_server.md\">uses its OAuth2 tokens, Client Metadata ID, and transaction info to create a payment<\/a>.<\/p>\n<p><strong>Inside your Activity code:<\/strong><\/p>\n<p>1.\u00a0Create a <em><code>PayPalConfiguration<\/code><\/em> object. This object allows you to configure various aspects of the SDK.<\/p>\n<p>[sourcecode language=&#8221;java&#8221; wraplines=&#8221;false&#8221; collapse=&#8221;false&#8221;]<br \/>\nprivate static PayPalConfiguration config = new PayPalConfiguration()<\/p>\n<p>        \/\/ Start with mock environment.  When ready, switch to sandbox (ENVIRONMENT_SANDBOX)<br \/>\n        \/\/ or live (ENVIRONMENT_PRODUCTION)<br \/>\n        .environment(PayPalConfiguration.ENVIRONMENT_NO_NETWORK)<\/p>\n<p>        .clientId(&quot;&lt;YOUR_CLIENT_ID&gt;&quot;)<\/p>\n<p>        \/\/ Minimally, you will need to set three merchant information properties.<br \/>\n        \/\/ These should be the same values that you provided to PayPal when you registered your app.<br \/>\n        .merchantName(&quot;Hipster Store&quot;)<br \/>\n        .merchantPrivacyPolicyUri(Uri.parse(&quot;https:\/\/www.example.com\/privacy&quot;))<br \/>\n        .merchantUserAgreementUri(Uri.parse(&quot;https:\/\/www.example.com\/legal&quot;));<\/p>\n<p>[\/sourcecode]<\/p>\n<p>2. Start <span style=\"color: #ff0000;\"><em>PayPalService<\/em><\/span> when your activity is created and stop it upon destruction:<\/p>\n<p>[sourcecode language=&#8221;java&#8221; wraplines=&#8221;false&#8221; collapse=&#8221;false&#8221;]<br \/>\n@Override<br \/>\nprotected void onCreate(Bundle savedInstanceState) {<br \/>\n    super.onCreate(savedInstanceState);<br \/>\n    setContentView(R.layout.activity_main);<\/p>\n<p>    Intent intent = new Intent(this, PayPalService.class);<\/p>\n<p>    intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);<\/p>\n<p>    startService(intent);<br \/>\n}<\/p>\n<p>@Override<br \/>\npublic void onDestroy() {<br \/>\n    stopService(new Intent(this, PayPalService.class));<br \/>\n    super.onDestroy();<br \/>\n}<br \/>\n[\/sourcecode]<\/p>\n<p>3. Launch the <span style=\"color: #ff0000;\">PayPalFuturePaymentActivity<\/span>, for example, when a button is pressed:<\/p>\n<p>[sourcecode language=&#8221;java&#8221; wraplines=&#8221;false&#8221; collapse=&#8221;false&#8221;]<br \/>\npublic void onFuturePaymentPressed(View pressed) {<br \/>\n    Intent intent = new Intent(SampleActivity.this, PayPalFuturePaymentActivity.class);<\/p>\n<p>    \/\/ send the same configuration for restart resiliency<br \/>\n    intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);<\/p>\n<p>    startActivityForResult(intent, REQUEST_CODE_FUTURE_PAYMENT);<br \/>\n}<br \/>\n[\/sourcecode]<\/p>\n<p>4. Implement <em><span style=\"color: #ff0000;\">onActivityResult()<\/span><\/em>:<\/p>\n<p>[sourcecode language=&#8221;java&#8221; wraplines=&#8221;false&#8221; collapse=&#8221;false&#8221;]<br \/>\n@Override<br \/>\nprotected void onActivityResult (int requestCode, int resultCode, Intent data) {<br \/>\n    if (resultCode == Activity.RESULT_OK) {<br \/>\n        PayPalAuthorization auth = data<br \/>\n                .getParcelableExtra(PayPalFuturePaymentActivity.EXTRA_RESULT_AUTHORIZATION);<br \/>\n        if (auth != null) {<br \/>\n            try {<br \/>\n                String authorization_code = auth.getAuthorizationCode();<\/p>\n<p>                sendAuthorizationToServer(auth);<\/p>\n<p>            } catch (JSONException e) {<br \/>\n                Log.e(&quot;FuturePaymentExample&quot;, &quot;an extremely unlikely failure occurred: &quot;, e);<br \/>\n            }<br \/>\n        }<br \/>\n    } else if (resultCode == Activity.RESULT_CANCELED) {<br \/>\n        Log.i(&quot;FuturePaymentExample&quot;, &quot;The user canceled.&quot;);<br \/>\n    } else if (resultCode == PayPalFuturePaymentActivity.RESULT_EXTRAS_INVALID) {<br \/>\n        Log.i(&quot;FuturePaymentExample&quot;,<br \/>\n                &quot;Probably the attempt to previously start the PayPalService had an invalid PayPalConfiguration. Please see the docs.&quot;);<br \/>\n    }<br \/>\n}<br \/>\n[\/sourcecode]<\/p>\n<p>5. Send the authorization response to your server in order to complete the process.<\/p>\n<p>[sourcecode language=&#8221;java&#8221; wraplines=&#8221;false&#8221; collapse=&#8221;false&#8221;]<br \/>\nprivate void sendAuthorizationToServer(PayPalAuthorization authorization) {<\/p>\n<p>    \/\/ TODO:<br \/>\n    \/\/ Send the authorization response to your server, where it can exchange the authorization code<br \/>\n    \/\/ for OAuth access and refresh tokens.<br \/>\n    \/\/<br \/>\n    \/\/ Your server must then store these tokens, so that your server code can execute payments<br \/>\n    \/\/ for this user in the future.<\/p>\n<p>}<br \/>\n[\/sourcecode]<\/p>\n<p>6. Obtain a Client Metadata ID:<\/p>\n<p>When initiating a pre-consented payment (a &#8220;future payment&#8221;) from a mobile device, your mobile application must obtain a Client Metadata Id from the Mobile SDK to pass to your server. Your server must include this Client Metadata ID in its payment request sent to PayPal. PayPal uses this Client Metadata ID to verify that the payment is originating from a valid, user-consented device+application.<\/p>\n<p>Example:<\/p>\n<p>[sourcecode language=&#8221;java&#8221; wraplines=&#8221;false&#8221; collapse=&#8221;false&#8221;]<br \/>\npublic void onFuturePaymentPurchasePressed(View pressed) {<br \/>\n\/\/ Get the Client Metadata ID from the SDK<br \/>\nString metadataId = PayPalConfiguration.getClientMetadataId(this);<\/p>\n<p>\/\/ TODO: Send metadataId and transaction details to your server for processing with<br \/>\n\/\/ PayPal&#8230;<br \/>\n}<br \/>\n[\/sourcecode]<\/p>\n<p>When your server makes its payment request to PayPal, it must include a PayPal-Client-Metadata-Id HTTP header with this Client Metadata ID value obtained from the SDK.<\/p>\n<p><strong>Future Payments Server-Side Integration:<\/strong><\/p>\n<p>Once a user has authorized your app to use their PayPal account, you can then use that authorization to obtain tokens that let you create future payments from that user.<\/p>\n<p>Read <a href=\"https:\/\/github.com\/paypal\/PayPal-Android-SDK\/blob\/master\/docs\/future_payments_server.md\">Future Payments Server-Side Integration<\/a> to exchange the authorization code for OAuth2 tokens and create payments with an access token and a Metadata ID.<\/p>\n<h3><strong>3. Profile Sharing:<\/strong><\/h3>\n<p>This section will show how to obtain a user&#8217;s authorized consent for profile sharing from their PayPal account.<\/p>\n<p>Your customer logs in to PayPal and consents to PayPal sharing information with you:<\/p>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2015\/01\/profile_sharing_user_consent.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone  wp-image-16890\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/01\/profile_sharing_user_consent.png\" alt=\"profile_sharing_user_consent\" width=\"349\" height=\"621\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p>You must <a href=\"https:\/\/github.com\/paypal\/PayPal-Android-SDK\/blob\/master\/docs\/profile_sharing_mobile.md#obtain-customer-consent\">obtain customer consent<\/a> to share information from their PayPal account.<\/p>\n<p>How this works:<\/p>\n<ul class=\"task-list\">\n<li>On the PayPal Developer site&#8230;\n<ol class=\"task-list\">\n<li>Specify the pieces of information that you wish your customers to share with you.<\/li>\n<\/ol>\n<\/li>\n<li>The PayPal Android SDK&#8230;\n<ol class=\"task-list\">\n<li>Presents UI for your user to authenticate using their PayPal account.<\/li>\n<li>Asks your user to consent to <a href=\"http:\/\/tools.ietf.org\/html\/rfc6749#page-23\">OAuth access token scope<\/a> to use PayPal for profile sharing.<\/li>\n<li>Returns an OAuth2 authorization code to your app.<\/li>\n<\/ol>\n<\/li>\n<li>Your app&#8230;\n<ol class=\"task-list\">\n<li>Specifies the required scopes in SDK request<\/li>\n<li>Receives an OAuth2 authorization code from the SDK.<\/li>\n<li>Sends the authorization code to your server, which then <a href=\"https:\/\/github.com\/paypal\/PayPal-Android-SDK\/blob\/master\/docs\/profile_sharing_server.md#obtain-oauth2-tokens\">exchanges the code for OAuth2 access and refresh tokens<\/a>.<\/li>\n<li>Your server then <a href=\"https:\/\/github.com\/paypal\/PayPal-Android-SDK\/blob\/master\/docs\/profile_sharing_server.md\">uses its OAuth2 tokens to request the relevant customer information from PayPal<\/a>.<\/li>\n<\/ol>\n<\/li>\n<\/ul>\n<h3><strong>Specify Desired Information for Sharing On the PayPal Developer site:<\/strong><\/h3>\n<ol class=\"task-list\">\n<li>Log in to the <a href=\"https:\/\/developer.paypal.com\/\">PayPal Developer site<\/a>.<\/li>\n<li>Select your app.<\/li>\n<li>Under <code>APP CAPABILITIES<\/code> select <code>Log In with PayPal<\/code>, and click <code>Advanced options<\/code>.<\/li>\n<li>Under <code>Information requested from customers<\/code> select the items (&#8220;scope attributes&#8221;) you wish to have shared.<\/li>\n<li>If you provide Privacy Policy and User Agreement URLs under <code>Links shown on customer consent page<\/code>, these will override the corresponding URLs that you provide below in the <code>PayPalConfiguration<\/code> object.<\/li>\n<\/ol>\n<h3>Obtain Customer Consent:<\/h3>\n<p>1. Create a <span style=\"color: #ff0000;\"><code>PayPalConfiguration<\/code><\/span> object. This object allows you to configure various aspects of the SDK:<\/p>\n<p>[sourcecode language=&#8221;java&#8221; wraplines=&#8221;false&#8221; collapse=&#8221;false&#8221;]<br \/>\nprivate static PayPalConfiguration config = new PayPalConfiguration()<\/p>\n<p>        \/\/ Start with mock environment.  When ready, switch to sandbox (ENVIRONMENT_SANDBOX)<br \/>\n        \/\/ or live (ENVIRONMENT_PRODUCTION)<br \/>\n        .environment(PayPalConfiguration.ENVIRONMENT_NO_NETWORK)<\/p>\n<p>        .clientId(&quot;&lt;YOUR_CLIENT_ID&gt;&quot;)<\/p>\n<p>        \/\/ Minimally, you will need to set three merchant information properties.<br \/>\n        \/\/ These should be the same values that you provided to PayPal when you registered your app.<br \/>\n        .merchantName(&quot;Hipster Store&quot;)<br \/>\n        .merchantPrivacyPolicyUri(Uri.parse(&quot;https:\/\/www.example.com\/privacy&quot;))<br \/>\n        .merchantUserAgreementUri(Uri.parse(&quot;https:\/\/www.example.com\/legal&quot;));<br \/>\n[\/sourcecode]<\/p>\n<p>2. Start <span style=\"color: #ff0000;\">PayPalService<\/span> when your activity is created and stop it upon destruction:<\/p>\n<p>[sourcecode language=&#8221;java&#8221; wraplines=&#8221;false&#8221; collapse=&#8221;false&#8221;]<br \/>\n@Override<br \/>\nprotected void onCreate(Bundle savedInstanceState) {<br \/>\n    super.onCreate(savedInstanceState);<br \/>\n    setContentView(R.layout.activity_main);<\/p>\n<p>    Intent intent = new Intent(this, PayPalService.class);<\/p>\n<p>    intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);<\/p>\n<p>    startService(intent);<br \/>\n}<\/p>\n<p>@Override<br \/>\npublic void onDestroy() {<br \/>\n    stopService(new Intent(this, PayPalService.class));<br \/>\n    super.onDestroy();<br \/>\n}<br \/>\n[\/sourcecode]<\/p>\n<p>3. Launch the <em><span style=\"color: #ff0000;\">PayPalProfileSharing<\/span><\/em> activity, for example, when a button is pressed:<\/p>\n<p>[sourcecode language=&#8221;java&#8221; wraplines=&#8221;false&#8221; collapse=&#8221;false&#8221;]<br \/>\npublic void onProfileSharingPressed(View pressed) {<br \/>\n    Intent intent = new Intent(SampleActivity.this, PayPalProfileSharingActivity.class);<\/p>\n<p>    \/\/ send the same configuration for restart resiliency<br \/>\n    intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);<\/p>\n<p>    intent.putExtra(PayPalProfileSharingActivity.EXTRA_REQUESTED_SCOPES, getOauthScopes());<\/p>\n<p>    startActivityForResult(intent, REQUEST_CODE_PROFILE_SHARING);<br \/>\n}<br \/>\n[\/sourcecode]<\/p>\n<p>The <em><span style=\"color: #ff0000;\">PayPalOAuthScopes<\/span><\/em> are initialized with a set of scope names:<\/p>\n<p>[sourcecode language=&#8221;java&#8221; wraplines=&#8221;false&#8221; collapse=&#8221;false&#8221;]<br \/>\nprivate PayPalOAuthScopes getOauthScopes() {<br \/>\n    \/* create the set of required scopes<br \/>\n     * Note: see https:\/\/developer.paypal.com\/docs\/integration\/direct\/identity\/attributes\/ for mapping between the<br \/>\n     * attributes you select for this app in the PayPal developer portal and the scopes required here.<br \/>\n     *\/<br \/>\n    Set&lt;String&gt; scopes = new HashSet&lt;String&gt;(<br \/>\n            Arrays.asList(PayPalOAuthScopes.PAYPAL_SCOPE_EMAIL, PayPalOAuthScopes.PAYPAL_SCOPE_ADDRESS) );<br \/>\n    return new PayPalOAuthScopes(scopes);<br \/>\n}<br \/>\n[\/sourcecode]<\/p>\n<p>4. Implement <em><span style=\"color: #ff0000;\">onActivityResult()<\/span><\/em>:<\/p>\n<p>[sourcecode language=&#8221;java&#8221; wraplines=&#8221;false&#8221; collapse=&#8221;false&#8221;]<br \/>\n@Override<br \/>\nprotected void onActivityResult (int requestCode, int resultCode, Intent data) {<br \/>\n    if (resultCode == Activity.RESULT_OK) {<br \/>\n        PayPalAuthorization auth = data<br \/>\n                .getParcelableExtra(PayPalProfileSharingActivity.EXTRA_RESULT_AUTHORIZATION);<br \/>\n        if (auth != null) {<br \/>\n            try {<br \/>\n                String authorization_code = auth.getAuthorizationCode();<\/p>\n<p>                sendAuthorizationToServer(auth);<\/p>\n<p>            } catch (JSONException e) {<br \/>\n                Log.e(&quot;ProfileSharingExample&quot;, &quot;an extremely unlikely failure occurred: &quot;, e);<br \/>\n            }<br \/>\n        }<br \/>\n    } else if (resultCode == Activity.RESULT_CANCELED) {<br \/>\n        Log.i(&quot;ProfileSharingExample&quot;, &quot;The user canceled.&quot;);<br \/>\n    } else if (resultCode == PayPalProfileSharingActivity.RESULT_EXTRAS_INVALID) {<br \/>\n        Log.i(&quot;ProfileSharingExample&quot;,<br \/>\n                &quot;Probably the attempt to previously start the PayPalService had an invalid PayPalConfiguration. Please see the docs.&quot;);<br \/>\n    }<br \/>\n}<br \/>\n[\/sourcecode]<\/p>\n<p>5. Send the authorization response to your server in order to complete the process:<\/p>\n<p>[sourcecode language=&#8221;java&#8221; wraplines=&#8221;false&#8221; collapse=&#8221;false&#8221;]<br \/>\nprivate void sendAuthorizationToServer(PayPalAuthorization authorization) {<\/p>\n<p>    \/\/ TODO:<br \/>\n    \/\/ Send the authorization response to your server, where it can exchange the authorization code<br \/>\n    \/\/ for OAuth access and refresh tokens.<br \/>\n    \/\/<br \/>\n    \/\/ Your server must then store these tokens, so that your server code can use it<br \/>\n    \/\/ for getting user profile data in the future.<\/p>\n<p>}<br \/>\n[\/sourcecode]<\/p>\n<p><strong>Profile Sharing Server Side Integration:<\/strong><\/p>\n<p>Read <a href=\"https:\/\/github.com\/paypal\/PayPal-Android-SDK\/blob\/master\/docs\/profile_sharing_server.md\">Profile Sharing Server-Side Integration<\/a> to exchange the authorization code for OAuth2 tokens and retrieve the customer information from PayPal.<\/p>\n<h3><strong>The Sandbox Environment:<\/strong><\/h3>\n<p>PayPal provides a test environment, called Sandbox, using which developer can mock transactions to test and debug his\/her application.<\/p>\n<p>By using fictitious Sandbox <em>test accounts<\/em> and their associated authentication credentials in your PayPal API calls, you can test and debug your application without referencing any real PayPal users or their live PayPal accounts. The Sandbox lets you operate your application in a safe environment and provides you a way to fine tune your PayPal routines before moving your product into production.<\/p>\n<p>When you initiate a transaction using Sandbox test accounts, PayPal creates a mock transaction that behaves exactly like a transaction in the live environment.<\/p>\n<p>The PayPal Sandbox environment comprises the following:<\/p>\n<ul>\n<li>The <strong>Sandbox Test Site<\/strong> (<a href=\"https:\/\/www.sandbox.paypal.com\/\">https:\/\/www.sandbox.paypal.com\/<\/a>)<\/li>\n<li>The <strong>Sandbox Accounts page<\/strong> (accessed through <a href=\"https:\/\/developer.paypal.com\/\">https:\/\/developer.paypal.com<\/a>)<\/li>\n<\/ul>\n<p>During your testing phase, create and manage your set of test accounts from the Sandbox <em>Accounts page<\/em>. Use the <em>Sandbox test site<\/em> to review the transactions associated with the calls you make using your test accounts. \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 Log in to the Sandbox test site with the credentials from any of your test accounts to review the status of the mock transactions associated with that account.<\/p>\n<p><strong>PayPal Sandbox Account page:<\/strong><\/p>\n<p>For testing purpose we create Personal accounts to simulate payment from this Personal account.<\/p>\n<p>To create read about the personal account and how to create one for you follow the links below:<\/p>\n<p>1. <a href=\"https:\/\/developer.paypal.com\/webapps\/developer\/docs\/classic\/lifecycle\/sb_about-accounts\/\">What are Personal and Business accounts<\/a>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 2. <a href=\"https:\/\/developer.paypal.com\/webapps\/developer\/docs\/classic\/lifecycle\/sb_create-accounts\/\">How to create a Personal account<\/a><\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2015\/01\/Screen-Shot-2015-01-22-at-4.14.02-pm-copy.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-16892\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/01\/Screen-Shot-2015-01-22-at-4.14.02-pm-copy.png\" alt=\"Screen Shot 2015-01-22 at 4.14.02 pm copy\" width=\"972\" height=\"487\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p><strong>PayPal Sandbox test\u00a0site: <\/strong><\/p>\n<p>Log into PayPal Sandbox test\u00a0site\u00a0using one of your\u00a0Personal accounts. For example I have created a personal\u00a0account &#8220;develope@gmail.com&#8221; and after login using this account\u00a0I was landed on the following account details page.<\/p>\n<p><a href=\"\/blog\/wp-ttn-blog\/uploads\/2015\/01\/sandbox_testing.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-16893\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/01\/sandbox_testing.png\" alt=\"sandbox_testing\" width=\"861\" height=\"763\" \/><\/a><\/p>\n<p><strong>Going Live:<\/strong><\/p>\n<p>Once you&#8217;ve finished coding and debugging your application (including testing all your PayPal API calls in the <a href=\"https:\/\/developer.paypal.com\/webapps\/developer\/docs\/classic\/lifecycle\/ug_sandbox\/\">Sandbox<\/a>), go live by moving your application to PayPal&#8217;s production environment.<\/p>\n<p>Just follow the <a href=\"https:\/\/developer.paypal.com\/webapps\/developer\/docs\/classic\/lifecycle\/goingLive\/\">procedure of going live with your application<\/a>\u00a0and you are finished with integrating\u00a0PayPal SDK in your Android application.<\/p>\n<p>&nbsp;<\/p>\n<p>Cheers!!!<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I recently had to integrate a PayPal payment mechanism in one of my Android applications. So I thought to share my knowledge with the community that you can easily integrate payment mechanism in your application. What is PayPal? Established in 1998, PayPal is an American company allowing payments and money transfers to be made through [&hellip;]<\/p>\n","protected":false},"author":150,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":37},"categories":[518],"tags":[2175,2174,2173],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/16830"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/150"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=16830"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/16830\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=16830"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=16830"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=16830"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}