我正在尝试使用PayPal付款方式来完成我的应用程序,将其用作大学的项目,我无法找到解决方案,我很困惑。 我添加到build.gradle
//Paypal
implementation 'com.paypal.sdk:paypal-android-sdk:2.15.3'
此处还有logcat:https://ibb.co/hkJeZw
这是我在购物车中的代码:
package com.example.onix.AndroEat;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.example.onix.AndroEat.Common.Common;
import com.example.onix.AndroEat.Database.Database;
import com.example.onix.AndroEat.Model.MyResponse;
import com.example.onix.AndroEat.Model.Notification;
import com.example.onix.AndroEat.Model.Order;
import com.example.onix.AndroEat.Model.Request;
import com.example.onix.AndroEat.Model.Sender;
import com.example.onix.AndroEat.Model.Token;
import com.example.onix.AndroEat.Remote.APIService;
import com.example.onix.AndroEat.ViewHolder.CartAdapter;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;
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 com.rengwuxian.materialedittext.MaterialEditText;
import org.json.JSONException;
import org.json.JSONObject;
import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import com.example.onix.AndroEat.Common.Config;
import info.hoang8f.widget.FButton;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import uk.co.chrisjenx.calligraphy.CalligraphyConfig;
import uk.co.chrisjenx.calligraphy.CalligraphyContextWrapper;
public class Cart extends AppCompatActivity {
private static final int PAYPAL_REQUEST_CODE = 9999;
RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;
FirebaseDatabase database;
DatabaseReference requests;
public TextView txtTotalPrice;
FButton btnPlace;
List<Order> cart = new ArrayList<>();
CartAdapter adapter;
APIService mService;
//Paypal payment
static PayPalConfiguration config = new PayPalConfiguration()
.environment(PayPalConfiguration.ENVIRONMENT_SANDBOX) // use Sandbox because we test
.clientId(Config.PAYPAL_CLIENT_ID);
String address,comment; //
//Press Ctrl+O
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(CalligraphyContextWrapper.wrap(newBase));
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Note : add this code before setContentView method
CalligraphyConfig.initDefault(new CalligraphyConfig.Builder()
.setDefaultFontPath("fonts/restaurant_font.otf")
.setFontAttrId(R.attr.fontPath)
.build());
setContentView(R.layout.activity_cart);
//Init paypal
Intent intent = new Intent(this, PayPalService.class);
intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION,config);
startService(intent);
//Init Service
mService = Common.getFCMService();
//Firebase
database = FirebaseDatabase.getInstance();
requests = database.getReference("Requests");
//Init
recyclerView = (RecyclerView) findViewById(R.id.listCart);
recyclerView.setHasFixedSize(true);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
txtTotalPrice = (TextView) findViewById(R.id.total);
btnPlace = (FButton) findViewById(R.id.btnPlaceOrder);
btnPlace.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (cart.size() > 0)
showAlertDialog();
else
Toast.makeText(Cart.this, "Your cart is empty", Toast.LENGTH_SHORT).show();
}
});
loadListFood();
}
private void showAlertDialog() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(Cart.this);
alertDialog.setTitle("One more step!");
alertDialog.setMessage("Enter your address:");
LayoutInflater inflater = this.getLayoutInflater();
View order_address_comment = inflater.inflate(R.layout.order_address_comment, null);
final MaterialEditText edtAddress = (MaterialEditText) order_address_comment.findViewById(R.id.edtAddress);
final MaterialEditText edtComment = (MaterialEditText) order_address_comment.findViewById(R.id.edtComment);
alertDialog.setView(order_address_comment);
alertDialog.setIcon(R.drawable.ic_shopping_cart_black_24dp);
alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//Show Paypal to payment
//first , get Address and Comment from Alert Dialog
address = edtAddress.getText().toString();
comment = edtComment.getText().toString();
String formatAmount = txtTotalPrice.getText().toString()
.replace("£","")
.replace(",","");
PayPalPayment payPalPayment = new PayPalPayment(new BigDecimal(formatAmount),
"GBP",
"AndroEat app Order",
PayPalPayment.PAYMENT_INTENT_SALE);
Intent intent = new Intent(getApplicationContext(), PaymentActivity.class);
intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION,config);
intent.putExtra(PaymentActivity.EXTRA_PAYMENT,payPalPayment);
startActivityForResult(intent,PAYPAL_REQUEST_CODE);
}
});
alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
alertDialog.show();
}
//Press Ctrl+O
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == PAYPAL_REQUEST_CODE)
{
if(resultCode == RESULT_OK)
{
PaymentConfirmation confirmation = data.getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION);
if(confirmation != null)
{
try{
String paymentDetail = confirmation.toJSONObject().toString(4);
JSONObject jsonObject = new JSONObject(paymentDetail);
//Create new Request
Request request = new Request(
Common.currentUser.getPhone(),
Common.currentUser.getName(),
address,
txtTotalPrice.getText().toString(),
"0", // status
comment,
jsonObject.getJSONObject("response").getString("state"), // State from JSON
cart
);
//Submit to Firebase
//We will using System.CurrentMilli to key
String order_number = String.valueOf(System.currentTimeMillis());
requests.child(order_number)
.setValue(request);
//Delete cart
new Database(getBaseContext()).cleanCart();
sendNotificationOrder(order_number);
Toast.makeText(Cart.this, "Order Placed, Thank you.", Toast.LENGTH_SHORT).show();
finish();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
else if(resultCode == Activity.RESULT_CANCELED)
Toast.makeText(this, "Payment cancel", Toast.LENGTH_SHORT).show();
else if(resultCode == PaymentActivity.RESULT_EXTRAS_INVALID)
Toast.makeText(this, "Invalid payment", Toast.LENGTH_SHORT).show();
}
}
private void sendNotificationOrder(final String order_number) {
DatabaseReference tokens = FirebaseDatabase.getInstance().getReference("Tokens");
Query data = tokens.orderByChild("isServerToken").equalTo(true); // get all node with isServertoken is true
data.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Token serverToken = null;
for (DataSnapshot postSnapShot : dataSnapshot.getChildren()) {
serverToken = postSnapShot.getValue(Token.class);
}
//Create raw payload to send
Notification notification = new Notification("EDMT Dev", "You have new order " + order_number);
Sender content = new Sender(serverToken.getToken(), notification);
mService.sendNotification(content)
.enqueue(new Callback<MyResponse>() {
@Override
public void onResponse(Call<MyResponse> call, Response<MyResponse> response) {
//Only run when get result
if(response.code() == 200) {
if (response.body().success == 1) {
Toast.makeText(Cart.this, "Thank you , Order Place", Toast.LENGTH_SHORT).show();
finish();
} else {
Toast.makeText(Cart.this, "Failed !!!", Toast.LENGTH_SHORT).show();
}
}
}
@Override
public void onFailure(Call<MyResponse> call, Throwable t) {
Log.e("ERROR", t.getMessage());
}
});
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
private void loadListFood() {
cart = new Database(this).getCarts();
adapter = new CartAdapter(cart, this);
adapter.notifyDataSetChanged();
recyclerView.setAdapter(adapter);
//Calculate total price
int total = 0;
for (Order order : cart)
total += (Integer.parseInt(order.getPrice()))*(Integer.parseInt(order.getQuantity()));
Locale locale = new Locale("en", "UK");
NumberFormat fmt = NumberFormat.getCurrencyInstance(locale);
txtTotalPrice.setText(fmt.format(total));
}
@Override
public boolean onContextItemSelected(MenuItem item) {
if (item.getTitle().equals(Common.DELETE))
deleteCart(item.getOrder());
return true;
}
private void deleteCart(int position) {
//We will remove item at List<Order> by position
cart.remove(position);
//After that , we will delete all old data from SQLite
new Database(this).cleanCart();
//And final , we will update new data from List<Order> to SQLite
for (Order item : cart)
new Database(this).addToCart(item);
//Refresh
loadListFood();
}
}
我的请求代码:
package com.example.onix.AndroEat.Model;
import java.util.List;
public class Request {
//Add new field state payment
private String phone;
private String name;
private String address;
private String total;
private String status;
private String comment;
private String paymentState;
private List<Order> foods; // list of food order
public Request() {
}
public Request(String phone, String name, String address, String total, String status, String comment, String paymentState, List<Order> foods) {
this.phone = phone;
this.name = name;
this.address = address;
this.total = total;
this.status = status;
this.comment = comment;
this.paymentState = paymentState;
this.foods = foods;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getTotal() {
return total;
}
public void setTotal(String total) {
this.total = total;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
public String getPaymentState() {
return paymentState;
}
public void setPaymentState(String paymentState) {
this.paymentState = paymentState;
}
public List<Order> getFoods() {
return foods;
}
public void setFoods(List<Order> foods) {
this.foods = foods;
}
}
我还有一个java类名称&#39; Config1里面是public static final String PAYPAL_CLIENT_ID =
,如果从Sandbox在paypal上添加app,我会得到它。
答案 0 :(得分:0)
将您的ABCGHI
字符串值解析为int并将其传递到formatAmount
,在下面的代码中:
BigDecimal
答案 1 :(得分:0)
将区域设置“country”从“UK”更改为“GB”并正常工作。 谢谢大家的帮助