我有这段代码,我正在尝试传递this.thecost
给该public void click()
方法,但它看起来不起作用,因为我在单击 paypal 按钮时收到“Paymnet Failed”。
我做错了什么?
顺便说一句,你怎么称呼private {}
, private void {}
- 所有那些看起来像 function() 的东西?
private void initUI(int theprice) {
launchPayPalButton = mPayPal.getCheckoutButton(this,
PayPal.BUTTON_278x43, CheckoutButton.TEXT_PAY);
LinearLayout.LayoutParams params = new
LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params.bottomMargin = theprice;
this.thecost = theprice;
launchPayPalButton.setLayoutParams(params);
launchPayPalButton.setOnClickListener(this);
((LinearLayout)findViewById(R.id.main_layout2)).addView(launchPayPalButton);
}
public void onClick(View v) {
payWithPaypal(this.thecost);
}
private void payWithPaypal(Integer gg) {
PayPalPayment newPayment = new PayPalPayment();
BigDecimal bigDecimal=new BigDecimal(gg);
newPayment.setSubtotal(bigDecimal);
newPayment.setCurrencyType(Currency.getInstance(Locale.US));
newPayment.setRecipient("email@hotmail.com");
newPayment.setMerchantName("My Merchant");
Intent paypalIntent = PayPal.getInstance().checkout(newPayment, this);
this.startActivityForResult(paypalIntent, 1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(resultCode) {
case Activity.RESULT_OK:
String payKey = data.getStringExtra(PayPalActivity.EXTRA_PAY_KEY);
data.getStringExtra(PayPalActivity.EXTRA_PAY_KEY);
Toast.makeText(this,"Paymnet Successful",Toast.LENGTH_LONG).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(this,"Paymnet Cancel",Toast.LENGTH_LONG).show();
break;
case PayPalActivity.RESULT_FAILURE:
Toast.makeText(this,"Paymnet Failed",Toast.LENGTH_LONG).show();
String errorID =
data.getStringExtra(PayPalActivity.EXTRA_ERROR_ID);
String errorMessage =
data.getStringExtra(PayPalActivity.EXTRA_ERROR_MESSAGE);
break;
}
编辑:我在 oncreate 方法调用 initUI()
再次编辑:我将全局变量更改为“double”,因为价格通常有小数位。现在我试着为这个值敬酒,我更清楚地看到了错误。Toast 显示一条消息,表明传递的值为“0.0”。因此,出现“付款失败”和无效付款的错误。
int eprice;
double thecost;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detail);
//Data mSource = new Data;
Intent myLocalIntent = getIntent();
Bundle myBundle = myLocalIntent.getExtras();
eprice = myBundle.getInt("eprice");
String epricetxt = myBundle.getString("eprice");
Adapter mAdapter = new Adapter(this, mSource);
//details = (Data) mAdapter.getItem(pos);
TextView theprice = (TextView) findViewById(R.id.priceTxt);
theprice.setText("Price: $" + epricetxt);
this.setCost(eprice);
this.thecost = eprice;
//Paypal
mPayPal=PayPal.initWithAppID(this,Constants.PAYPAL_APP_ID,PayPal.ENV_SANDBOX);
initUI(eprice);
}
private void initUI(int theprice) {
launchPayPalButton = mPayPal.getCheckoutButton(this,
PayPal.BUTTON_278x43, CheckoutButton.TEXT_PAY);
LinearLayout.LayoutParams params = new
LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
params.bottomMargin = theprice;
this.thecost = theprice;
launchPayPalButton.setLayoutParams(params);
launchPayPalButton.setOnClickListener(this);
((LinearLayout)findViewById(R.id.main_layout2)).addView(launchPayPalButton);
}
public void onClick(View v) {
//
payWithPaypal(getCost());
}
public void setCost(int cost) {
this.thecost = cost;
}
public double getCost() {
return this.thecost;
}
private void payWithPaypal(Double gg) {
PayPalPayment newPayment = new PayPalPayment();
Toast.makeText(getApplicationContext(),gg.toString(), Toast.LENGTH_LONG).show();
BigDecimal bigDecimal=new BigDecimal(gg);
newPayment.setSubtotal(bigDecimal);
newPayment.setCurrencyType(Currency.getInstance(Locale.US));
newPayment.setRecipient("email@hotmail.com");
newPayment.setMerchantName("My Merchant");
Intent paypalIntent = PayPal.getInstance().checkout(newPayment, this);
this.startActivityForResult(paypalIntent, 1);
}