Android集成Paypal支付Braintree

最新發現Paypal的官方SDK已經不再維護了,所以需要把項目的支付做一下升級。

文檔鏈接:點擊這裏

根據文檔來看Paypal支付的集成相比以前簡單了許多,下面我們講一下集成步驟:

1:在 build.gradle 中添加以下內容

    compile 'com.braintreepayments.api:braintree:2.+'

2:在你的 AndroidManifest.xml  中添加

<activity android:name="com.braintreepayments.api.BraintreeBrowserSwitchActivity"
    android:launchMode="singleTask">
    <intent-filter>
        <action android:name="android.intent.action.VIEW" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="android.intent.category.BROWSABLE" />
        <data android:scheme="${applicationId}.braintree" />
    </intent-filter>
</activity>

3:從服務器獲取Token

     根據後臺提供的 url 獲取到Token  後臺 集成鏈接

 

4:初始化

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_paypal);  
//初始化並添加監聽
 try {
     mBraintreeFragment = BraintreeFragment.newInstance(this, mAuthorization);


    mBraintreeFragment.addListener(new ConfigurationListener() {
        @Override
        public void onConfigurationFetched(Configuration configuration) {

        }
    });
    // 支付完成監聽
    mBraintreeFragment.addListener(new PaymentMethodNonceCreatedListener() {
        @Override
        public void onPaymentMethodNonceCreated(PaymentMethodNonce paymentMethodNonce) {
            // Send nonce to server
            String nonce = paymentMethodNonce.getNonce();
            postNonceToServer(nonce);
            if (paymentMethodNonce instanceof PayPalAccountNonce) {
                PayPalAccountNonce payPalAccountNonce = (PayPalAccountNonce) paymentMethodNonce;
                // Access additional information
                String email = payPalAccountNonce.getEmail();
                String firstName = payPalAccountNonce.getFirstName();
                String lastName = payPalAccountNonce.getLastName();
                String phone = payPalAccountNonce.getPhone();
                // See PostalAddress.java for details
                PostalAddress billingAddress = payPalAccountNonce.getBillingAddress();
                PostalAddress shippingAddress = payPalAccountNonce.getShippingAddress();
            }
        }
    });
    // 取消監聽
    mBraintreeFragment.addListener(new BraintreeCancelListener() {
        @Override
        public void onCancel(int requestCode) {
         
        }
    });
    // 錯誤監聽
    mBraintreeFragment.addListener(new BraintreeErrorListener() {
        @Override
        public void onError(Exception error) {
            if (error instanceof ErrorWithResponse) {
                ErrorWithResponse errorWithResponse = (ErrorWithResponse) error;
                BraintreeError cardErrors = errorWithResponse.errorFor("creditCard");
                if (cardErrors != null) {
                    // There is an issue with the credit card.
                    BraintreeError expirationMonthError = cardErrors.errorFor("expirationMonth");
                    if (expirationMonthError != null) {
                        // There is an issue with the expiration month.
                        LogUtils.e("paypal",expirationMonthError.getMessage());
                    }
                }
            }
        }
    });
    // mBraintreeFragment is ready to use!
} catch (InvalidArgumentException e) {
    // There was an issue with your authorization string.
}
}

5:調起支付

yourButton.setOnClickListener(new View.OnClickListener(){
    @Override public void onClick (View view){
    setupBraintreeAndStartExpressCheckout();
}
})
public void setupBraintreeAndStartExpressCheckout() {
    PayPalRequest request = new PayPalRequest(“價格”)
            .currencyCode("貨幣符號")
            .intent(PayPalRequest.INTENT_SALE);
    PayPal.requestOneTimePayment(mBraintreeFragment, request);
}

這裏注意三種Intent的意思  INTENT_SALE 表示立即到賬。

5:付款成功後訪問服務器驗證

private void postNonceToServer(String nonce) {
    final Map<String, String> dataMap = new HashMap<>();
    dataMap.put("nonce", nonce);
    dataMap.put("amount", price);
   
}

 

驗證的接口由後臺給這裏邏輯由後臺處理,這裏返回成功,說明支付並驗證成功。

 

大家測試的時候一定要用美國地址的paypal進行測試哈。

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章