Problème Paypal sandbox sur tel android

Fermé
zinebinfo Messages postés 10 Date d'inscription dimanche 19 avril 2009 Statut Membre Dernière intervention 27 juillet 2013 - 21 déc. 2011 à 14:57
Bonjour,

Je travaille dans Android sur le service de payment paypal.
Quand je lance mon programme sur l'appareil Android cela ne fonctionne pas mais il fonctionne correctement sur l'émulateur.
Merci de me proposer une solution pour que mon programme fonctionne aussi sur l'appareil.



voici mon code:

public class Main extends Activity implements OnClickListener,
OnItemSelectedListener, OnMultiChoiceClickListener, OnDismissListener{

/*final static public int SIZE_SMALL = 0;
final static public int SIZE_MEDIUM = 1;
final static public int SIZE_LARGE = 2;

final static public int METHOD_PICKUP = 0;
final static public int METHOD_DELIVERY = 1;
*/

private NumberFormat _df;

private CheckoutButton launchPayPalButton;
final static public int PAYPAL_BUTTON_ID = 10001;
private static final int REQUEST_PAYPAL_CHECKOUT = 2;

private ProgressDialog _progressDialog;
private boolean _paypalLibraryInit = false;
private boolean _progressDialogRunning = false;
private Button bt1;

private static final int request = 1;

/** Called when the activity is first created. */

// méthode pour vérifier si l'appareil est connecté au réseau
public boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
}
return false;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

if (this.isOnline()) {
Thread libraryInitializationThread = new Thread() {
public void run() {
initLibrary();
}
};

libraryInitializationThread.start();
}

setContentView(R.layout.main);
bt1 = (Button)findViewById(R.id.bouton1);
bt1.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
loadReviewPage();
}
});
}


public void loadReviewPage(){
setContentView(R.layout.review);


if (_paypalLibraryInit) {
showPayPalButton();
} else {
// Afficher un dialogue de progression à l'utilisateur et de commencer à vérifier
//pour le moment de l'initialisation est terminée
_progressDialog = new ProgressDialog(this);
_progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
_progressDialog.setMessage("Loading PayPal Payment Library");
_progressDialog.setCancelable(false);
_progressDialog.show();
_progressDialogRunning = true;
Thread newThread = new Thread(checkforPayPalInitRunnable);
newThread.start();
}
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {

if (requestCode == request) {
PayPalActivityResult(requestCode, resultCode, intent);
} else {
super.onActivityResult(requestCode, resultCode, intent);
}
}

public void paymentSucceeded(String payKey) {

}

public void paymentFailed(String errorID, String errorMessage) {
Toast.makeText(getApplicationContext(),
"We're sorry, but your payment failed."+errorMessage, Toast.LENGTH_LONG).show();

}

public void paymentCanceled() {

}

/*@Override
public void onDismiss(DialogInterface arg0) {
//findViewById(R.id.Continue).setVisibility(View.VISIBLE);
}*/





// Cela nous permet de montrer le bouton PayPal
// après la bibliothèque a été initialisé
final Runnable showPayPalButtonRunnable = new Runnable() {
public void run() {
showPayPalButton();
}
};

// Cela nous permet de lancer une boucle pour vérifier l'état de la Bibliothèque
// de PayPal d'initialisation
final Runnable checkforPayPalInitRunnable = new Runnable() {
public void run() {
checkForPayPalLibraryInit();
}
};


private void checkForPayPalLibraryInit() {
// tant que la bibliothèque n'est pas initialisé
while (_paypalLibraryInit == false) {
try {

Thread.sleep(500);
} catch (InterruptedException e) {

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Error initializing PayPal Library")
.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {

}
});
AlertDialog alert = builder.create();
alert.show();
}
}
// biblithéque initialisée
// ajoutez le «Payer with PayPal" bouton à l'écran
runOnUiThread(showPayPalButtonRunnable);
}


