I am trying to integrate the PayPal payflow to my Android app, using the latest implementation issued by PayPal late last year. But unfortunately I can't get it to work. Any idea on what I am doing wrong? Any help you could give me would be greatly appreciated.
This is my code:
public class PM_Fragment extends Fragment {
private static final String CONFIG_ENVIRONMENT = //Enter the correct environment here;
private static final String CONFIG_CLIENT_ID = //you need to register with PayPal and enter your client_ID here;
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)
.acceptCreditCards(true)
.languageOrLocale("EN")
.rememberUser(true)
.merchantName("Company name");
EditText Name, Age;
Spinner amount;
Button Donate;
@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
/**
* Inflate the layout for this fragment
*/
View rootView= inflater.inflate(
R.layout.pm_fragment, container, false);
Name = (EditText) rootView.findViewById(R.id.editText1);
Age = (EditText) rootView.findViewById(R.id.editText2);
amount = (Spinner) rootView.findViewById(R.id.spinner1);
Donate = (Button) rootView.findViewById(R.id.button1);
Donate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
onBuyPressed(v);
}
});
initPaymentService();
return rootView;
}
public void initPaymentService() {
try {
Intent intent = new Intent(getActivity(), PayPalService.class);
intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);
getActivity().startService(intent);
} catch (Exception e) {
Log.i("PayPal Exception", e.getMessage());
}
}
public double onChoiceMade() {
int pos = amount.getSelectedItemPosition();
double payment;
if (pos == 0) {
payment = 5.00;
} else if (pos == 1) {
payment = 10.00;
} else if (pos == 2) {
payment = 20.00;
} else {
payment = 50.00;
}
return payment;
}
public void onBuyPressed(View pressed) {
try{
int age = Integer.parseInt(Age.getText().toString());
if (age >= 18 && age < 99) {
PayPalPayment thingToBuy = new PayPalPayment(new BigDecimal(onChoiceMade()), "EUR", "Rockestra Donation", PayPalPayment.PAYMENT_INTENT_SALE);
Intent intent = new Intent(getActivity(), PaymentActivity.class);
intent.putExtra(PaymentActivity.EXTRA_PAYMENT, thingToBuy);
startActivityForResult(intent, REQUEST_CODE_PAYMENT);
} else {
Toast.makeText(getActivity(), "You must be 18 years of age to Process a payment. Please enter a correct age.", Toast.LENGTH_LONG).show();
Age.setText("");
}
} catch (NumberFormatException e){
Toast.makeText(getActivity(), "Age value cannot be empty. \n Please enter a valid age.", Toast.LENGTH_LONG).show();
}
}
@Override
public 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) {
try {
Log.i("paymentExample", confirm.toJSONObject().toString(4));
Toast.makeText(getActivity().getApplicationContext(), "PaymentConfirmation info received from PayPal",
Toast.LENGTH_LONG).show();
} catch (JSONException e) {
Log.e("paymentExample", "an extremely unlikely failure occurred: ", e);
}
}
} else if (resultCode == Activity.RESULT_CANCELED) {
Log.i("paymentExample", "The user canceled.");
} else if (resultCode == PaymentActivity.RESULT_EXTRAS_INVALID) {
Log.i("paymentExample", "An invalid Payment was submitted. Please see the docs.");
}
} 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(getActivity().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) {
}
@Override
public void onDestroy() {
// Stop service when done
getActivity().stopService(new Intent(getActivity(), PayPalService.class));
super.onDestroy();
}
}
I have the following code implemented in a fragment. Please take a look at the below. Note that you need to make some changes, so I take a look at the PayPal documentation too.
Class code:
public class DonationFragment extends Fragment {
private static final String CONFIG_ENVIRONMENT = //Enter the correct environment here;
private static final String CONFIG_CLIENT_ID = //you need to register with PayPal and enter your client_ID here;
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)
.acceptCreditCards(true)
.languageOrLocale("EN")
.rememberUser(true)
.merchantName("Company name");
EditText Name, Age;
Spinner amount;
Button Donate;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_donations, container, false);
Name = (EditText) rootView.findViewById(R.id.editText1);
Age = (EditText) rootView.findViewById(R.id.editText2);
amount = (Spinner) rootView.findViewById(R.id.spinner1);
Donate = (Button) rootView.findViewById(R.id.button1);
Donate.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
onBuyPressed(v);
}
});
initPaymentService();
return rootView;
}
public void initPaymentService() {
try {
Intent intent = new Intent(getActivity(), PayPalService.class);
intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);
getActivity().startService(intent);
} catch (Exception e) {
Log.i("PayPal Exception", e.getMessage());
}
}
public double onChoiceMade() {
int pos = amount.getSelectedItemPosition();
double payment;
if (pos == 0) {
payment = 5.00;
}else if (pos == 1) {
payment = 10.00;
}else if (pos == 2) {
payment = 20.00;
}else {
payment = 50.00;
}
return payment;
}
public void onBuyPressed(View pressed) {
try{
int age = Integer.parseInt(Age.getText().toString());
if (age >= 18 && age < 99) {
PayPalPayment thingToBuy = new PayPalPayment(new BigDecimal(onChoiceMade()), "EUR", "Rockestra Donation", PayPalPayment.PAYMENT_INTENT_SALE);
Intent intent = new Intent(getActivity(), PaymentActivity.class);
intent.putExtra(PaymentActivity.EXTRA_PAYMENT, thingToBuy);
startActivityForResult(intent, REQUEST_CODE_PAYMENT);
}
else {
Toast.makeText(getActivity(), "You must be 18 years of age to Process a payment. Please enter a correct age.", Toast.LENGTH_LONG).show();
Age.setText("");
}
}catch (NumberFormatException e){
Toast.makeText(getActivity(), "Age value cannot be empty. \n Please enter a valid age.", Toast.LENGTH_LONG).show();
}
}
@Override
public 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) {
try {
Log.i("paymentExample", confirm.toJSONObject().toString(4));
Toast.makeText(getActivity().getApplicationContext(), "PaymentConfirmation info received from PayPal",
Toast.LENGTH_LONG).show();
} catch (JSONException e) {
Log.e("paymentExample", "an extremely unlikely failure occurred: ", e);
}
}
} else if (resultCode == Activity.RESULT_CANCELED) {
Log.i("paymentExample", "The user canceled.");
} else if (resultCode == PaymentActivity.RESULT_EXTRAS_INVALID) {
Log.i("paymentExample", "An invalid Payment was submitted. Please see the docs.");
}
} 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(getActivity().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) {
}
@Override
public void onDestroy() {
// Stop service when done
getActivity().stopService(new Intent(getActivity(), PayPalService.class));
super.onDestroy();
}
}
Please note that I have implemented a spinner to select the amount to donate in this program. Also, I have included a check which makes sure that the age of the user is more than 18 and less than 100.
XML layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg_black"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="Name (optional):"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editText1"
android:layout_width="262dp"
android:layout_height="wrap_content"
android:ems="10"
android:gravity="center_vertical"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="Age:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/editText2"
android:layout_width="79dp"
android:layout_height="wrap_content"
android:ems="10"
android:gravity="center_vertical"
android:inputType="number" />
</LinearLayout>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:text="Amount to donate:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="@+id/spinner1"
android:layout_width="276dp"
android:layout_height="wrap_content"
android:entries="@array/donations"
android:gravity="center_vertical" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="141dp"
android:gravity="center"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="180dp"
android:layout_height="49dp"
android:background="@drawable/checkout"
android:text="Donate"
android:textColor="#000000" />
</LinearLayout>
</LinearLayout>
Hope this helps :)