package anywheresoftware.b4a.paypal;
import java.lang.ref.WeakReference;
import java.math.BigDecimal;
import java.util.ArrayList;
import com.paypal.android.MEP.CheckoutButton;
import com.paypal.android.MEP.PayPal;
import com.paypal.android.MEP.PayPalActivity;
import com.paypal.android.MEP.PayPalPayment;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.IOnActivityResult;
import anywheresoftware.b4a.BA.ActivityObject;
import anywheresoftware.b4a.BA.DependsOn;
import anywheresoftware.b4a.BA.Events;
import anywheresoftware.b4a.BA.Permissions;
import anywheresoftware.b4a.BA.ShortName;
import anywheresoftware.b4a.BA.Version;
import anywheresoftware.b4a.keywords.Common;
import anywheresoftware.b4a.objects.ViewWrapper;
@Version(1.0f)
@Permissions(values={"android.permission.INTERNET", "android.permission.READ_PHONE_STATE", "android.permission.ACCESS_WIFI_STATE"})
@DependsOn(values={"PayPal_MPL"})
@ShortName("PayPal")
@ActivityObject
@Events(values={"Ready (Success As Boolean)", "Result (Success As Boolean, Extra As String)", "Click"})
public class PayPalWrapper {
public static final int PAYMENT_TYPE_GOODS = 0;
public static final int PAYMENT_TYPE_SERVICE = 1;
public static final int PAYMENT_TYPE_PERSONAL = 2;
public static final int PAYMENT_TYPE_NONE = 3;
private static IOnActivityResult ion;
private String eventName;
private ArrayList<CheckoutButton> buttons = new ArrayList<CheckoutButton>();
/**
* Initializes the PayPal object. The Ready event will be raised when the service is ready.
*AppId - PayPal app id.
*Sandbox - Whether to run in sandbox mode.
*EventName - Sets the subs that will handle the events.
*/
public void Initialize(final BA ba,final String AppId,final boolean Sandbox,final String EventName) {
eventName = EventName.toLowerCase(BA.cul);
Runnable r = new Runnable() {
@Override
public void run() {
try {
if (PayPal.getInstance() == null) {
PayPal pp;
pp = PayPal.initWithAppID(ba.context, AppId, Sandbox ? PayPal.ENV_SANDBOX : PayPal.ENV_LIVE);
pp.setLanguage("en_US");
}
ba.raiseEventFromDifferentThread(PayPalWrapper.this, null, 0, eventName + "_ready", false,
new Object[] {PayPal.getInstance().isLibraryInitialized()});
} catch (Exception e) {
BA.printException(e, true);
ba.raiseEventFromDifferentThread(PayPalWrapper.this, null, 0, eventName + "_ready", false,
new Object[] {false});
}
}
};
ba.submitRunnable(r, null, 0);
}
/**
* Gets the PayPal button.
*Donate - Whether to show a Donate button or Pay button.
*/
public View GetPayPalButton(final BA ba, boolean Donate) {
final CheckoutButton v = PayPal.getInstance().getCheckoutButton(ba.activity, PayPal.BUTTON_278x43, Donate ?
CheckoutButton.TEXT_DONATE : CheckoutButton.TEXT_PAY);
v.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
ba.raiseEvent(PayPalWrapper.this, eventName + "_click");
}
});
buttons.add(v);
return v;
}
/**
* Makes a payment request.
*The Result event will be raised after the payment is processed.
*Currency - 3 characters currency code.
*Total - Payment amount.
*Recipient - Recipient email address.
*MerchantName - Merchant name.
*PaymentType - One of the PAYMENT_TYPE constants.
*/
public void RequestPayment(BA ba, String Currency, String Total, String Recipient, String MerchantName, int PaymentType) {
for (CheckoutButton cb : buttons) {
cb.updateButton();
}
PayPalPayment pay = new PayPalPayment();
pay.setSubtotal(new BigDecimal(Total));
pay.setCurrencyType(Currency);
pay.setRecipient(Recipient);
pay.setPaymentType(PaymentType);
pay.setMerchantName(MerchantName);
final BA processBA = ba.processBA;
ion = new IOnActivityResult() {
@Override
public void ResultArrived(int resultCode, Intent intent) {
String extra = "";
boolean success = false;
switch (resultCode) {
case Activity.RESULT_OK:
success = true;
extra = intent.getStringExtra(PayPalActivity.EXTRA_PAY_KEY);
break;
case Activity.RESULT_CANCELED:
extra = "canceled";
break;
case PayPalActivity.RESULT_FAILURE:
String errorID =
intent.getStringExtra(PayPalActivity.EXTRA_ERROR_ID);
String errorMessage =
intent.getStringExtra(PayPalActivity.EXTRA_ERROR_MESSAGE);
extra = errorID + ": " + errorMessage;
}
processBA.raiseEvent(PayPalWrapper.this, eventName + "_result", success, extra);
}
};
Intent ppIntent = PayPal.getInstance().checkout(pay, ba.context);
ba.startActivityForResult(ion, ppIntent);
}
}