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真正的快捷支付的更多相关文章
- Android-支付宝快捷支付
支付宝的快捷支付Android版业务流程比較麻烦,出现的意外情况比較多.在此,简单说下开发流程以及出现错误的解决方式; 1.注冊支付业务.这里不在赘述.建立数据安全传输所须要的私钥公钥,这里採用的是R ...
- 「美团外卖APP签约快捷支付」流程体验
§1 添加银行卡 新用户在美团外卖APP订餐支付时,首先要绑定银行卡.如下是“添加银行卡”页,输入卡号后,系统自动调用卡bin库校验卡号的有效性,如果有效会显示发卡行和卡类型(借记卡/贷记卡). 这 ...
- java如何集成支付宝移动快捷支付功能
项目需要,需要在客户端集成支付宝接口.第一次集成,过程还是挺简单的,不过由于支付宝官方文档写的不够清晰,也是走了一些弯路,下面把过程写出来分享给大家.就研究了一下:因为使用支付宝接口,就需要到支付宝官 ...
- Android学习笔记_69_android 支付宝之网页支付和快捷支付
参考资料: https://b.alipay.com/order/productDetail.htm?productId=2013080604609654 https://b.alipay.com/o ...
- 移动APP 支付宝快捷支付开发流程
[代码] [Java]代码 ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ...
- 使用OpenSSL做RSA签名验证 支付宝移动快捷支付 的server异步通知
因为业务须要.我们须要使用支付宝移动快捷支付做收款.支付宝给了我们<移动快捷支付应用集成接入包支付接口>见支付宝包<WS_SECURE_PAY_SDK>. 支付宝给的serve ...
- phonegap支付宝2.0移动快捷支付插件IOS版
坑爹的支付宝,一两年都没有更新sdk了,这两天突然更新sdk,而且更新的变化特别大,所以只能对之前的支付宝快捷支付插件重新写了一遍. 这样既顺应了支付宝的更新,同时也支持了ios8. 废话少说,集成过 ...
- 农业银行快捷支付php版说明和实例代码
接入的是shopnc,代码改改就可以用了,虽然不是一个完善的类,也可以按照类的方法直接调用,省得再去扣开发文档 农行在接收返回信息也会验证一次,还有一点就是页面通知返回结果 一定要用服务器通知,不然会 ...
- Payssion,海外本地支付_海外本地收款_小语种本地支付_外贸收款_外贸网店收款_欧洲本地支付_俄罗斯本地支付_巴西支付_跨境支付_PAYSSION,让跨境支付更轻松!
Payssion,海外本地支付_海外本地收款_小语种本地支付_外贸收款_外贸网店收款_欧洲本地支付_俄罗斯本地支付_巴西支付_跨境支付_PAYSSION,让跨境支付更轻松! 首页 / 关于 ...
随机推荐
- 03C#数据类型
C#数据类型 值类型和引用类型区别 在C#语言中,值类型变量存储的是指定数据类型的数据,值类型变量的值(或实例)存储在栈(Stack)中,赋值语句是传递变量的值.引用类型(例如类就是引用类型)的实例, ...
- XP禁用了U盘和移动硬盘方法
会不会是你XP禁用了U盘和移动硬盘下面这些是任何禁用U盘的!你自己反着试下嘛!方法一,BIOS设置法(快刀斩乱麻法) 进入BIOS设置,选择“Integrated Peripherals”选项,展开后 ...
- UTF-8,UTF-16
UTF是 Unicode Translation Format,即把Unicode转做某种格式的意思. 在Unicode基本多文种平面定义的字符(无论是拉丁字母.汉字或其他文字或符号),一律使用2字节 ...
- CSU 2018年12月月赛 F(2218): Finding prime numbers
Description xrdog has a number set. There are 95 numbers in this set. They all have something in com ...
- Python学习-列表的其它主要操作
列表的其它主要操作 还记得之前使用del语句去清除一个列表中的所有内容,那么会因为把列表全部清空,所以输出会报错.可以使用clear() 完成 clear(self):可以将一个列表变成空列表 lis ...
- 在CentOS6.4上安装GitLab
1.Install and configure the necessary dependencies On CentOS 6 (and RedHat/Oracle/Scientific Linux 6 ...
- Buffer.isBuffer()详解
Buffer.isBuffer(obj) obj {Object} 返回:{Boolean} 如果 obj 是一个 Buffer 则返回 true.
- python书籍推荐:Python数据科学手册
所属网站分类: 资源下载 > python电子书 作者:today 链接:http://www.pythonheidong.com/blog/article/448/ 来源:python黑洞网 ...
- BNUOJ 1541 Air Raid
Air Raid Time Limit: 1000ms Memory Limit: 10000KB This problem will be judged on PKU. Original ID: 1 ...
- 数据库 SQL SQL转义
SQL转义 @author ixenos 前言 类似文件分隔符在不同系统的实现不同,我们需要一个中间的转义字符来作为接口,各厂商再具体实现 而SQL的转义语法主要为了支持各种数据库普遍支持的特性,但各 ...