Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

请问是否支持请求预生成二维码付款方式? #9

Closed
relei1988 opened this issue Mar 20, 2017 · 24 comments
Closed

请问是否支持请求预生成二维码付款方式? #9

relei1988 opened this issue Mar 20, 2017 · 24 comments

Comments

@relei1988
Copy link

然后异步检测是否收款的

@fzlee
Copy link
Owner

fzlee commented Mar 20, 2017

你说的是这个吗,用户跳转到支付宝网页,通过扫码或者密实现支付。然后后台可以异步收到支付提醒
https://doc.open.alipay.com/doc2/detail?treeId=62&articleId=103566&docType=1

用create_web_trade可以实现

@relei1988
Copy link
Author

不是这个,是这个
https://doc.open.alipay.com/doc2/detail?treeId=194&articleId=105072&docType=1
WEB和网页还有即时到账现在都要求域名备案的,这个是可以不备案的一个方法,但是官方没python sdk

@fzlee
Copy link
Owner

fzlee commented Mar 20, 2017

暂时没有,反正最近我有时间,明天我做一个试试

@relei1988
Copy link
Author

太好啦,我看了半天不会弄…

@fzlee
Copy link
Owner

fzlee commented Mar 21, 2017

试试新版的代码吧, pip install python-alipay-sdk --upgrade

测试不是很严谨,有bug及时找我

@relei1988
Copy link
Author

好的,我下班了试一下。支持预生成二维码收款的话就可以不用营业执照和备案啦

@relei1988
Copy link
Author

关于DEMO

预创建/查询/取消当面付

alipay = Alipay(appid="", ...)

# create an order
result1 = alipay.precreate_face_to_face_trade(
    "out_trade_no", 100, "test subject"
    # you may input more parameters here, refer to alipay official doc for details
    )

if result1["code"] = "10000":
    print("Order is paid")
elif result1["code"] == "10003":
    print("Pending paid by user")
    for i in range(10):
    # check every 3s to see if the order is paid
        time.sleep(3)
        result2 = alipay.query_face_to_face_trade(out_trade_no=out_trade_no)
        if result2["code"] == "10000":
            print("Order is paid")
            break

    # order is not paid in 30s , cancel this order
    alipay.cancel_face_to_face_trade(out_trade_no=out_trade_no)

中Alipay应为AliPay,if result1["code"] = "10000":应为if result1["code"] == "10000":

别的还没开始试,把密钥啥的输入完发现是要贴路径= =

@fzlee
Copy link
Owner

fzlee commented Mar 21, 2017

哈哈哈, 好的,我稍后会修改文档

@fzlee
Copy link
Owner

fzlee commented Mar 21, 2017

你是用的Python2吗?里面的code即使是unicode也应该可以取出的,因为python会对unicode做隐式的转换, 比如:

a = {u"name": u"Lily"}
a["name"]
u'Lily'
>>>

@relei1988
Copy link
Author

返回值为:
{u'alipay_trade_precreate_response': {u'msg': u'Success', u'out_trade_no': u'out_trade_no', u'code': u'10000', u'qr_code': u'https://qr.alipay.com/bax07***6095'}, u'sign': u'EDiiwckDOcvMU***I8Hybrrj+WTxZ89CzNJ55HSgPk0frTvJu7y7XszxrI='}
要用
if result1['alipay_trade_precreate_response']['code'] == "10000":
才行……

@relei1988
Copy link
Author

