首页 > 代码库 > Paypal支付(一)MPL真正的快捷支付
Paypal支付(一)MPL真正的快捷支付
一、前导
前面讲到了MEC支付,是在Web端集成好的,在手机端仅仅需通过WebView进行载入就可以,不须要不论什么Paypal第三方架包。以下将的是MPL支付。须要架包。
这样的支付的形式能够參考以下的演示:
https://www.paypal-biz.com/product/demo/product/mobile-payment/index.html
二、MPL支付案例
PaymentBean
package com.example.paypaldemo; public class PaymentBean { private float unitPrice; private int quantity; private float discount; public float getUnitPrice() { return unitPrice; } public void setUnitPrice(float unitPrice) { this.unitPrice = unitPrice; } public int getQuantity() { return quantity; } public void setQuantity(int quantity) { this.quantity = quantity; } public float getDiscount() { return discount; } public void setDiscount(float discount) { this.discount = discount; } public PaymentBean(float unitPrice, int quantity, float discount) { super(); this.unitPrice = unitPrice; this.quantity = quantity; this.discount = discount; } }
PizzaMain
package com.example.paypaldemo; /** * https://www.paypal-biz.com/product/demo/product/mobile-payment/index.html */ import java.math.BigDecimal; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.Gravity; import android.view.View; import android.view.View.OnClickListener; import android.widget.RelativeLayout; import android.widget.RelativeLayout.LayoutParams; import android.widget.Toast; import com.paypal.android.MEP.CheckoutButton; import com.paypal.android.MEP.PayPal; import com.paypal.android.MEP.PayPalActivity; import com.paypal.android.MEP.PayPalInvoiceData; import com.paypal.android.MEP.PayPalInvoiceItem; import com.paypal.android.MEP.PayPalPayment; public class PizzaMain extends Activity implements OnClickListener { private boolean _paypalLibraryInit; private static final String TAG = "PizzaMain"; // APP-80W284485P519543T private CheckoutButton launchPayPalButton; private int PAYPAL_BUTTON_ID = 1341515; private List<PaymentBean> list = new ArrayList<PaymentBean>();; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initLibrary(); showPayPalButton(); } public void initLibrary() { PayPal pp = PayPal.getInstance(); if (pp == null) { // Test to see if the library is already initialized // This main initialization call takes your Context, AppID, and // target server pp = PayPal.initWithAppID(this, "APP-80W284485P519543T", PayPal.ENV_NONE); // Required settings: // Set the language for the library pp.setLanguage("en_US"); // Some Optional settings: // Sets who pays any transaction fees. Possible values are: // FEEPAYER_SENDER, FEEPAYER_PRIMARYRECEIVER, FEEPAYER_EACHRECEIVER, // and FEEPAYER_SECONDARYONLY pp.setFeesPayer(PayPal.FEEPAYER_EACHRECEIVER); // true = transaction requires shipping pp.setShippingEnabled(true); _paypalLibraryInit = true; } } @Override protected void onResume() { super.onResume(); showPayPalButton(); } private void showPayPalButton() { // Generate the PayPal checkout button and save it for later use PayPal pp = PayPal.getInstance(); // Paypal按钮类型:CheckoutButton.TEXT_DONATE捐赠,CheckoutButton.TEXT_PAY付款 launchPayPalButton = pp.getCheckoutButton(this, PayPal.BUTTON_278x43, CheckoutButton.TEXT_PAY); // The OnClick listener for the checkout button launchPayPalButton.setOnClickListener(this); // 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; launchPayPalButton.setLayoutParams(params); launchPayPalButton.setId(PAYPAL_BUTTON_ID); ((RelativeLayout) findViewById(R.id.RelativeLayout01)).addView(launchPayPalButton); ((RelativeLayout) findViewById(R.id.RelativeLayout01)).setGravity(Gravity.CENTER_HORIZONTAL); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); switch (resultCode) { // The payment succeeded case Activity.RESULT_OK: Toast.makeText(this, "transact successful,has submit infomation to PayPal", Toast.LENGTH_SHORT).show(); String payKey = data.getStringExtra(PayPalActivity.EXTRA_PAY_KEY); String transactionID = data.getStringExtra(PayPalActivity.EXTRA_CORRELATION_ID);// 交易号 String paymentInfo = data.getStringExtra(PayPalActivity.EXTRA_PAYMENT_INFO);// 交易信息 String status = data.getStringExtra(PayPalActivity.EXTRA_PAYMENT_STATUS);// 交易状态 Log.e(TAG, "payKey:" + payKey + ",transactionID:" + transactionID + ",paymentInfo" + paymentInfo + ",status" + status); break; // The payment was canceled case Activity.RESULT_CANCELED: Toast.makeText(this, "cancle", Toast.LENGTH_SHORT).show(); break; // The payment failed, get the error from the EXTRA_ERROR_ID and // EXTRA_ERROR_MESSAGE case PayPalActivity.RESULT_FAILURE: Toast.makeText(this, "error", Toast.LENGTH_SHORT).show(); String errorID = data.getStringExtra(PayPalActivity.EXTRA_ERROR_ID); String errorMessage = data.getStringExtra(PayPalActivity.EXTRA_ERROR_MESSAGE); } } // 点击按钮之后的操作都是在Payment中进行的,也就是在PayActivity中进行的,交易完成后,会返回到当前Activity @Override public void onClick(View arg0) { PayPalPayment payment = new PayPalPayment(); // 设置货币种类,默认是USD payment.setCurrencyType("USD"); // 收件人信息:email或者phone number payment.setRecipient("admin@chinabuye.com"); // 设置物品描写叙述。默觉得空 payment.setDescription(""); // 显示给买家看的商户名称, 假设没有设置则显示收款方email payment.setMerchantName("ChinaBuye"); // 设置付款类型,捐款、个人、商品 // PAYMENT_TYPE_SERVICE, PAYMENT_TYPE_PERSONAL, or PAYMENT_TYPE_NONE payment.setPaymentType(PayPal.PAYMENT_TYPE_GOODS); // 交易结果还会调用web远端的一个地址传递交易数据 payment.setIpnUrl("http://www...../.../x.php"); // 设置交易备注信息,千万别写中文。否则怎么错的都不知道 payment.setMemo("Note: please give me the invoice"); // 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(); // 设置税 invoice.setTax(new BigDecimal(2f)); list.clear(); // 向购物车中加入一个产品 PayPalInvoiceItem item1 = new PayPalInvoiceItem(); item1.setName("gadget"); item1.setID("01"); // 单位价格和数量的问题 // 这里有个问题。就是价格格式必须xx#.##的形式。否则交易不成功,解决方式參考formatPrice() float unitPrice = 12.22f; int quantity = 2; float discount = 1; PaymentBean bean1 = new PaymentBean(unitPrice, quantity, discount); list.add(bean1); item1.setTotalPrice(formatTotalPrice(unitPrice, quantity, discount));// 单位价格*数量 item1.setUnitPrice(formatUnitPrice(unitPrice));// 单位价格 item1.setQuantity(2); // 数量 invoice.getInvoiceItems().add(item1); // 加入item到购物篮 // 设置付款金额,不含税和运费 payment.setSubtotal(formatSubtotal()); Intent paypalIntent = PayPal.getInstance().checkout(payment, this); this.startActivityForResult(paypalIntent, 1); // 以下这样的方法报错 // Intent paypalIntent = new Intent(this, PayPalActivity.class); // paypalIntent.putExtra(PayPalActivity.EXTRA_PAYMENT_INFO, payment); // this.startActivityForResult(paypalIntent, 1); } // 格式化付款金额 private BigDecimal formatSubtotal() { float subTotal = 0; for (int x = 0; x < list.size(); x++) { PaymentBean bean = list.get(x); subTotal += bean.getQuantity() * bean.getUnitPrice() * bean.getDiscount(); } return formatUnitPrice(subTotal); } // 格式化单个商品的总价价格 private BigDecimal formatTotalPrice(float unitPrice, int quantity, float discount) { DecimalFormat dfm = new DecimalFormat("#.##"); float price = unitPrice * discount; BigDecimal big = new BigDecimal(dfm.format(price * quantity)); return big; } // 格式化单个商品的单位价格 private BigDecimal formatUnitPrice(float unitPrice) { DecimalFormat dfm = new DecimalFormat("#.##"); BigDecimal big = new BigDecimal(dfm.format(unitPrice)); return big; } }项目结构例如以下:
Paypal支付(一)MPL真正的快捷支付
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。