简体   繁体   中英

Capture Paypal Payment in android

I have authorized Paypal amount using PayPalPayment.PAYMENT_INTENT_AUTHORIZE intent in PaymentActivity now i have authorizationId but how to capture this amount now?

Code to start PaymentActivity

PayPalPayment thingToBuy = getStuffToBuy(PayPalPayment.PAYMENT_INTENT_AUTHORIZE);

        /*
         * See getStuffToBuy(..) for examples of some available payment options.
         */

        Intent intent = new Intent(SampleActivity.this, PaymentActivity.class);

        // send the same configuration for restart resiliency
        intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);

        intent.putExtra(PaymentActivity.EXTRA_PAYMENT, thingToBuy);

        startActivityForResult(intent, REQUEST_CODE_PAYMENT);

onActivityResult

    if (requestCode == REQUEST_CODE_PAYMENT) {
        if (resultCode == Activity.RESULT_OK) {
            PaymentConfirmation confirm =
                    data.getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION);
            ProofOfPayment proof = confirm.getProofOfPayment();
            PayPalPayment payment = confirm.getPayment();
            payment.enablePayPalShippingAddressesRetrieval(true);
            JSONObject object = proof.toJSONObject();
            String authID = "";

            try {
                authID = object.getString("authorization_id");
            } catch (JSONException e) {
                e.printStackTrace();
            }

            if (confirm != null) {
                try {
                    Log.i(TAG, confirm.toJSONObject().toString(4));
                    Log.i(TAG, confirm.getPayment().toJSONObject().toString(4));
                    /**
                     *  TODO: send 'confirm' (and possibly confirm.getPayment() to your server for verification
                     * or consent completion.
                     * See https://developer.paypal.com/webapps/developer/docs/integration/mobile/verify-mobile-payment/
                     * for more details.
                     *
                     * For sample mobile backend interactions, see
                     * https://github.com/paypal/rest-api-sdk-python/tree/master/samples/mobile_backend
                     */
                    displayResultText(authID);


                } catch (JSONException e) {
                    Log.e(TAG, "an extremely unlikely failure occurred: ", e);
                }
            }
        } else if (resultCode == Activity.RESULT_CANCELED) {
            Log.i(TAG, "The user canceled.");
        } else if (resultCode == PaymentActivity.RESULT_EXTRAS_INVALID) {
            Log.i(
                    TAG,
                    "An invalid Payment or PayPalConfiguration was submitted. Please see the docs.");
        }
    }

Now I have authorization id in authId now i want to capture this amount. Can I do this within my app without any server side work.

  1. Make a post request to https://api.sandbox.paypal.com/v1/oauth2/token

2.Set the Authorization type to Basic Auth and then set the client_id into the Username field and secret into the Password field.

3.Set the Body to x-www-form-urlencoded and then set grant_type in the key field and client_credentials in the value field.

In response to above you will get an access token.

Now make GET request to https://api.sandbox.paypal.com/v1/payments/payment/PAY-5YK922393D847794YKER7MUI with Headers, Content-Type -- application/json" and Authorization -- Bearer accessToken_you_get_in_above_call .(PAY-5YK922393D847794YKER7MUI this is your payment id)

In response of above you will get an object as "state": "approved", if it is approved means your payment has been successfully done.

Note:- In the first response you will get an access token, but this token is valid for only few seconds , so you have to get another access token if it is expired.

For more details refer:- https://developer.paypal.com/webapps/developer/docs/integration/mobile/verify-mobile-payment/ https://developer.paypal.com/docs/integration/direct/make-your-first-call/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM