Created
November 10, 2016 08:17
-
-
Save griajobag/c7f938293902bce0cb51a6bf5e142558 to your computer and use it in GitHub Desktop.
PayPal
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.putuguna.paymentpaypal; | |
import android.content.Intent; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.EditText; | |
import com.paypal.android.sdk.payments.PayPalConfiguration; | |
import com.paypal.android.sdk.payments.PayPalPayment; | |
import com.paypal.android.sdk.payments.PayPalService; | |
import com.paypal.android.sdk.payments.PaymentActivity; | |
import com.paypal.android.sdk.payments.PaymentConfirmation; | |
import org.json.JSONException; | |
import java.math.BigDecimal; | |
public class MainActivity extends AppCompatActivity implements View.OnClickListener{ | |
public static final int PAYPAL_REQUEST_CODE = 123; | |
private static PayPalConfiguration config = new PayPalConfiguration() | |
.environment(PayPalConfiguration.ENVIRONMENT_SANDBOX) | |
.clientId(PaypalConfig.PAYPAL_CLIENT_ID); | |
private Button btnPaypal; | |
private EditText etAmount; | |
private String mPaymentAmount; | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
btnPaypal = (Button) findViewById(R.id.buttonPay); | |
etAmount = (EditText) findViewById(R.id.editTextAmount); | |
btnPaypal.setOnClickListener(this); | |
Intent intent = new Intent(this, PayPalService.class); | |
intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config); | |
startService(intent); | |
} | |
@Override | |
public void onClick(View view) { | |
getPayment(); | |
} | |
private void getPayment(){ | |
//getting the amount from edittext | |
mPaymentAmount = etAmount.getText().toString(); | |
//creating a paypalpayment | |
PayPalPayment payment = new PayPalPayment(new BigDecimal(String.valueOf(mPaymentAmount)), "USD", "putuguna fee", PayPalPayment.PAYMENT_INTENT_SALE); | |
//creating paypal payment activity intent | |
Intent intents = new Intent(this, PaymentActivity.class); | |
intents.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config); | |
intents.putExtra(PaymentActivity.EXTRA_PAYMENT, payment); | |
startActivityForResult(intents, PAYPAL_REQUEST_CODE); | |
} | |
@Override | |
protected void onActivityResult(int requestCode, int resultCode, Intent data) { | |
//super.onActivityResult(requestCode, resultCode, data); | |
if(requestCode == PAYPAL_REQUEST_CODE){ | |
if(resultCode == RESULT_OK){ | |
//getting payment confirmation | |
PaymentConfirmation confirmation = data.getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION); | |
//if confimation isn't null | |
if(confirmation!=null){ | |
//getting the payment detail | |
try { | |
String paymentDetails = confirmation.toJSONObject().toString(4); | |
Log.d("TAG", "PAYMENT EXAMPLE : " + paymentDetails); | |
//starting a new activity for the payment details and also putting the payment details with intent | |
startActivity(new Intent(this, ConfirmationActivity.class) | |
.putExtra("PaymentDetails", paymentDetails) | |
.putExtra("PaymentAmount", mPaymentAmount)); | |
} catch (JSONException e) { | |
e.printStackTrace(); | |
Log.e("paymentExample", "an extremely unlikely failure occurred: ", e); | |
} | |
} | |
}else if(resultCode==RESULT_CANCELED){ | |
Log.i("paymentExample", "The user canceled."); | |
}else if(resultCode==PaymentActivity.RESULT_EXTRAS_INVALID){ | |
Log.i("paymentExample", "An invalid Payment or PayPalConfiguration was submitted. Please see the docs."); | |
} | |
} | |
} | |
@Override | |
protected void onDestroy() { | |
super.onDestroy(); | |
stopService(new Intent(this, PayPalService.class)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
good