调用支付宝支付接口

调用支付宝支付接口,第1张

概述支付宝支付AliPay阿里的支付也就是支付宝,官网以及出了Python的SDK了~所以要先下载~~pip install alipay-sdk-python支付宝沙箱环境的地址 https://openhome.alipay.com/platform/appDaily.htm?tab=info注意: 如果安装alipay报需要先下载一个安装环境:pip install alipay-sdk-python 具体 *** 作第一步 访问沙箱环境地址,注册一个商家账户按照他要求的方式生成商户的公钥和私钥~把公钥放入网站~私钥自己留着签名用~~下面是商户信息以及卖家信息~下载一个支付宝的沙箱环境~然后按照下面的账号和密码登录和支付~~只支持安卓的~  第二步 阅读文档 找到一个适合自己的API仔细阅读文档~按照文档的参数要求去向接口发请求~~第三步 代码实例from django.conf.urls import urlfrom django.contrib import adminfrom demo.views import AliPayView, PayHandlerViewurlpatterns = [url(r'^admin/', admin.site.urls),url(r'^pay$', AliPayView.as_view()),url(r'^alipay_handler', PayHandlerView.as_view()),]urls.pyfrom django.shortcuts import render, redirectfrom django.http import HttpResponsefrom rest_framework.views import APIViewfrom rest_framework.response import Responsefrom alipay.aop.api.DefaultAlipayClient import DefaultAlipayClientfrom alipay.aop.api.AlipayClientConfig import AlipayClientConfigfrom alipay.aop.api.domain.AlipayTradePrecreateModel import AlipayTradePrecreateModelfrom alipay.aop.api.request. AlipayTradePrecreateRequest import AlipayTradePrecreateRequestfrom alipay.aop.api.domain.AlipayTradePagePayModel import AlipayTradePagePayModelfrom alipay.aop.api.request.AlipayTradePagePayRequest import AlipayTradePagePayRequestimport time# Create your views here.# 沙箱环境地址:https://openhome.alipay.com/platform/appDaily.htm?tab=info# "https://openapi.alipaydev.com/gateway.do", APP_ID, APP_PRIVATE_KEY, "json", CHARSET, ALIPAY_PUBLIC_KEY, "RSA2"def ali_pay():# 为阿里支付实例化一个配置信息对象alipay_config = AlipayClientConfig(sandbox_debug=True)# 初始化各种配置信息# 阿里提供服务的接口alipay_config.server_url = "https://openapi.alipaydev.com/gateway.do"# 申请的沙箱环境的app_idalipay_config.app_id = "2016091800540924"# 商户的私钥with open("keys/app_private_key.txt") as f:alipay_config.app_private_key = f.read()# 阿里的公钥with open("keys/alipay_public_key.txt") as f:alipay_config.alipay_public_key = f.read()# 实例化一个支付对象并返回alipay_client = DefaultAlipayClient(alipay_client_config=alipay_config)return alipay_clientclass AliPayView(APIView):def get(self, request):return render(request, "pay.html")# 生成支付宝自带页面的APIdef post(self, request):# 得到阿里支付的实例化对象client = ali_pay()# 为API生成一个模板对象 初始化参数用的model = AlipayTradePagePayModel()# 订单号model.out_trade_no = "pay" + str(time.time())# 金额model.total_amount = 8888# 商品标题model.subject = "测试"# 商品详细内容model.body = "支付宝测试"# 销售产品码,与支付宝签约的产品码名称model.product_code = "FAST_INSTANT_TRADE_PAY"# 实例化一个请求对象request = AlipayTradePagePayRequest(biz_model=model)# get请求 用户支付成功后返回的页面请求地址request.return_url = "http://140.143.63.45:8000/alipay_handler"# post请求 用户支付成功通知商户的请求地址request.notify_url = "http://140.143.63.45:8000/alipay_handler"# 利用阿里支付对象发一个获得页面的请求 参数是requestresponse = client.page_execute(request, http_method="GET")return redirect(response)class PayHandlerView(APIView):def get(self, request):# return_url的回调地址print(request.data)# 用户支付成功之后回到哪return HttpResponse("return_url测试")def post(self, request):print(request.data)# 用户支付成功 在这里修改订单状态以及优惠券贝里等等情况return HttpResponse("notify_url")views.pyviews.py 注意: 我们的回调url的测试要放在公网IP上~所以测试的时候放入自己服务器 支付宝支付AliPay

阿里的支付也就是支付宝,官网以及出了Python的SDK了~所以要先下载~~

pip install alipay-sdk-python

