Search This Blog

Thursday, June 23, 2016

Paypal - Thanh toán bằng Paypal

AndroidManifest.xml :

<uses-sdk android:minSdkVersion="INSERT_YOUR_DESIRED_minSdkVersion_HERE"    tools:overrideLibrary="com.paypal.android.sdk.payments"/>
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_TASKS" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-feature android:name="android.hardware.camera" android:required="false"/>
<uses-feature android:name="android.hardware.camera.autofocus" android:required="false"/>
<service android:name="com.paypal.android.sdk.payments.PayPalService" android:exported="false"/>

<application>

.....
<activity android:name="com.paypal.android.sdk.payments.PaymentActivity"/>
<activity android:name="com.paypal.android.sdk.payments.LoginActivity"/>
<activity android:name="com.paypal.android.sdk.payments.PaymentMethodActivity"/>
<activity android:name="com.paypal.android.sdk.payments.PaymentConfirmActivity"/>
<activity android:name="com.paypal.android.sdk.payments.PayPalFuturePaymentActivity"/>
<activity android:name="com.paypal.android.sdk.payments.FuturePaymentConsentActivity"/>
<activity android:name="com.paypal.android.sdk.payments.FuturePaymentInfoActivity"/>
<activity android:name="io.card.payment.DataEntryActivity"/>
......

</application>

Main.java : (Đặt trên onCreate )

Button button ;
private static final String CONFIG_CLIENT_ID = "AQYYknEkhEfge9adn3A8uN26guXPRz2ieFZNxBriOzEQJQxLAAWOffHCvy4mD0FBDT0PZJ4cZkci_NVh";
private static final String CONFIG_ENVIRONMENT = PayPalConfiguration.ENVIRONMENT_SANDBOX ;
private static final int REQUEST_CODE_PAYMENT = 1;
private static final int REQUEST_CODE_FUTURE_PAYMENT = 2;
private static PayPalConfiguration config = new PayPalConfiguration()
.environment(CONFIG_ENVIRONMENT)
.clientId(CONFIG_CLIENT_ID).merchantName("Hipster Store")
.merchantPrivacyPolicyUri(Uri.parse("https://www.example.com/privacy")).
                merchantUserAgreementUri(Uri.parse("https://www.example.com/legal"));
PayPalPayment thanhToan;

@Overrideprotected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_trang_dat_hang);
    Intent intent = new Intent(this, PayPalService.class);
    intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);
    startService(intent);

button.setOnClickListener(new View.OnClickListener() {
    @Override    public void onClick(View v) {
            // kích hoat thanh toán bằng paypal            thanhToan = new PayPalPayment(new BigDecimal("5"), "USD", "Kem Trị Mụn", PayPalPayment.PAYMENT_INTENT_SALE);
            Intent intent = new Intent(getApplicationContext(),PaymentActivity.class);
            intent.putExtra(PaymentActivity.EXTRA_PAYMENT, thanhToan);
            startActivityForResult(intent, REQUEST_CODE_PAYMENT);
        }
    }
});

}
// Hàm paypal    @Override    protected void onActivityResult(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) {
                        Toast.makeText(getApplicationContext(), "Thanh Toán thành công", Toast.LENGTH_LONG).show();
                }
            } else if (resultCode == Activity.RESULT_CANCELED) {
                //Toast.makeText(getApplicationContext(), "CANCELED", Toast.LENGTH_LONG).show();            } else if (resultCode == PaymentActivity.RESULT_EXTRAS_INVALID) {
                //Toast.makeText(getApplicationContext(), " INVALID ", Toast.LENGTH_LONG).show();            }
        } else if (requestCode == REQUEST_CODE_FUTURE_PAYMENT) {
            if (resultCode == Activity.RESULT_OK) {
                PayPalAuthorization auth = data.getParcelableExtra(PayPalFuturePaymentActivity.EXTRA_RESULT_AUTHORIZATION);
                if (auth != null) {
                    try {
                        Log.i("FuturePaymentExample", auth.toJSONObject()
                                .toString(4));
                        String authorization_code = auth.getAuthorizationCode();
                        Log.i("FuturePaymentExample", authorization_code);
                        sendAuthorizationToServer(auth);
                        Toast.makeText(getApplicationContext(), "Future Payment code received from PayPal", Toast.LENGTH_LONG).show();
                    } catch (JSONException e) {
                        Log.e("FuturePaymentExample", "an extremely unlikely failure occurred: ", e);
                    }
                }
            } else if (resultCode == Activity.RESULT_CANCELED) {
                Log.i("FuturePaymentExample", "The user canceled.");
            } else if (resultCode == PayPalFuturePaymentActivity.RESULT_EXTRAS_INVALID) {
                Log.i("FuturePaymentExample", "Probably the attempt to previously start the PayPalService had an invalid PayPalConfiguration. Please see the docs.");
            }
        }
    }
    private void sendAuthorizationToServer(PayPalAuthorization authorization) {
    }
    public void onFuturePaymentPurchasePressed(View pressed) {
// Get the Application Correlation ID from the SDK        String correlationId = PayPalConfiguration
                .getApplicationCorrelationId(this);
        Log.i("FuturePaymentExample", "Application Correlation ID: "                + correlationId);
// TODO: Send correlationId and transaction details to your server for// processing with// PayPal...        Toast.makeText(getApplicationContext(), "App Correlation ID received from SDK", Toast.LENGTH_LONG).show();
    }
    @Override    public void onDestroy() {
// Stop service when done        stopService(new Intent(this, PayPalService.class));
        super.onDestroy();
    }


http://www.theappguruz.com/blog/integrate-paypal-in-android

No comments:

Post a Comment