跳转到主要内容

Pagseguro API v2封装器

项目描述

python-pagseguro
[![所有贡献者](https://img.shields.io/badge/all_contributors-6-orange.svg?style=flat-square)](#contributors)
================
[![Build
Status](https://travis-ci.org/rochacbruno/python-pagseguro.png)](https://travis-ci.org/rochacbruno/python-pagseguro)
[![Coverage
Status](https://coveralls.io/repos/rochacbruno/python-pagseguro/badge.png)](https://coveralls.io/r/rochacbruno/python-pagseguro)
[![Code Health](https://landscape.io/github/rochacbruno/python-pagseguro/master/landscape.svg)](https://landscape.io/github/rochacbruno/python-pagseguro/master)
[![Gitter chat](https://badges.gitter.im/gitterHQ/gitter.png)](https://gitter.im/python-pagseguro/Lobby)

<a target="_blank" href="https://www.paypal.com/cgi-bin/webscr?cmd=_donations&amp;business=rochacbruno%40gmail%2ecom&amp;lc=BR&amp;item_name=pythonpagseguro&amp;no_note=0&amp;currency_code=BRL&amp;bn=PP%2dDonationsBF%3abtn_donate_SM%2egif%3aNonHostedGuest"><img alt='Donate with Paypal' src='http://www.paypalobjects.com/en_US/i/btn/btn_donate_SM.gif' /></a>

使用requests集成Pagseguro的支付和通知API v2。

安装
==========================
```bash
pip install pagseguro
```

ou


```bash
使用pip安装:pip install -e git+https://github.com/rochacbruno/python-pagseguro#egg=pagseguro
```

ou

```
使用git克隆:git clone https://github.com/rochacbruno/python-pagseguro
进入目录:cd python-pagseguro
安装requirements.txt中的依赖:pip install -r requirements.txt
安装Python包:python setup.py install
```

运行测试:Rodando os testes
=================

```
执行测试:make test
```

如何使用:Como usar
=========

### 购物车 / 销售订单

**PagSeguro**的一个实例可以作为销售订单或购物车。需要通过参数传入电子邮件和令牌来创建实例。

可选地,可以传入包含要直接传递给API的值的参数**data**。

```python
from pagseguro import PagSeguro

pg = PagSeguro(email="seuemail@dominio.com", token="ABCDEFGHIJKLMNO")
```

### 沙盒和自定义配置

实例化`PagSeguro`对象时,可以传入一个包含要使用的配置类的参数`config`。变量`config`只能接受`dict`类型。

```python
from pagseguro import PagSeguro

config = {'sandbox': True}
pg = PagSeguro(email="seuemail@dominio.com", token="ABCDEFGHIJKLMNO", config=config)
```

您的`config`也可以覆盖类中预定义的一些变量。它们是:

- CURRENCY - 使用的货币。默认值:`'BRL'`
- DATETIME_FORMAT - 日期/时间格式。默认值:`'%Y-%m-%dT%H:%M:%S'`
- REFERENCE_PREFIX - 产品参考值格式。默认值:`'REF%s'` 注意:在这种情况下,必须在末尾保留`%s`,以便自动填充。
- USE_SHIPPING - 使用交付地址。默认值:`True`


### 配置买家信息

```python
pg.sender = {
"name": "Bruno Rocha",
"area_code": 11,
"phone": 981001213,
"email": "rochacbruno@gmail.com",
}
```

### 配置交付地址
```python
pg.shipping = {
"type": pg.SEDEX,
"street": "Av Brig Faria Lima",
"number": 1234,
"complement": "5 andar",
"district": "Jardim Paulistano",
"postal_code": "06650030",
"city": "Sao Paulo",
"state": "SP",
"country": "BRA"
}
```

如果未提供**country**,则默认值为"BRA"。

**type**可以是pg.SEDEX、pg.PAC或pg.NONE。
可选地,可以是数字,遵循pagseguro表

| 数值 | 描述 | 类型 |
| ------ | --------- | ---- |
| 1 | PAC | pg.PAC |
| 2 | SEDEX | pg.SEDEX |
| 3 | 未指定 | pg.NONE |

**shipping**的可选值
- "cost": "123456.26"
十进制,两位小数(例如,1234.56),大于0.00且小于等于9999999.00。


### 配置参考值

参考值通常是识别您系统中购买的代码

默认情况下,参考值将带有前缀"REF",但可以通过设置不同的前缀来更改

```python
pg.reference_prefix = "CODE"
pg.reference_prefix = None # 禁用前缀
```

```python
pg.reference = "00123456789"
print pg.reference
"REF00123456789"
```

### 配置额外金额

指定应添加到或从付款总金额中添加或减去的额外金额。此金额可以表示付款中的额外费用或折扣,如果值为负数。

格式:浮点数(正数或负数)。

```python
pg.extra_amount = 12.70
```

### 在购物车中添加产品

购物车是一个包含字典的列表,代表以下格式的每个产品。

#### 添加多个产品

```python
pg.items = [
{"id": "0001", "description": "产品 1", "amount": 354.20, "quantity": 2, "weight": 200},
{"id": "0002", "description": "产品 2", "amount": 50, "quantity": 1, "weight": 1000}
]
```

产品的**weight**以克为单位表示

#### 添加单个产品

传统形式

```python
pg.items.append(
{"id": "0003", "description": "产品 3", "amount": 354.20, "quantity": 2, "weight": 200},
)
```

或通过助手

```python
pg.add_item(id="0003", description="产品 4", amount=320, quantity=1, weight=2500)
```

### 配置重定向 URL

支付完成后买家将被重定向到何处

```python
pg.redirect_url = "http://meusite.com/obrigado"
```

### 配置通知 URL(可选)

```python
pg.notification_url = "http://meusite.com/notification"
```

### 执行结账流程

在购物车全部配置并添加项目后,例如当客户点击“结账”按钮时,应执行以下方法。

```python
response = pg.checkout()
```

checkout 方法向 pagseguro 发送请求并返回一个包含 code、date、payment_url、errors 属性的 PagSeguroResponse 对象。

建议将交易代码存储在您的数据库中,同时存储购物车信息以便内部控制。

使用 **payment_url** 将买家发送到 pagseguro 的支付页面。

```python
return redirect(response.payment_url)
```

支付后,买家将通过全局返回 URL 配置或使用 **redirect_url** 参数中指定的 URL 返回到您的网站。

# 通知

PagSeguro 使用 HTTP 协议通过 POST 方法将通知发送到您配置的 URL。

假设您将在:http://seusite.com/notification 接收通知

> 模拟代码

```python
from pagseguro import PagSeguro

def notification_view(request)
notification_code = request.POST['notificationCode']
pg = PagSeguro(email="seuemail@dominio.com", token="ABCDEFGHIJKLMNO")
notification_data = pg.check_notification(notification_code)
...
```

在上面的例子中,我们获取了通过 pagseguro 发送的 **notificationCode**,并进行了查询以获取通知数据,返回将是以下格式的 Python 字典

```python
{
"date": datetime(2013, 01, 01, 18, 23, 0000),
"code": "XDFD454545",
"reference": "REF00123456789",
"type": 1,
"status": 3,
"cancellationSource": "INTERNAL",
...
}
```

完整的值列表可以在 https://pagseguro.uol.com.br/v2/guia-de-integracao/api-de-notificacoes.html 查阅


# 实现

> 需要实现的实现,期待您的 Pull Request!!!

## Quokka CMS
[Quokka Cart PagSeguro Processor](https://github.com/pythonhub/quokka-cart/blob/master/processors/pagseguro_processor.py)

## Django 示例

## Flask 示例

[FlaskSeguro](https://github.com/rochacbruno/python-pagseguro/tree/master/examples/flask)
by @shyba

## web2py 示例



## 维护者

感谢这些可爱的人们 ([emoji key](https://github.com/kentcdodds/all-contributors#emoji-key))

<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
| [<img src="https://avatars1.githubusercontent.com/u/1661112?v=4" width="100px;"/><br /><sub>Patrick Mazulo</sub>](http://blog.dunderlabs.com)<br />[💻](https://github.com/rochacbruno/python-pagseguro/commits?author=mazulo "Code") [📖](https://github.com/rochacbruno/python-pagseguro/commits?author=mazulo "Documentation") | [<img src="https://avatars3.githubusercontent.com/u/234982?v=4" width="100px;"/><br /><sub>mauler</sub>](http://mauler.github.io)<br />[💻](https://github.com/rochacbruno/python-pagseguro/commits?author=mauler "Code") [📖](https://github.com/rochacbruno/python-pagseguro/commits?author=mauler "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/178641?v=4" width="100px;"/><br /><sub>Ellison Leão</sub>](http://ellison.rocks)<br />[💻](https://github.com/rochacbruno/python-pagseguro/commits?author=ellisonleao "Code") [📖](https://github.com/rochacbruno/python-pagseguro/commits?author=ellisonleao "Documentation") | [<img src="https://avatars3.githubusercontent.com/u/13952931?v=4" width="100px;"/><br /><sub>Augusto Goulart</sub>](https://augustogoulart.me/)<br />[💻](https://github.com/rochacbruno/python-pagseguro/commits?author=augustogoulart "Code") [📖](https://github.com/rochacbruno/python-pagseguro/commits?author=augustogoulart "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/458654?v=4" width="100px;"/><br /><sub>Bruno Rocha</sub>](http://brunorocha.org)<br />[💻](https://github.com/rochacbruno/python-pagseguro/commits?author=rochacbruno "Code") [📖](https://github.com/rochacbruno/python-pagseguro/commits?author=rochacbruno "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/153288?v=4" width="100px;"/><br /><sub>Guto Maia</sub>](http://gutomaia.net)<br />[💻](https://github.com/rochacbruno/python-pagseguro/commits?author=gutomaia "Code") [📖](https://github.com/rochacbruno/python-pagseguro/commits?author=gutomaia "Documentation") |
| :---: | :---: | :---: | :---: | :---: | :---: |
<!-- ALL-CONTRIBUTORS-LIST:END -->

本项目遵循[all-contributors](https://github.com/kentcdodds/all-contributors)规范。欢迎任何形式的贡献!

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。

源分发

pagseguro-0.3.4.tar.gz (19.4 kB 查看哈希)

上传时间 源代码

支持者