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,让跨境支付更轻松! 首页 / 关于 ...
随机推荐
- No-7.运算符
数学符号表链接:https://zh.wikipedia.org/wiki/数学符号表 01. 算数运算符 是完成基本的算术运算使用的符号,用来处理四则运算 运算符 描述 实例 + 加 10 + 20 ...
- react入门(下)
react生命周期 1. 组件的三个生命周期状态: * Mount:插入真实 DOM * Update:被重新渲染 * Unmount:被移出真实 DOM2. React 为每个状态都提供了两种勾子( ...
- 对拍 bat命令快速模板
对拍.bat @echo off :loop maker.exe > in.in wq.exe < in.in > out.out std.exe < in.in >st ...
- [LUOGU] P1466 集合 Subset Sums
题目描述 对于从1到N (1 <= N <= 39) 的连续整数集合,能划分成两个子集合,且保证每个集合的数字和是相等的.举个例子,如果N=3,对于{1,2,3}能划分成两个子集合,每个子 ...
- [JOYOI] 1052 没有上司的舞会
/ Joy OI / 题目列表 / 没有上司的舞会 题目限制 时间限制 内存限制 评测方式 题目来源 1000ms 131072KiB 标准比较器 Local 题目描述 Ural大学有N个职员,编号为 ...
- 将json格式转为url参数格式的方法(xjl456852整理修改)
测试页面: <html> <head> <script type="text/javascript" src="jquery-1.11.3. ...
- 使用idea搭建ssh项目
参考: https://www.cnblogs.com/getchen/p/8036709.html 需要自己提前在数据库中建好表 然后连接数据库通过侧边栏的persistence来生成实体类和相应的 ...
- Android Studio 使用图片
首先将图片放在drawable下 然后: <ImageView android:layout_width="wrap_content" android:layout_heig ...
- sql 日期问题从周转换到日期
alter procedure p_date@year int=2005, --年份@week int=33, --第几周@firstday datetime =null output, ...
- [NOIP2007] 普及组
奖学金 模拟 开个struct排序即可 c++吼啊 /*by SilverN*/ #include<algorithm> #include<iostream> #include ...