打印应用

简介

用户扫描商家的云打印二维码,进入商家的打印页面,在这个页面选择从支付宝选择需要打印的发票,由支付宝将用户选择的发票推送给商家打印系统,用户自助打印发票。

支付宝提供的能力

1.扫码能力
使用支付宝【扫一扫】,可以进入商家的云打印页面。

2.发票输出能力
用户主动选取发票管家的发票后,可以输出给云打印系统进行发票打印。

DEMO示例

image.png

系统流程

image.png

接入说明

该解决方案适用于支付宝外商户为h5应用的调用,通过商户应用和支付宝互相唤起获取用户选择输出的发票列表token,商户应用再通过支付宝openapi接口换取发票列表。

1.H5唤起支付宝发票列表组件

唤起支付宝h5发票列表链接示例:
https://render.alipay.com/p/s/i?appId=20000920&startMultApp=YES&url=%2Fwww%2FinvoiceSelect.htm%3Fscene%3DINVOICE_EXPENSE%26einvMerchantId%3D9131000074027295XF%26serverRedirectUrl%3Dhttp%253A%252F%252Fisv.app.com%253Fparam1%253Daaa%2526param2%253Dbbb

链接中各节点说明:

url参数值的构造分析

示例:
/www/invoiceSelect.htm?scene=INVOICE_EXPENSE&einvMerchantId=9131000074027295XF&serverRedirectUrl=http%3A%2F%2Fisv.app.com%3Fparam1%3Daaa%26param2%3Dbbb

  • /www/invoiceSelect.htm?scene=INVOICE_EXPENSE固定值不要做修改
  • einvMerchantId=#9131000074027295XF#isv提供给支付宝的企业税号(用户选择发票要交付给的企业)需要提供企业信息给支付宝做配置,企业信息配置模版请查看本文5,6章节
  • serverRedirectUrl=#http%3A%2F%2Fisv.app.com%3Fparam1%3Daaa%26param2%3Dbbb#为isv提供给支付宝的回调地址,即用户选择发票后提交,支付宝需要将用户本次操作token拼接到该地址作为参数回传

注:

  • serverRedirectUrl和url值需要分表做urlEncode,编码格式为utf-8
  • serverRedirectUrl回调地址参数部分命名不能包含(invoiceToken),整体url长度不能超过300个字符

编码示例:URLEncoder.encode("http://isv.app.com?param1=aaa&param2=bbb","utf-8");

构造唤起支付宝H5链接代码示例(java):

//唤起支付宝固定链接
String url="https://render.alipay.com/p/s/i?appId=20000920&startMultApp=YES&url=";
//isv应用回调地址
String isvUrl="http://isv.app.com?param1=aaa&param2=bbb";
//编码之后的应用回调地址
String encodeIsvUrl= URLEncoder.encode(isvUrl,"utf-8");
//发票输出组件固定链接
String alipayUrl="/www/invoiceSelect.htm?scene=INVOICE_EXPENSE&einvMerchantId=9131000074027295XF&serverRedirectUrl="+encodeIsvUrl;
//编码之后的发票输出组件链接
String encodeAlipayUrl= URLEncoder.encode(alipayUrl,"utf-8");
//唤起发票输出组件的完整链接
String alipayLandingUrl=url+encodeAlipayUrl;

2.根据查询令牌获取发票要素列表(alipay.ebpp.invoice.token.batchquery 

场景说明:只有在外部app唤起支付宝选择发票后才能调用该接口,根据用户通过发票管家选择发票列表产生的请求令牌获取用户选择发票要素列表

image.png

SDK调用示例(以JAVA为例,更多语言示例参见接口文档)

AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do","app_id","your private_key","json","GBK","alipay_public_key","RSA2");
AlipayEbppInvoiceTokenBatchqueryRequest request = new AlipayEbppInvoiceTokenBatchqueryRequest();
request.setBizContent("{" +
"\"invoice_token\":\"0136d80096e34a07a528db7cc5bc442888s\"," +
"\"scene\":\"INVOICE_EXPENSE\"" +
"  }");
AlipayEbppInvoiceTokenBatchqueryResponse response = alipayClient.execute(request);
if(response.isSuccess()){
System.out.println("调用成功");
} else {
System.out.println("调用失败");
}

JSON响应示例

{
"alipay_ebpp_invoice_token_batchquery_response":{
    "code":"10000",
    "msg":"Success",
"user_id":"20880000000000000",
      "invoice_element_list":[{
        "expense_status":"WAIT_EXPENSE",
"invoice_status":"SUCCEED",
"invoice_code":"1234567890",
"invoice_no":"12345678",
"invoice_amount":"100",
"invoice_date":"2017-07-11",
"invoice_kind":"PLAIN",
"payer_name":"支付宝(中国)技术有限公司",
"payer_tax_no":"1234567890",
"payee_name":"国力大酒店",
"payee_tax_no":"1234567890",
"invoice_img_url":"https://www.alipay.com",
"has_risk":true,
"has_pdf_file":true
        }]
  }
,"sign":"ERITJKEIJKJHKKKKKKKHJEREEEEEEEEEEE"
}

根据【invoice_img_url】的内容进行打印。

onlineServer