public void initLibrary() {
PayPal pp = PayPal.getInstance();


if (pp == null) {
// C'est l'appel d'initialisation principale qui prend dans votre contexte,
// l'ID d'application et le serveur auquel vous souhaitez vous connecter.
pp = PayPal.initWithAppID(this, "APP-80W284485P519543T",
PayPal.ENV_SANDBOX);


pp.setLanguage("fr_FR"); // Définit la langue pour la bibliothèque.
pp.setFeesPayer(PayPal.FEEPAYER_EACHRECEIVER);
pp.setShippingEnabled(true);

pp.setDynamicAmountCalculationEnabled(false);

_paypalLibraryInit = true;
}
}

private void showPayPalButton() {
removePayPalButton();

PayPal pp = PayPal.getInstance();
launchPayPalButton = pp.getCheckoutButton(this, PayPal.BUTTON_278x43,
CheckoutButton.TEXT_PAY);

launchPayPalButton.setOnClickListener(this);

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
params.bottomMargin = 10;

launchPayPalButton.setLayoutParams(params);
launchPayPalButton.setId(PAYPAL_BUTTON_ID);
((RelativeLayout) findViewById(R.id.RelativeLayout01))
.addView(launchPayPalButton);
((RelativeLayout) findViewById(R.id.RelativeLayout01))
.setGravity(Gravity.CENTER_HORIZONTAL);
if (_progressDialogRunning) {
_progressDialog.dismiss();
_progressDialogRunning = false;
}
}

private void removePayPalButton() {

if (launchPayPalButton != null) {
((RelativeLayout) findViewById(R.id.RelativeLayout01))
.removeView(launchPayPalButton);
}
}


public PayPalPayment PayPalButtonClick() {


PayPalPayment payment = new PayPalPayment();

payment.setCurrencyType("USD");

payment.setRecipient("example-merchant-1@paypal.com");

payment.setSubtotal(new BigDecimal("8.25"));

payment.setPaymentType(PayPal.PAYMENT_TYPE_GOODS);


PayPalInvoiceData invoice = new PayPalInvoiceData();

invoice.setTax(new BigDecimal("1.25"));

invoice.setShipping(new BigDecimal("4.50"));


PayPalInvoiceItem item1 = new PayPalInvoiceItem();

item1.setName("Pink Stuffed Bunny");

item1.setID("87239");

item1.setTotalPrice(new BigDecimal("6.00"));

item1.setUnitPrice(new BigDecimal("2.00"));

item1.setQuantity(3);


invoice.getInvoiceItems().add(item1);


PayPalInvoiceItem item2 = new PayPalInvoiceItem();
item2.setName("Well Wishes");
item2.setID("56691");
item2.setTotalPrice(new BigDecimal("2.25"));
item2.setUnitPrice(new BigDecimal("0.25"));
item2.setQuantity(9);
invoice.getInvoiceItems().add(item2);


payment.setInvoiceData(invoice);

payment.setMerchantName("The Gift Store");

payment.setDescription("Quite a simple payment");

payment.setCustomID("8873482296");

payment.setIpnUrl("http://www.exampleapp.com/ipn");

payment.setMemo("Hi! I'm making a memo for a simple payment.");

return payment;
// Créer un PayPalPayment base.
/*PayPalPayment payment = new PayPalPayment();

// définit le type de device pour ce paiment.
payment.setCurrencyType("USD");

// Définit le destinataire du paiement. Cela peut aussi être un numéro de téléphone.
payment.setRecipient("example-merchant-1@paypal.com");

// Définit le montant du versement, non compris l'impôt et les montants d'expédition.
double totale=100;
BigDecimal st = new BigDecimal(totale);
st = st.setScale(2, RoundingMode.HALF_UP);
payment.setSubtotal(st);
payment.setPaymentType(PayPal.PAYMENT_TYPE_GOODS);

Intent checkoutIntent = PayPal.getInstance()
.checkout(payment, this , new ResultDelegate());

PayPalInvoiceData invoice = new PayPalInvoiceData();

invoice.setTax(new BigDecimal("1.25"));

invoice.setShipping(new BigDecimal("4.50"));

// PayPalInvoiceItem has several parameters available to it. None of these parameters is required.
PayPalInvoiceItem item1 = new PayPalInvoiceItem();
// Sets the name of the item.
item1.setName("Pink Stuffed Bunny");
// Sets the ID. This is any ID that you would like to have associated with the item.
item1.setID("87239");
// Sets the total price which should be (quantity * unit price). The total prices of all PayPalInvoiceItem should add up
// to less than or equal the subtotal of the payment.
item1.setTotalPrice(new BigDecimal("6.00"));
// Sets the unit price.
item1.setUnitPrice(new BigDecimal("2.00"));
// Sets the quantity.
item1.setQuantity(3);
// Add the PayPalInvoiceItem to the PayPalInvoiceData. Alternatively, you can create an ArrayList<PayPalInvoiceItem>
// and pass it to the PayPalInvoiceData function setInvoiceItems().
invoice.getInvoiceItems().add(item1);

// Create and add another PayPalInvoiceItem to add to the PayPalInvoiceData.
PayPalInvoiceItem item2 = new PayPalInvoiceItem();
item2.setName("Well Wishes");
item2.setID("56691");
item2.setTotalPrice(new BigDecimal("2.25"));
item2.setUnitPrice(new BigDecimal("0.25"));
item2.setQuantity(9);
invoice.getInvoiceItems().add(item2);

// Sets the PayPalPayment invoice data.
payment.setInvoiceData(invoice);
// Sets the merchant name. This is the name of your Application or Company.
payment.setMerchantName("The Gift Store");
// Sets the description of the payment.
payment.setDescription("Quite a simple payment");
// Sets the Custom ID. This is any ID that you would like to have associated with the payment.
payment.setCustomID("8873482296");
// Sets the Instant Payment Notification url. This url will be hit by the PayPal server upon completion of the payment.
payment.setIpnUrl("http://www.exampleapp.com/ipn");
// Sets the memo. This memo will be part of the notification sent by PayPal to the necessary parties.
payment.setMemo("Hi! I'm making a memo for a simple payment.");


//**********

*/
// demarer la bibliotheque.
//startActivityForResult(checkoutIntent, REQUEST_PAYPAL_CHECKOUT);
}