支付宝沙箱环境的地址 href="https://openhome.alipay.com/platform/appDaily.htm?tab=info" target="_blank">https://openhome.alipay.com/platform/appDaily.htm?tab=info

注意: 

  如果安装alipay报

  需要先下载一个安装环境:

pip install alipay-sdk-python具体 *** 作第一步 访问沙箱环境地址,注册一个商家账户

按照他要求的方式生成商户的公钥和私钥~把公钥放入网站~私钥自己留着签名用~~

下面是商户信息以及卖家信息~下载一个支付宝的沙箱环境~

然后按照下面的账号和密码登录和支付~~只支持安卓的~

 第二步 阅读文档 找到一个适合自己的API

仔细阅读文档~按照文档的参数要求去向接口发请求~~

第三步 代码实例 django.conf.urls django.contrib demo.vIEws urlpatterns =<span > [
url(r
<span >'
<span >^admin/
<span >'
<span >,admin.site.urls),url(r
<span >'
<span >^pay$
<span >'
<span >,AliPayVIEw.as_vIEw()),url(r<span >'<span >^alipay_handler<span >'<span >,PayHandlerVIEw.as_vIEw()),]

django.shortcuts django.http rest_framework.vIEws rest_framework.response alipay.aop.API.DefaultAlipayClIEnt alipay.aop.API.AlipayClIEntConfig alipay.aop.API.domain.AlipayTradePrecreateModel alipay.aop.API.request. AlipayTradePrecreateRequest alipay.aop.API.domain.AlipayTradePagePayModel alipay.aop.API.request.AlipayTradePagePayRequest <span >#<span > Create your vIEws here.<span >

<span > 沙箱环境地址:https://openhome.alipay.com/platform/appDaily.htm?tab=info

<span >#<span > "https://openAPI.alipaydev.com/gateway.do",APP_ID,APP_PRIVATE_KEY,"Json",CHARSET,AliPAY_PUBliC_KEY,"RSA2"

<span >def<span > ali_pay():
<span >#<span > 为阿里支付实例化一个配置信息对象
alipay_config = AlipayClIEntConfig(sandBox_deBUG=<span >True)
<span >#<span > 初始化各种配置信息
<span >#<span > 阿里提供服务的接口
alipay_config.server_url = <span >"<span >https://openAPI.alipaydev.com/gateway.do<span >"
<span >#<span > 申请的沙箱环境的app_ID
alipay_config.app_ID = <span >"<span >2016091800540924<span >"
<span >#<span > 商户的私钥
with open(<span >"<span >keys/app_private_key.txt<span >"<span >) as f:
alipay_config.app_private_key =<span > f.read()
<span >#<span > 阿里的公钥
with open(<span >"<span >keys/alipay_public_key.txt<span >"<span >) as f:
alipay_config.alipay_public_key =<span > f.read()
<span >#<span > 实例化一个支付对象并返回
alipay_clIEnt = DefaultAlipayClIEnt(alipay_clIEnt_config=<span >alipay_config)
<span >return<span > alipay_clIEnt

<span >class<span > AliPayVIEw(APIVIEw):
<span >def<span > get(self,request):
<span >return render(request,<span >"<span >pay.HTML<span >"<span >)

