将我的应用与paypal集成时,我遇到了问题。 我已经成功启动了PayPal服务但是当我尝试发送我的PayPal付款时,它总是会抛出错误,如下所示:
解组时找不到类:com.paypal.android.sdk.payments.PayPalPayment
我的代码如下:
package com.example.zukami.apps.blynk;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.http.client.ClientProtocolException;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.example.zukami.apps.blynk.bean.AppConfig;
import com.example.zukami.apps.blynk.bean.Coupon;
import com.example.zukami.apps.blynk.bean.Order;
import com.example.zukami.apps.blynk.bean.Paypal;
import com.example.zukami.apps.blynk.bean.User;
import com.example.zukami.apps.blynk.util.BlynkProgressDialog;
import com.example.zukami.apps.blynk.util.CommandResponse;
import com.example.zukami.apps.blynk.util.Flag.DiscountType;
import com.example.zukami.apps.blynk.util.Flag.OrderStatus;
import com.example.zukami.apps.blynk.util.Flag.PaymentOptions;
import com.example.zukami.apps.blynk.util.ResourceManager;
import com.example.zukami.apps.blynk.util.Util;
import com.example.zukami.apps.blynk.util.WebserviceAssessor;
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;
public class PaymentOptionFragment extends Fragment {
private static String TAG = PaymentOptionFragment.class.getCanonicalName();
private static final String CONFIG_ENVIRONMENT = PayPalConfiguration.ENVIRONMENT_NO_NETWORK;
private static final int REQUEST_CODE_PAYMENT = 1;
private static final int REQUEST_CODE_FUTURE_PAYMENT = 2;
private View mainView;
private FragmentActivity activity;
private Button ipayButton;
private Button transferButton;
private Button ccPaypalButton;
private Button cashButton;
private String compId;
private String paymentMethod;
private Order order;
private User user;
private OrderStatus statOrder;
private float totalPurchaseOrder;
private String bankTransferInstructions;
private String cashOnPickUpInstructions;
private Paypal paypal;
private PayPalConfiguration ppConfig;
private AppConfig config;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activity = getActivity();
config = Util.getConfigFromPreference(activity);
compId = config.companyId;
new ShowHidePaymentButton().execute(compId);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mainView = inflater.inflate(R.layout.payment_option_fragment,
container, false);
user = Util.getUserFromPreference(activity);
order = Util.getOrderFromPreference(activity);
ccPaypalButton = (Button) mainView.findViewById(R.id.cc_button);
ccPaypalButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Log.e(TAG, "ALOHA PAYPAL");
order = Util.getOrderFromPreference(activity);
paymentMethod = PaymentOptions.PAYPAL.getOption();
order.paymentMethod = paymentMethod;
statOrder = OrderStatus.getOrderStatus(order.status);
if (statOrder == OrderStatus.NONE) {
order.total = totalPurchaseOrder;
new AddingOrderToServer().execute(order);
} else if (statOrder == OrderStatus.PENDING) {
Util.saveOrderToPreference(activity, order);
Log.e(TAG, "CONNECT WITH PAYPAL");
connectWithPaypal();
}
}
});
return mainView;
}
private void connectWithPaypal() {
String totalOrder = Float.toString(order.total);
String currency = activity.getResources().getString(R.string.currency);
String orderNumber = "Order# " + order.invoiceNo;
PayPalPayment thingToBuy = new PayPalPayment(
new BigDecimal(totalOrder), currency, orderNumber,
PayPalPayment.PAYMENT_INTENT_SALE);
Intent buyingIntent = new Intent(activity, PaymentActivity.class);
buyingIntent.putExtra(PaymentActivity.EXTRA_PAYMENT, thingToBuy);
startActivityForResult(buyingIntent, REQUEST_CODE_PAYMENT);
}
private class ShowHidePaymentButton extends
AsyncTask<String, Void, CommandResponse> {
private CommandResponse response;
private BlynkProgressDialog progressDialog;
private String companyId;
@Override
protected void onPreExecute() {
progressDialog = BlynkProgressDialog.show(activity, "title",
"message");
}
@Override
protected CommandResponse doInBackground(String... params) {
companyId = params[0];
try {
response = WebserviceAssessor.getData(
ResourceManager.getPaymentOptions(companyId), null);
} catch (ClientProtocolException e) {
Log.e(TAG, e.getMessage());
} catch (IOException e) {
Log.e(TAG, e.getMessage());
}
return response;
}
@Override
protected void onPostExecute(CommandResponse response) {
if (response != null) {
if (response.success) {
String json;
try {
json = response.message;
JSONObject jObject = new JSONObject(json);
JSONObject data = jObject.getJSONObject("Data");
Iterator iterator = data.keys();
while (iterator.hasNext()) {
String key = (String) iterator.next();
Log.e(TAG, "KEY= " + key);
if (!data.isNull(key)) {
JSONObject opt = data.getJSONObject(key);
Log.e(TAG, "OPT = " + opt.toString());
if (opt.toString().length() != 0) {
PaymentOptions option = PaymentOptions
.getPaymentOptions(key);
switch (option) {
case BANK_TRANSFER:
bankTransferInstructions = opt
.getString("Instruction");
transferButton
.setVisibility(View.VISIBLE);
break;
case CASH:
cashOnPickUpInstructions = opt
.getString("Instruction");
cashButton.setVisibility(View.VISIBLE);
break;
case IPAY:
ipayButton.setVisibility(View.VISIBLE);
break;
case PAYPAL:
ccPaypalButton
.setVisibility(View.VISIBLE);
paypal = new Paypal();
paypal.id = opt.getString("Id");
paypal.clientId = opt
.getString("ClientId");
paypal.receiverEmail = opt
.getString("ReceiverEmail");
paypal.createdOn = opt
.getString("CreatedOn");
paypal.updatedOn = opt
.getString("updateddOn");
startPaypalService(paypal);
break;
}
} else {
continue;
}
}
}
} catch (JSONException e) {
Log.e(TAG, e.getMessage());
}
}
}
progressDialog.dismiss();
}
}
private void startPaypalService(Paypal paypal) {
if (paypal != null) {
ppConfig = new PayPalConfiguration()
.environment(CONFIG_ENVIRONMENT)
.clientId(paypal.clientId)
.merchantName(config.companyName)
.merchantPrivacyPolicyUri(
Uri.parse("https://www.example.com/privacy"))
.merchantUserAgreementUri(
Uri.parse("https://www.example.com/legal"));
Intent intent = new Intent(activity, PayPalService.class);
intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, ppConfig);
activity.startService(intent);
}
}
@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.e(TAG, "ALOHAA pAYPAL == "
+ confirm.toJSONObject().toString(4));
// Log.i("paymentExample",
// confirm.toJSONObject().toString(4));
Toast.makeText(
activity,
"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.e("paymentExample", "The user canceled.");
} else if (resultCode == PaymentActivity.RESULT_EXTRAS_INVALID) {
Log.e("paymentExample",
"An invalid Payment was submitted. Please see the docs.");
}
}
}
private class AddingOrderToServer extends
AsyncTask<Order, Void, CommandResponse> {
private CommandResponse response;
private BlynkProgressDialog progressDialog;
private Order orderToSend;
@Override
protected void onPreExecute() {
progressDialog = BlynkProgressDialog.show(activity, "title",
"message");
}
@Override
protected CommandResponse doInBackground(Order... params) {
orderToSend = params[0];
orderToSend.status = OrderStatus.PENDING.getStatus();
Map<String, Object> orderData = orderDataToPost(orderToSend);
try {
response = WebserviceAssessor.postData(
ResourceManager.addOrder(), orderData);
} catch (IOException e) {
Log.e(TAG, e.getMessage());
}
return response;
}
@Override
protected void onPostExecute(CommandResponse response) {
if (response != null) {
if (response.success) {
Log.e(TAG, response.message);
try {
String json = response.message;
JSONObject jObject = new JSONObject(json);
JSONObject data = jObject.getJSONObject("Data");
orderToSend.orderId = data.getString("Id");
orderToSend.invoiceNo = data.getString("InvoiceNo");
orderToSend.invoicePrefix = data
.getString("InvoicePrefix");
Util.saveOrderToPreference(activity, orderToSend);
PaymentOptions payOpt = PaymentOptions
.getPaymentOptions(orderToSend.paymentMethod);
switch (payOpt) {
case BANK_TRANSFER:
goToInstructionPage(bankTransferInstructions);
break;
case CASH:
goToInstructionPage(cashOnPickUpInstructions);
break;
case PAYPAL:
connectWithPaypal();
break;
case IPAY:
break;
}
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
}
}
progressDialog.dismiss();
}
}
public void OnDestroy() {
activity.stopService(new Intent(activity, PayPalService.class));
super.onDestroy();
}
}
我试图用bundle对象包装并作为额外的parcelable并设置PaypalPayment的类加载器,但结果仍然相同。 请告知我应该怎样做才能修复上述代码,并且非常感谢您提供任何帮助。