Card payment Using PayPal android

可紊 提交于 2019-12-13 03:21:29

问题


Can we implement Paypal with a card payment. e.g. Somebody doesn't have paypal account so he/she can pay using debit or credit card. Is there any way to implement paypal with card. Please help.


回答1:


Hi I know I am very late to answer this question but surely folks implementing Paypal in their app will get benefit from this! Android SDK for Paypal does not support Card Payment but yes "Rest API sdk for Paypal" has the capability. Include this in your build.gradle: compile 'com.paypal.sdk:rest-api-sdk:1.2.5'

And then try the following method:

public static final String  CLIENT_ID = "AQkquBDf1zctJOWGKWUEtKXm6qVhueUEMvXO_-MCI4DQQ4-LWvkDLIN2fGsd";
public static final String  CLIENT_SECRET = "EL1tVxAjhT7cJimnz5-Nsx9k2reTKSVfErNQF-CmrwJgxRtylkGTKlU4RvrX";

/**
     * @method getAccessToken is used to get AccessToken for performing authorised transcations
     * @param clientId credential that we get when we register our application on https://developer.paypal.com/
     * @param clientSecret credential that we get when we register our application on https://developer.paypal.com/
     * @return String accessToken
     */
    public static final String getAccessToken(String clientId, String clientSecret){
        Log.i(TAG,"GetAccessToken called");
        String accessToken = "";
        long expiresIn;

        try {
            OAuthTokenCredential oAuthTokenCredential = new OAuthTokenCredential(clientId, clientSecret, getSdKConfig());
            expiresIn = oAuthTokenCredential.expiresIn();
            accessToken = oAuthTokenCredential.getAccessToken();
             Log.i(TAG, "AccessToken: "+accessToken);
        } catch (PayPalRESTException e) {
            e.printStackTrace();
        }
        return accessToken;
    }

 /**
         * @method makeDirectPayment is used for making direct payment via credit cards. Customers who don't have paypal account can pay via this method.
         * @return String with Payment Id and Payment status
         */
        public static final String makeDirectPayment(){

            String accessToken = getAccessToken(Constants.CLIENT_ID, Constants.CLIENT_SECRET);
            String message = "";
            if (accessToken != null && !accessToken.equals("")){
                APIContext apiContext = new APIContext(accessToken);
                apiContext.setConfigurationMap(getSdKConfig());

                CreditCard creditCard = new CreditCard();
                creditCard.setType("visa");
                creditCard.setNumber("4446283280247004");
                creditCard.setExpireMonth(11);
                creditCard.setExpireYear(2019);
                creditCard.setFirstName("Test");
                creditCard.setLastName("Shopper");

                FundingInstrument fundingInstrument = new FundingInstrument();
                fundingInstrument.setCreditCard(creditCard);

                List<FundingInstrument> fundingInstrumentList = new ArrayList<>();
                fundingInstrumentList.add(fundingInstrument);

                Payer payer = new Payer();
                payer.setFundingInstruments(fundingInstrumentList);
                payer.setPaymentMethod("credit_card");

                Amount amount = new Amount();
                amount.setCurrency("EUR");
                amount.setTotal("50");

                Transaction transaction = new Transaction();
                transaction.setDescription("Creating Direct Payment with Credit Card");
                transaction.setAmount(amount);

                List<Transaction> transactionList = new ArrayList<>();
                transactionList.add(transaction);

                Payment payment = new Payment();
                payment.setIntent("sale");
                payment.setTransactions(transactionList);
                payment.setPayer(payer);

                try {
                    Payment createdPayment = payment.create(apiContext);

                    if (createdPayment != null){
                        Log.i(TAG,"Payment object: "+createdPayment.toJSON());
                        message = "Payment Id: " + createdPayment.getId() + " Payment status: "+createdPayment.getState();
                        Log.i(TAG, message);
                    }
                } catch (PayPalRESTException e) {
                    e.printStackTrace();
                }

            }
            return message;
        }

Note for simplicity I have used everything static but you can maintain your own UI to get no of items, its pricing, credit card details from user.



来源:https://stackoverflow.com/questions/15964851/card-payment-using-paypal-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!