</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;#</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt; <a href="https://www.jb51.cc/tag/shengcheng/" target="_blank" >生成</a>支付宝<a href="https://www.jb51.cc/tag/zidai/" target="_blank" >自带</a><a href="https://www.jb51.cc/tag/yemian/" target="_blank" >页面</a>的<a href="https://m.jb51.cc/tag/API/" target="_blank" >API</a></span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;def</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; post(self,request):    </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;#</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt; 得到阿里支付的实例化对象</span>    cl<a href="https://m.jb51.cc/tag/IE/" target="_blank" >IE</a>nt =<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; a<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>_pay()    </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;#</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt; 为<a href="https://m.jb51.cc/tag/API/" target="_blank" >API</a><a href="https://www.jb51.cc/tag/shengcheng/" target="_blank" >生成</a><a href="https://www.jb51.cc/tag/yige/" target="_blank" >一个</a><a href="https://m.jb51.cc/tag/moban/" target="_blank" >模板</a>对象 初始化<a href="https://m.jb51.cc/tag/canshu/" target="_blank" >参数</a>用的</span>    model =<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; A<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>pay<a href="https://www.jb51.cc/tag/Trad/" target="_blank" >Trad</a>ePagePayModel()    </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;#</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt; 订单号</span>    model.out_<a href="https://www.jb51.cc/tag/Trad/" target="_blank" >Trad</a>e_no = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;pay</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;"</span> +<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; str(time.time())    </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;#</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt; 金额</span>    model.total_amount = 8888    <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;#</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt; 商品<a href="https://www.jb51.cc/tag/biaoti/" target="_blank" >标题</a></span>    model.subject = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;测试</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;"</span>    <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;#</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt; 商品详细<a href="https://www.jb51.cc/tag/neirong/" target="_blank" >内容</a></span>    model.body = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;支付宝测试</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;"</span>    <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;#</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt; 销售产品码,与支付宝签约的产品码<a href="https://www.jb51.cc/tag/mingcheng/" target="_blank" >名称</a></span>    model.product_code = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;FAST_INSTANT_<a href="https://www.jb51.cc/tag/Trad/" target="_blank" >Trad</a>E_PAY</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;"</span>    <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;#</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt; 实例化<a href="https://www.jb51.cc/tag/yige/" target="_blank" >一个</a>请求对象</span>    request = A<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>pay<a href="https://www.jb51.cc/tag/Trad/" target="_blank" >Trad</a>ePagePayRequest(biz_model=<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;model)    </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;#</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt; get请求 <a href="https://www.jb51.cc/tag/yonghu/" target="_blank" >用户</a>支付成功后返回的<a href="https://www.jb51.cc/tag/yemian/" target="_blank" >页面</a>请求<a href="https://m.jb51.cc/tag/dizhi/" target="_blank" >地址</a></span>    request.return_url = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;<a href="https://m.jb51.cc/tag/http/" target="_blank" >http</a>://140.143.63.45:8000/a<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>pay_handler</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;"</span>    <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;#</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt; post请求 <a href="https://www.jb51.cc/tag/yonghu/" target="_blank" >用户</a>支付成功<a href="https://www.jb51.cc/tag/tongzhi/" target="_blank" >通知</a>商户的请求<a href="https://m.jb51.cc/tag/dizhi/" target="_blank" >地址</a></span>    request.notify_url = <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;<a href="https://m.jb51.cc/tag/http/" target="_blank" >http</a>://140.143.63.45:8000/a<a href="https://m.jb51.cc/tag/li/" target="_blank" >li</a>pay_handler</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;"</span>    <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;#</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt; 利用阿里支付对象发<a href="https://www.jb51.cc/tag/yige/" target="_blank" >一个</a>获得<a href="https://www.jb51.cc/tag/yemian/" target="_blank" >页面</a>的请求 <a href="https://m.jb51.cc/tag/canshu/" target="_blank" >参数</a>是request</span>    response = cl<a href="https://m.jb51.cc/tag/IE/" target="_blank" >IE</a>nt.page_execute(request,<a href="https://m.jb51.cc/tag/http/" target="_blank" >http</a>_method=<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;GET</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;)    </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;return</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; redirect(response)

<span >class<span > PayHandlerVIEw(APIVIEw):

</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;def</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; get(self,request):    </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;#</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt; return_url的回调<a href="https://m.jb51.cc/tag/dizhi/" target="_blank" >地址</a></span>    <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;print</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;(request.data)    </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;#</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt; <a href="https://www.jb51.cc/tag/yonghu/" target="_blank" >用户</a>支付成功之后回到哪</span>    <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;return</span> <a href="https://m.jb51.cc/tag/http/" target="_blank" >http</a>Response(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;return_url测试</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;)</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;def</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt; post(self,request):    </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;print</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;(request.data)    </span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt;#</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #008000;"&gt; <a href="https://www.jb51.cc/tag/yonghu/" target="_blank" >用户</a>支付成功 <a href="https://www.jb51.cc/tag/zaizheli/" target="_blank" >在这里</a><a href="https://www.jb51.cc/tag/xiugai/" target="_blank" >修改</a>订单状态<a href="https://m.jb51.cc/tag/yiji/" target="_blank" >以及</a>优惠券贝里等等情况</span>    <span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #0000ff;"&gt;return</span> <a href="https://m.jb51.cc/tag/http/" target="_blank" >http</a>Response(<span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;notify_url</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #800000;"&gt;"</span><span https://m.jb51.cc/tag/color/" target="_blank" >color</a>: #000000;"&gt;)

vIEws.py

注意: 我们的回调url的测试要放在公网IP上~所以测试的时候放入自己服务器

总结

以上是内存溢出为你收集整理的调用支付宝支付接口全部内容,希望文章能够帮你解决调用支付宝支付接口所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1208065.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存