This section will show how to obtain a user's authorized consent for profile sharing from their PayPal account.
If you haven't already, see the README for an initial overview and instructions for adding the SDK to your project.
You must obtain customer consent to share information from their PayPal account. How this works:
- On the PayPal Developer site...
- Specify the pieces of information that you wish your customers to share with you.
- The PayPal Android SDK...
- Presents UI for your user to authenticate using their PayPal account.
- Asks your user to consent to OAuth access token scope to use PayPal for profile sharing.
- Returns an OAuth2 authorization code to your app.
- Your app...
- Specifies the required scopes in SDK request
- Receives an OAuth2 authorization code from the SDK.
- Sends the authorization code to your server, which then exchanges the code for OAuth2 access and refresh tokens.
- Your server then uses its OAuth2 tokens to request the relevant customer information from PayPal.
Notes:
- See
PayPalOAuthScopes
for a complete list of scope-values available to the PayPal Android SDK. - See Log In with PayPal user attributes for a complete list of available scope attributes.
- Log in to the PayPal Developer site.
- Select your app.
- Under
APP CAPABILITIES
selectLog In with PayPal
, and clickAdvanced options
. - Under
Information requested from customers
select the items ("scope attributes") you wish to have shared. - If you provide Privacy Policy and User Agreement URLs under
Links shown on customer consent page
, these will override the corresponding URLs that you provide below in thePayPalConfiguration
object.
The sample app provides a more complete example. However, at minimum, you must:
-
Add PayPal Android SDK dependency to your
build.gradle
file as shown in README.md -
Create a
PayPalConfiguration
object. This object allows you to configure various aspects of the SDK.private static PayPalConfiguration config = new PayPalConfiguration() // Start with mock environment. When ready, switch to sandbox (ENVIRONMENT_SANDBOX) // or live (ENVIRONMENT_PRODUCTION) .environment(PayPalConfiguration.ENVIRONMENT_NO_NETWORK) .clientId("<YOUR_CLIENT_ID>") // Minimally, you will need to set three merchant information properties. // These should be the same values that you provided to PayPal when you registered your app. .merchantName("Example Store") .merchantPrivacyPolicyUri(Uri.parse("https://www.example.com/privacy")) .merchantUserAgreementUri(Uri.parse("https://www.example.com/legal"));
-
Start
PayPalService
when your activity is created and stop it upon destruction:@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent intent = new Intent(this, PayPalService.class); intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config); startService(intent); } @Override public void onDestroy() { stopService(new Intent(this, PayPalService.class)); super.onDestroy(); }
-
Launch the PayPal Profile Sharing activity, for example, when a button is pressed:
public void onProfileSharingPressed(View pressed) { Intent intent = new Intent(SampleActivity.this, PayPalProfileSharingActivity.class); // send the same configuration for restart resiliency intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config); intent.putExtra(PayPalProfileSharingActivity.EXTRA_REQUESTED_SCOPES, getOauthScopes()); startActivityForResult(intent, REQUEST_CODE_PROFILE_SHARING); }
The
PayPalOAuthScopes
are initialized with a set of scope names:private PayPalOAuthScopes getOauthScopes() { /* create the set of required scopes * Note: see https://developer.paypal.com/docs/integration/direct/identity/attributes/ for mapping between the * attributes you select for this app in the PayPal developer portal and the scopes required here. */ Set<String> scopes = new HashSet<String>( Arrays.asList(PayPalOAuthScopes.PAYPAL_SCOPE_EMAIL, PayPalOAuthScopes.PAYPAL_SCOPE_ADDRESS) ); return new PayPalOAuthScopes(scopes); }
-
Implement
onActivityResult()
:@Override protected void onActivityResult (int requestCode, int resultCode, Intent data) { if (resultCode == Activity.RESULT_OK) { PayPalAuthorization auth = data .getParcelableExtra(PayPalProfileSharingActivity.EXTRA_RESULT_AUTHORIZATION); if (auth != null) { try { String authorization_code = auth.getAuthorizationCode(); sendAuthorizationToServer(auth); } catch (JSONException e) { Log.e("ProfileSharingExample", "an extremely unlikely failure occurred: ", e); } } } else if (resultCode == Activity.RESULT_CANCELED) { Log.i("ProfileSharingExample", "The user canceled."); } else if (resultCode == PayPalProfileSharingActivity.RESULT_EXTRAS_INVALID) { Log.i("ProfileSharingExample", "Probably the attempt to previously start the PayPalService had an invalid PayPalConfiguration. Please see the docs."); } }
-
Send the authorization response to your server in order to complete the process.
private void sendAuthorizationToServer(PayPalAuthorization authorization) { // TODO: // Send the authorization response to your server, where it can exchange the authorization code // for OAuth access and refresh tokens. // // Your server must then store these tokens, so that your server code can use it // for getting user profile data in the future. }
Read Profile Sharing Server-Side Integration to exchange the authorization code for OAuth2 tokens and retrieve the customer information from PayPal.