是字典里套了个字典的关系 前面我复制的时候少复制了第一个{

@fzlee
Copy link
Owner

fzlee commented Mar 21, 2017

哦,了解, 我明天修改一下吧,其实我是希望所有result返回内容里面不带有alipay_trade_precreate_response这个字段的。

@relei1988
Copy link
Author

我了解啦 那我等更新了再操作 辛苦了

@fzlee
Copy link
Owner

fzlee commented Mar 22, 2017

0.6.1 好了,你可以再试试

@relei1988
Copy link
Author

神速(大拇指),试了有问题我再报告哈

@relei1988
Copy link
Author

现在有个问题是
result1 = alipay.precreate_face_to_face_trade(out_trade_no="20150320010101001", total_amount=0.01,subject= "test subject")
用这行可以正确生成付款二维码链接,扫码确认正常,但是result1中返回的code其实是指生成二维码是否成功,只要生成二维码成功了返回都是10000,例子中

if result1["code"] == "10000":
    print("Order is paid")

讲的应该是异步检测,检测的结果不管付没付都是生成二维码的结果,也就是成功

@relei1988
Copy link
Author

查了一下,异步检测是要有服务器的,ali会post给你一个支付成功的通知= =
https://doc.open.alipay.com/docs/doc.htm?spm=a219a.7629140.0.0.oLj0ut&treeId=194&articleId=103296&docType=1

@relei1988
Copy link
Author

node js的F2F项目里,作者写道

服务器网络暴毙? 没收到支付宝回调?

不要慌我给你实现了一个checkInvoiceStatus方法.

	/**
     * 校验通知签名
     * @param  {string} postData  支付宝post过的来数据
     * @return {bool}             是否通过校验   
     */
	verifyCallback(postData) {
		return aop.notifyVerify(postData, this._config);
	}

	/**
	 * 检测订单状况
	 * @param  {string} tradeNo 订单号
	 * @return {Promise}
	 */
	checkInvoiceStatus(tradeNo) {
		return new Promise((resolve, reject) => {
			if (tradeNo == "" || tradeNo == null || tradeNo == undefined) {
				return reject({ message: "订单号不能为空.", info : null });
			}
			aop.execute("alipay.trade.query", this._config, {
				out_trade_no: tradeNo
			}).then(resolve).catch(reject);
		});
	}

看来下是这段,不知道是不是一定要有服务器接收post,还是说可以手动request查一下……

@fzlee
Copy link
Owner

fzlee commented Mar 22, 2017

notify_url 可以在使用precreate_face_to_face_order 的时候传入,然后支付成功会收到支付宝的回调请求。

然后为了不使用这种回调的方法,可以检查result["trade_status"], 如果是“WAIT_BUYER_PAY”, 说明没有支付,如果是“TRADE_SUCCESS”, 则是支付了

>>> alipay.query_face_to_face_trade(out_trade_no="out_trade_no23")
{'code': '10000', 'msg': 'Success', 'buyer_logon_id': 'csq***@sandbox.com', 'buyer_pay_amount': '0.00', 'buyer_user_id': '20881
02169481075', 'invoice_amount': '0.00', 'open_id': '20880072506750308812798160715407', 'out_trade_no': 'out_trade_no23', 'point
_amount': '0.00', 'receipt_amount': '0.00', 'total_amount': '20.00', 'trade_no': '2017032221001004070200177033', 'trade_status'
: 'WAIT_BUYER_PAY'}

@relei1988
Copy link
Author

好的我一会看一看,谢谢

@relei1988
Copy link
Author

relei1988 commented Mar 22, 2017

刚传入alipay.precreate_face_to_face_trade("out_trade_no23", total_amount=0.01,subject= "test subject")

{u'msg': u'Success', u'out_trade_no': u'out_trade_no23', u'code': u'10000', u'qr_code': u'https://qr.alipay.com/ba***niq4091'}

返回成功,然后查询时同样传入alipay.query_face_to_face_trade(out_trade_no="out_trade_no23")
返回交易不存在
{u'code': u'40004', u'sub_msg': u'\u4ea4\u6613\u4e0d\u5b58\u5728', u'sub_code': u'ACQ.TRADE_NOT_EXIST', u'receipt_amount': u'0.00', u'out_trade_no': u'out_trade_no23', u'buyer_pay_amount': u'0.00', u'invoice_amount': u'0.00', u'msg': u'Business Failed', u'point_amount': u'0.00'}

@fzlee
Copy link
Owner

fzlee commented Mar 22, 2017

用户扫码前一直是显示4004的,扫码之后就会有trade_status了,你可以这样试试

alipay = AliPay(appid="", ...)

# create an order
result1 = alipay.precreate_face_to_face_trade(
    "out_trade_no", 100, "test subject"
    # you may input more parameters here, refer to alipay official doc for details
)

# check order status
paid = False
for i in range(10):
    # check every 3s, and 10 times in all
    print("now sleep 3s")
    time.sleep(3)
    result = alipay.query_face_to_face_trade(out_trade_no="out_trade_no24")
    if result.get("trade_status", "") == "TRADE_SUCCESS":
        paid = True
        break
    print("not paid...")

# order is not paid in 30s , cancel this order
if paid is False:
    alipay.cancel_face_to_face_trade(out_trade_no=out_trade_no)

@relei1988
Copy link
Author

果然是要扫一下才行,我以为会有pedding之类的状态,之前看到文档里写的WAIT_BUYER_PAY状态 以为会有

@fzlee
Copy link
Owner

fzlee commented Mar 22, 2017

是的,我之前看文档, 也是以为有你说的那个状态,感觉文档和实际的有点对不上了

@fzlee fzlee closed this as completed Mar 22, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants