加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

调用支付宝支付接口

发布时间:2020-12-17 00:03:08 所属栏目:Python 来源:网络整理
导读:支付宝支付 AliPay 阿里的支付也就是支付宝,官网以及出了Python的SDK了~所以要先下载~~ pip install alipay-sdk-python 支付宝沙箱环境的地址? 注意:? 如果安装alipay报 需要先下载一个安装环境: pip install alipay-sdk-python 具体操作 第一步 访问沙箱环

支付宝支付

AliPay

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

pip install alipay-sdk-python

支付宝沙箱环境的地址?

注意:?

  如果安装alipay报

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

pip install alipay-sdk-python

具体操作

第一步 访问沙箱环境地址,注册一个商家账户

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

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

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

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

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

第三步 代码实例

django.conf.urls django.contrib demo.views urlpatterns =<span style="color: #000000;"> [
url(r
<span style="color: #800000;">'
<span style="color: #800000;">^admin/
<span style="color: #800000;">'
<span style="color: #000000;">,admin.site.urls),url(r
<span style="color: #800000;">'
<span style="color: #800000;">^pay$
<span style="color: #800000;">'
<span style="color: #000000;">,AliPayView.as_view()),url(r<span style="color: #800000;">'<span style="color: #800000;">^alipay_handler<span style="color: #800000;">'<span style="color: #000000;">,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 style="color: #008000;">#<span style="color: #008000;"> Create your views here.<span style="color: #008000;">

<span style="color: #008000;"> 沙箱环境地址:https://openhome.alipay.com/platform/appDaily.htm?tab=info

<span style="color: #008000;">#<span style="color: #008000;"> "https://openapi.alipaydev.com/gateway.do",APP_ID,APP_PRIVATE_KEY,"json",CHARSET,ALIPAY_PUBLIC_KEY,"RSA2"

<span style="color: #0000ff;">def<span style="color: #000000;"> ali_pay():
<span style="color: #008000;">#<span style="color: #008000;"> 为阿里支付实例化一个配置信息对象
alipay_config = AlipayClientConfig(sandbox_debug=<span style="color: #000000;">True)
<span style="color: #008000;">#<span style="color: #008000;"> 初始化各种配置信息
<span style="color: #008000;">#<span style="color: #008000;"> 阿里提供服务的接口
alipay_config.server_url = <span style="color: #800000;">"<span style="color: #800000;">https://openapi.alipaydev.com/gateway.do<span style="color: #800000;">"
<span style="color: #008000;">#<span style="color: #008000;"> 申请的沙箱环境的app_id
alipay_config.app_id = <span style="color: #800000;">"<span style="color: #800000;">2016091800540924<span style="color: #800000;">"
<span style="color: #008000;">#<span style="color: #008000;"> 商户的私钥
with open(<span style="color: #800000;">"<span style="color: #800000;">keys/app_private_key.txt<span style="color: #800000;">"<span style="color: #000000;">) as f:
alipay_config.app_private_key =<span style="color: #000000;"> f.read()
<span style="color: #008000;">#<span style="color: #008000;"> 阿里的公钥
with open(<span style="color: #800000;">"<span style="color: #800000;">keys/alipay_public_key.txt<span style="color: #800000;">"<span style="color: #000000;">) as f:
alipay_config.alipay_public_key =<span style="color: #000000;"> f.read()
<span style="color: #008000;">#<span style="color: #008000;"> 实例化一个支付对象并返回
alipay_client = DefaultAlipayClient(alipay_client_config=<span style="color: #000000;">alipay_config)
<span style="color: #0000ff;">return<span style="color: #000000;"> alipay_client

<span style="color: #0000ff;">class<span style="color: #000000;"> AliPayView(APIView):
<span style="color: #0000ff;">def<span style="color: #000000;"> get(self,request):
<span style="color: #0000ff;">return render(request,<span style="color: #800000;">"<span style="color: #800000;">pay.html<span style="color: #800000;">"<span style="color: #000000;">)

</span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 生成支付宝自带页面的API</span>
<span style="color: #0000ff;"&gt;def</span><span style="color: #000000;"&gt; post(self,request):
    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 得到阿里支付的实例化对象</span>
    client =<span style="color: #000000;"&gt; ali_pay()
    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 为API生成一个模板对象 初始化参数用的</span>
    model =<span style="color: #000000;"&gt; AlipayTradePagePayModel()
    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 订单号</span>
    model.out_trade_no = <span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;pay</span><span style="color: #800000;"&gt;"</span> +<span style="color: #000000;"&gt; str(time.time())
    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 金额</span>
    model.total_amount = 8888
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 商品标题</span>
    model.subject = <span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;测试</span><span style="color: #800000;"&gt;"</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 商品详细内容</span>
    model.body = <span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;支付宝测试</span><span style="color: #800000;"&gt;"</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 销售产品码,与支付宝签约的产品码名称</span>
    model.product_code = <span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;FAST_INSTANT_TRADE_PAY</span><span style="color: #800000;"&gt;"</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 实例化一个请求对象</span>
    request = AlipayTradePagePayRequest(biz_model=<span style="color: #000000;"&gt;model)
    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; get请求 用户支付成功后返回的页面请求地址</span>
    request.return_url = <span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;http://140.143.63.45:8000/alipay_handler</span><span style="color: #800000;"&gt;"</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; post请求 用户支付成功通知商户的请求地址</span>
    request.notify_url = <span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;http://140.143.63.45:8000/alipay_handler</span><span style="color: #800000;"&gt;"</span>
    <span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 利用阿里支付对象发一个获得页面的请求 参数是request</span>
    response = client.page_execute(request,http_method=<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;GET</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;)
    </span><span style="color: #0000ff;"&gt;return</span><span style="color: #000000;"&gt; redirect(response)

<span style="color: #0000ff;">class<span style="color: #000000;"> PayHandlerView(APIView):

</span><span style="color: #0000ff;"&gt;def</span><span style="color: #000000;"&gt; get(self,request):
    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; return_url的回调地址</span>
    <span style="color: #0000ff;"&gt;print</span><span style="color: #000000;"&gt;(request.data)
    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 用户支付成功之后回到哪</span>
    <span style="color: #0000ff;"&gt;return</span> HttpResponse(<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;return_url测试</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;)

</span><span style="color: #0000ff;"&gt;def</span><span style="color: #000000;"&gt; post(self,request):
    </span><span style="color: #0000ff;"&gt;print</span><span style="color: #000000;"&gt;(request.data)
    </span><span style="color: #008000;"&gt;#</span><span style="color: #008000;"&gt; 用户支付成功 在这里修改订单状态以及优惠券贝里等等情况</span>
    <span style="color: #0000ff;"&gt;return</span> HttpResponse(<span style="color: #800000;"&gt;"</span><span style="color: #800000;"&gt;notify_url</span><span style="color: #800000;"&gt;"</span><span style="color: #000000;"&gt;)

views.py

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读