手机客户端paypal接入方式

今天研究paypal接入项目中,发现国内的文档确实少的可怜。看英文文档还是看的有点费劲啊。记录下自己的开发过程,希望可以帮到大家。


目前sandbox环境;

1、去官方网站下载androidsdk,官方的demo就写的挺好的。

https://developer.paypal.com/webapps/developer/index

手机客户端paypal接入方式_第1张图片


2、注册开发者账号,登录第一步的网站。点击下图中的“create app”。client id填入到demo的client id 的对应字符串中

private static final String CONFIG_CLIENT_ID = 当前的client id;

//测试的环境切换到沙盒模式

private static final String CONFIG_ENVIRONMENT = PayPalConfiguration.ENVIRONMENT_SANDBOX;


注意:测试的账号如果是个人申请的需要将此账号添加到沙盒的测试账号中去,否则在测试过程中登录会提示“账号或者密码错误”。因为个人注册的账号是在正式环境中的。

而沙盒模式需要的是测试账号。





3、创建好后app(如下图),appname就是应用的名称,可以随便填写。


手机客户端paypal接入方式_第2张图片




以下就是我客户端中的主要代码:

public class PayManager {

private static final String TAG = "PayManager";

Activity activity;

private RechargeCoinType coinType = null ; //接收套餐

public PayManager(Activity activity) {

this.activity = activity;

}

/**  paypal支付  */

private static final String CONFIG_ENVIRONMENT = PayPalConfiguration.ENVIRONMENT_SANDBOX;

private static final String CONFIG_CLIENT_ID = client id;

private static final int REQUEST_CODE_PAYMENT = 1;

private static PayPalConfiguration config = new PayPalConfiguration()

    .environment(CONFIG_ENVIRONMENT)

    .clientId(CONFIG_CLIENT_ID)

    // The following are only used in PayPalFuturePaymentActivity.

    .merchantName("Hipster Store")

    .merchantPrivacyPolicyUri(Uri.parse("https://www.example.com/privacy"))

    .merchantUserAgreementUri(Uri.parse("https://www.example.com/legal"));

public void clickPayPal(RechargeCoinType coinType) {

Intent intent = new Intent(activity, PayPalService.class);

        intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);

        activity.startService(intent);

        

        PayPalPayment thingToBuy = getThingToBuy(coinType);

        Intent intent2 = new Intent(activity, PaymentActivity.class);

        intent2.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);


        intent2.putExtra(PaymentActivity.EXTRA_PAYMENT, thingToBuy);


        activity.startActivityForResult(intent2, REQUEST_CODE_PAYMENT);

}

private PayPalPayment getThingToBuy(RechargeCoinType coinType) {

PayPalPayment thingToBuy = new PayPalPayment(new BigDecimal(coinType.money + ""), "USD", "Test",

PayPalPayment.PAYMENT_INTENT_SALE);

        return thingToBuy;

    }

public void stopPayPalServer() {

activity.stopService(new Intent(activity, PayPalService.class));

}

public void payPalCallBack(int requestCode, int resultCode, Intent data) {

if (requestCode == REQUEST_CODE_PAYMENT) {

if (resultCode == Activity.RESULT_OK) { 

PaymentConfirmation confirm =

                        data.getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION);

        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

                        */


// 返回这一步表示支付成功


//返回的数据格式


  /*PayPal返回

                {

    "response": {

        "state": "",

        "id": "", //如果商家自己要做一些,比如加金币等一些后续操作,客户端需要把这个id传给服务端,服务端会拿这个id去paypal验证。确认支付成功了,服务端进行加币等后续操作

        "create_time": "",

        "intent": ""

    },

    "client": {

        "platform": "",

        "paypal_sdk_version": "",

        "product_name": "",

        "environment": ""

    },

    "response_type": "payment"

}

*/


                        Toast.makeText(activity,

                                "PayPal已接收支付信息", Toast.LENGTH_LONG)

                                .show();


                        Log.i(TAG, "paypal返回的数据.resultCode:" + resultCode);

                        Log.i(TAG, "paypal返回的数据.resultCode:" + resultCode);

                        Log.i(TAG, "paypal返回的数据.resultCode:" + resultCode);

                        Log.i(TAG, "paypal返回的数据.resultCode:" + resultCode);

                        Log.i(TAG, "paypal返回的数据.resultCode:" + resultCode);

                        Log.i(TAG, "paypal返回的数据.resultCode:" + resultCode);

                        Log.i(TAG, "paypal返回的数据.resultCode:" + resultCode);


            } catch (JSONException e) {

                Log.e("FuturePaymentExample", "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.");

        }

}

}


}



你可能感兴趣的:(手机客户端paypal接入方式)