/* Cette méthode traite les Résultats de l'activité PayPal.
* gère toutes les réponses de la Bibliothèque PayPal Paiements
*/
public void PayPalActivityResult(int requestCode, int resultCode, Intent intent) {
switch (resultCode) {
case Activity.RESULT_OK:
// Le paiement a réussi
String payKey = intent
.getStringExtra(PayPalActivity.EXTRA_PAY_KEY);
this.paymentSucceeded(payKey);
break;
case Activity.RESULT_CANCELED:
// Le paiement a été annulé
this.paymentCanceled();
break;
case PayPalActivity.RESULT_FAILURE:
// Le paiement a échoué - nous obtenons l'erreur de:
// EXTRA_ERROR_ID et EXTRA_ERROR_MESSAGE
String errorID = intent
.getStringExtra(PayPalActivity.EXTRA_ERROR_ID);
String errorMessage = intent
.getStringExtra(PayPalActivity.EXTRA_ERROR_MESSAGE);
this.paymentFailed(errorID, errorMessage);
}
}


@Override
public void onClick(View v) {
if (v == (Button) findViewById(R.id.bouton1)) {
setContentView(R.layout.main);
}else if (v == (CheckoutButton) findViewById(PAYPAL_BUTTON_ID)) {
//PayPalButtonClick(v);
//PayPalButtonClick(v);
PayPalPayment payment = PayPalButtonClick();
// Use checkout to create our Intent.
Intent checkoutIntent = PayPal.getInstance().checkout(payment, this, new ResultDelegate());
// Use the android's startActivityForResult() and pass in our Intent. This will start the library.
startActivityForResult(checkoutIntent, request);
}


}


@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub

}


@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub

}


@Override
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
// TODO Auto-generated method stub

}
@Override
public void onDismiss(DialogInterface dialog) {
// TODO Auto-generated method stub

}




}


A voir également: