android端集成paypal

因为最近项目中需要集成paypal,所以集成了一下,发现网上的有些文章过于老旧,所以自己写一篇踩坑记,首先是去官网申请账号,到时候会分配给你一个client_id
以上准备都做好了 就新建一个项目

  1. 引入paypel的包
compile('com.paypal.sdk:paypal-android-sdk:2.15.3') 
{ exclude group: 'io.card'    }//  如果不想支持信用卡支付 就不用加这句话
  1. 新建一个activity 配置
private static PayPalConfiguration config = 
new PayPalConfiguration()
 .environment(PayPalConfiguration.ENVIRONMENT_SANDBOX)
.clientId("");

这里就输入你的client_id

3.然后在oncreate中开启服务

// 开启PayPal服务
        Intent intent = new Intent(this, PayPalService.class);
        intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, paypalConfig);
        startService(intent);

4.买东西


    public void onBuyPressed(View pressed) {
        //创建支付对象,用于传过去给PayPal服务器进行收款
        PayPalPayment thingToBuy = getThingToBuy(PayPalPayment.PAYMENT_INTENT_SALE);
        Intent intent = new Intent(this, PaymentActivity.class);
        intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, paypalConfig);
        intent.putExtra(PaymentActivity.EXTRA_PAYMENT, thingToBuy);
        //这里直接调起PayPal的sdk进行付款操作
        startActivityForResult(intent, 1);
    }

    //这里只传一个总价格或者单个产品的信息收款情况
    private PayPalPayment getThingToBuy(String paymentIntent) {
        return new PayPalPayment(new BigDecimal("0.01"), "USD", "sample item",
                paymentIntent);

    }

    //这里是购买一系列产品创建购买对象
    private PayPalPayment getStuffToBuy(String paymentIntent) {
        PayPalItem[] items =
                {
                        new PayPalItem("sample item #1", 2, new BigDecimal("87.50"), "USD",
                                "sku-12345678"),
                        new PayPalItem("free sample item #2", 1, new BigDecimal("0.00"),
                                "USD", "sku-zero-price"),
                        new PayPalItem("sample item #3 with a longer name", 6, new BigDecimal("37.99"),
                                "USD", "sku-33333")
                };
        BigDecimal subtotal = PayPalItem.getItemTotal(items);
        BigDecimal shipping = new BigDecimal("7.21");
        BigDecimal tax = new BigDecimal("4.67");
        PayPalPaymentDetails paymentDetails = new PayPalPaymentDetails(shipping, subtotal, tax);
        BigDecimal amount = subtotal.add(shipping).add(tax);
        PayPalPayment payment = new PayPalPayment(amount, "USD", "sample item", paymentIntent);
        payment.items(items).paymentDetails(paymentDetails);
//--- set other optional fields like invoice_number, custom field, and soft_descriptor
        payment.custom("This is text that will be associated with the payment that the app can use.");
        return payment;
    }

5.接收回调

 /**
     * 接收支付结果的回调.
     */
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1 && resultCode == Activity.RESULT_OK) {
            PaymentConfirmation confirm =
                    data.getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION);
            if (confirm != null) {
                try {
                    //这里可以把PayPal带回来的json数据传给服务器以确认你的款项是否收到或者收全
                    //可以直接把 confirm.toJSONObject() 这个带给服务器,
                    //得到服务器返回的结果,你就可以跳转成功页面或者做相应的处理了
                    Log.i(TAG, confirm.toJSONObject().toString(4));
//                    confirm.toJSONObject().toString();
                    Log.i(TAG, confirm.getPayment().toJSONObject().toString(4));
                    Log.i(TAG, confirm.toJSONObject().toString());
                } 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.");
        }


    }

6.注销service

 @Override
    protected void onStop() {
        stopService(new Intent(this, PayPalService.class));
        super.onStop();
    }

你可能感兴趣的:(android端集成paypal)