发起支付
1. 后端接口设计
请求方式: GET /orders/(?P<order_id>\d+)/payment/
请求参数: 路径参数
参数 | 类型 | 是否必须 | 说明 |
---|---|---|---|
order_id | str | 是 | 订单编号 |
返回数据: JSON
返回值 | 类型 | 是否必须 | 说明 |
---|---|---|---|
alipay_url | str | 是 | 支付宝支付链接 |
2. 后端实现
在payment/views.py中创建视图
在配置文件中编辑支付宝的配置信息
# 支付宝 ALIPAY_APPID = "2016081600258081" ALIPAY_URL = "https://openapi.alipaydev.com/gateway.do" ALIPAY_DEBUG = True
3. 前端实现
在order_success.js中编写
// 去支付 next_operate: function() { if (this.pay_method == 1) { location.href = '/index.html'; } else { // 发起支付 axios.get(this.host + '/orders/' + this.order_id + '/payment/', { headers: { 'Authorization': 'JWT ' + this.token }, responseType: 'json' }) .then(response= > { // 跳转到支付宝支付 location.href = response.data.alipay_url; }) .catch(error= > { console.log(error.response.data); }) } }