Android:Paypal实现

时间:2013-08-07 08:40:44

标签: android paypal payment-gateway paypal-sandbox

我正在开发一个应用程序,该应用程序的需求是需要在线支付。

我想以印度货币(卢比)向我的申请人支付账单金额。 我尝试了使用MPL的演示示例。

我创建了一个类:

public class MainActivity extends Activity implements OnClickListener {

private boolean _paypalLibraryInit = false;

final static public int PAYPAL_BUTTON_ID = 10001;

CheckoutButton launchPayPalButton;

public static String resultTitle;
public static String resultInfo;
public static String resultExtra;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initLibrary();
    if (_paypalLibraryInit) {
        showPayPalButton();
    } else {
        finish();
    }
}

/** init method **/
public void initLibrary() {
    PayPal pp = PayPal.getInstance();
    if (pp == null) {
        // This is the main initialization call that takes in your Context,
        // the Application ID, and the server you would like to connect to.
        pp = PayPal.initWithAppID(this, "APP-80W284485P519543T",PayPal.ENV_SANDBOX);

        // -- These are required settings.
        pp.setLanguage("en_US"); // Sets the language for the library.
        // --

        // -- These are a few of the optional settings.
        // Sets the fees payer. If there are fees for the transaction, this
        // person will pay for them. Possible values are FEEPAYER_SENDER,
        // FEEPAYER_PRIMARYRECEIVER, FEEPAYER_EACHRECEIVER, and
        // FEEPAYER_SECONDARYONLY.
        pp.setFeesPayer(PayPal.FEEPAYER_EACHRECEIVER);
        // Set to true if the transaction will require shipping.
        pp.setShippingEnabled(true);
        // Dynamic Amount Calculation allows you to set tax and shipping
        // amounts based on the user's shipping address. Shipping must be
        // enabled for Dynamic Amount Calculation. This also requires you to
        // create a class that implements PaymentAdjuster and Serializable.
        pp.setDynamicAmountCalculationEnabled(false);
        // --
        _paypalLibraryInit = true;
    }
}

private void showPayPalButton() {
    // Generate the PayPal checkout button and save it for later use
    PayPal pp = PayPal.getInstance();
    launchPayPalButton = pp.getCheckoutButton(this, PayPal.BUTTON_278x43,CheckoutButton.TEXT_PAY);

    // Add the listener to the layout
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    params.bottomMargin = 10;
    params.leftMargin= 50;

    launchPayPalButton.setLayoutParams(params);
    launchPayPalButton.setId(PAYPAL_BUTTON_ID);
    // The OnClick listener for the checkout button
    launchPayPalButton.setOnClickListener(this);

    //((RelativeLayout) findViewById(R.id.RelativeLayout01)).addView(launchPayPalButton);       
    ((RelativeLayout) findViewById(R.id.rl)).addView(launchPayPalButton);
}

@Override
public void onClick(View v) {
    // Use our helper function to create the simple payment.
    PayPalButtonClick(v);
    // 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);
}

public void PayPalButtonClick(View arg0) {
    // Create a basic PayPal payment
    PayPalPayment payment = new PayPalPayment();

    // Set the currency type
    payment.setCurrencyType("USD");

    // Set the recipient for the payment (can be a phone number)
    payment.setRecipient("abc@gmail.com");

    // Set the payment amount, excluding tax and shipping costs
    payment.setSubtotal(new BigDecimal("1.0"));

    // Set the payment type--his can be PAYMENT_TYPE_GOODS,
    // PAYMENT_TYPE_SERVICE, PAYMENT_TYPE_PERSONAL, or PAYMENT_TYPE_NONE
    payment.setPaymentType(PayPal.PAYMENT_TYPE_GOODS);

    // PayPalInvoiceData can contain tax and shipping amounts, and an
    // ArrayList of PayPalInvoiceItem that you can fill out.
    // These are not required for any transaction.
    PayPalInvoiceData invoice = new PayPalInvoiceData();

    // Set the tax amount
    invoice.setTax(new BigDecimal("0"));
    Intent checkoutIntent = PayPal.getInstance().checkout(payment, this /*, new ResultDelegate()*/);
    this.startActivityForResult(checkoutIntent, 1);
}

public void PayPalActivityResult(int requestCode, int resultCode, Intent intent) {
    if(requestCode != 1)
        return;

    Toast.makeText(getApplicationContext(),resultTitle , Toast.LENGTH_SHORT).show();
}
}

结果委托类为:

public class ResultDelegate implements PayPalResultDelegate, Serializable {

private static final long serialVersionUID = 10001L;

/**
 * Notification that the payment has been completed successfully.
 * 
 * @param payKey            the pay key for the payment
 * @param paymentStatus     the status of the transaction
 */
public void onPaymentSucceeded(String payKey, String paymentStatus) {
    MainActivity.resultTitle = "SUCCESS";
    MainActivity.resultInfo = "You have successfully completed your transaction.";
    MainActivity.resultExtra = "Key: " + payKey;
}

/**
 * Notification that the payment has failed.
 * 
 * @param paymentStatus     the status of the transaction
 * @param correlationID     the correlationID for the transaction failure
 * @param payKey            the pay key for the payment
 * @param errorID           the ID of the error that occurred
 * @param errorMessage      the error message for the error that occurred
 */
public void onPaymentFailed(String paymentStatus, String correlationID,
        String payKey, String errorID, String errorMessage) {
    MainActivity.resultTitle = "FAILURE";
    MainActivity.resultInfo = errorMessage;
    MainActivity.resultExtra = "Error ID: " + errorID + "\nCorrelation ID: "
            + correlationID + "\nPay Key: " + payKey;
}

/**
 * Notification that the payment was canceled.
 * 
 * @param paymentStatus     the status of the transaction
 */
public void onPaymentCanceled(String paymentStatus) {
    MainActivity.resultTitle = "CANCELED";
    MainActivity.resultInfo = "The transaction has been cancelled.";
    MainActivity.resultExtra = "";
}
} 

还下载了Paypal_MPL.jar文件并在应用程序中导入并创建了一个paypal个人帐户。

1。现在的问题是如何测试应用程序?

2。我是在正确的轨道上吗?

第3。我还应该做些什么?

我不清楚MPL的实施步骤和如何在Paypal上创建帐户,测试帐户,将帐户与应用程序相关联等

请建议我并指导我。

1 个答案:

答案 0 :(得分:0)

http://www.androiddevelopersolution.com/2012/09/paypal-integration-in-android.html我希望这会对你有所帮助。要测试应用程序,您可以查看结果活动