|
1 | 1 | # Braspag SDK for Python |
2 | | -Unofficial SDK for Python to integrate with Braspag payment gateway. |
| 2 | +[](https://pypi.python.org/pypi/braspag-sdk) |
| 3 | + |
| 4 | +An unofficial Python SDK for [Braspag](https://braspag.github.io/). |
| 5 | + |
| 6 | +## Disclaimer |
| 7 | +This SDK is created for personal use and the functionality is limited to the needs of my projects. |
| 8 | +You are more than welcome to contribute the code you need as soon as it follows the same code style. |
| 9 | +Fortunately, this is a very simple SDK, so it should be easy to understand and extend. |
| 10 | + |
| 11 | +# Getting Started |
3 | 12 |
|
4 | 13 | ## Installation |
5 | 14 | ```bash |
6 | 15 | pip install --upgrade braspag-sdk |
7 | 16 | ``` |
8 | 17 |
|
9 | | -## Disclaimer |
10 | | -This SDK is created for personal use and is not officially supported by Braspag/Cielo. |
11 | | -The functionality is limited to what I need for my own projects. |
12 | | -You are more than welcome to contribute the functionality that you need as soon as it follows the same code style. |
13 | | -Fortunately, this is a very simple SDK, so it should be easy to understand and extend. |
| 18 | +## Configuration |
| 19 | +```python |
| 20 | +import os |
| 21 | + |
| 22 | +from braspag_sdk import Braspag, MerchantCredentials, SplitCredentials, EMV3DSCredentials, SilentOrderPostCredentials |
| 23 | + |
| 24 | +merchant_creds = MerchantCredentials( |
| 25 | + merchant_id=os.environ.get('BRASPAG_MERCHANT_ID'), |
| 26 | + merchant_key=os.environ.get('BRASPAG_MERCHANT_KEY'), |
| 27 | +) |
| 28 | + |
| 29 | +split_creds = SplitCredentials( |
| 30 | + client_secret=os.environ.get('BRASPAG_SPLIT_CLIENT_SECRET'), |
| 31 | +) |
| 32 | + |
| 33 | +emv3ds_creds = EMV3DSCredentials( |
| 34 | + client_id=os.environ.get('BRASPAG_EMV3DS_CLIENT_ID'), |
| 35 | + client_secret=os.environ.get('BRASPAG_EMV3DS_CLIENT_SECRET'), |
| 36 | +) |
| 37 | + |
| 38 | +sop_creds = SilentOrderPostCredentials( |
| 39 | + client_id=os.environ.get('BRASPAG_SOP_CLIENT_ID'), |
| 40 | + client_secret=os.environ.get('BRASPAG_SOP_CLIENT_SECRET'), |
| 41 | +) |
| 42 | + |
| 43 | +braspagSdk = Braspag(merchant_creds, is_sandbox=True) |
| 44 | +braspagSdk.add_split(split_creds) |
| 45 | +braspagSdk.add_emv3ds(emv3ds_creds) |
| 46 | +braspagSdk.add_sop(sop_creds) |
| 47 | +``` |
| 48 | + |
| 49 | +## Usage |
| 50 | + |
| 51 | +### Creating a payment |
| 52 | +[Official Documentation](https://braspag.github.io//manual/braspag-pagador) |
| 53 | + |
| 54 | +```python |
| 55 | +from braspag_sdk import Braspag, Sale, Customer, Address, CreditCard, Payment, SplitPayment |
| 56 | + |
| 57 | +# Configuration |
| 58 | +braspagSdk: Braspag = ... |
| 59 | + |
| 60 | +print(f'Creating payment transaction in Braspag...') |
| 61 | + |
| 62 | +# Prepare the basic sale info. |
| 63 | +sale = Sale(merchant_order_id=...) |
| 64 | + |
| 65 | +# Prepare the customer info. |
| 66 | +sale.customer = Customer(name=...) |
| 67 | +sale.customer.email = ... |
| 68 | +sale.customer.identity = ... |
| 69 | +sale.customer.identity_type = ... # 'cpf' or 'cnpj' |
| 70 | + |
| 71 | +# Prepare the customer's address. |
| 72 | +sale_address = Address() |
| 73 | +sale_address.street = ... |
| 74 | +sale_address.number = ... |
| 75 | +sale_address.complement = ... |
| 76 | +sale_address.zip_code = ... |
| 77 | +sale_address.city = ... |
| 78 | +sale_address.state = ... |
| 79 | +sale_address.country = ... |
| 80 | +sale.customer.address = sale_address |
| 81 | + |
| 82 | +# Prepare the payment info and credit card for it. |
| 83 | +# Most likely, you are not PCI compliant, so you will need to use Braspag's card tokenization. |
| 84 | +credit_card = CreditCard(security_code=None, brand=None) |
| 85 | +credit_card.card_token = '' |
| 86 | + |
| 87 | +sale.payment = Payment(amount=..., installments=...) |
| 88 | +sale.payment.soft_descriptor = ..., |
| 89 | +sale.payment.credit_card = credit_card |
| 90 | + |
| 91 | +# Split payment with subourdintate merchants. |
| 92 | +subordinate_merchant_id = ... |
| 93 | +amount = ... |
| 94 | +sale.payment.do_split = True |
| 95 | +sale.payment.split_payments = [ |
| 96 | + SplitPayment(subordinate_merchant_id, amount, mdr=..., fee=...) |
| 97 | +] |
| 98 | + |
| 99 | +# Create the payment in Cielo. |
| 100 | +sale_response = braspagSdk.payments.create_sale(sale) |
| 101 | +payment_response = sale_response['Payment'] |
| 102 | + |
| 103 | +# Save the payment reference in Dizconto's database. |
| 104 | +payment_id = payment_response['PaymentId'] |
| 105 | +payment_method = payment_response['Type'] |
| 106 | +payment_status = payment_response['Status'] |
| 107 | +payment_reason = payment_response['ReturnCode'] |
| 108 | +payment_amount = payment_response['Amount'] |
| 109 | +print(f'Created Braspag Payment: {payment_id}') |
| 110 | +``` |
| 111 | + |
| 112 | +### Creating a subordinate merchant |
| 113 | +[Official Documentation](https://braspag.github.io//manual/split-de-pagamentos-pagador) |
| 114 | + |
| 115 | +```python |
| 116 | +from braspag_sdk import Braspag, SplitMerchant, SplitMerchantAddress, SplitMerchantBank, SplitMerchantAgreement |
| 117 | + |
| 118 | +# Configuration. |
| 119 | +braspagSdk: Braspag = ... |
| 120 | +braspagSdk.add_split(...) |
| 121 | + |
| 122 | +print(f'Creating Braspag Split Subordinate Merchant...') |
| 123 | + |
| 124 | +address = SplitMerchantAddress( |
| 125 | + street=..., |
| 126 | + number=..., |
| 127 | + complement=..., |
| 128 | + neighborhood=..., |
| 129 | + zip_code=..., |
| 130 | + city=..., |
| 131 | + state=..., |
| 132 | +) |
| 133 | + |
| 134 | +bank_account = SplitMerchantBank( |
| 135 | + bank=..., |
| 136 | + bank_account_type=..., |
| 137 | + number=..., |
| 138 | + verifier_digit=..., |
| 139 | + agency_number=..., |
| 140 | + agency_digit=..., |
| 141 | + operation=..., |
| 142 | +) |
| 143 | + |
| 144 | +agreements = [ |
| 145 | + SplitMerchantAgreement( |
| 146 | + percent=..., |
| 147 | + fee=..., |
| 148 | + ) |
| 149 | +] |
| 150 | + |
| 151 | +split_merchant = SplitMerchant(master_merchant_id=...) |
| 152 | +split_merchant.fancy_name = ... |
| 153 | +split_merchant.website = ... |
| 154 | +split_merchant.mail_address = ... |
| 155 | +split_merchant.contact_name = ... |
| 156 | +split_merchant.contact_phone = ... |
| 157 | +split_merchant.corporate_name = ... |
| 158 | +split_merchant.document_number = ... |
| 159 | +split_merchant.document_type = ... |
| 160 | +split_merchant.address = address |
| 161 | +split_merchant.bank_account = bank_account |
| 162 | +split_merchant.agreements = agreements |
| 163 | + |
| 164 | +result = braspagSdk.split.create_split_merchant(split_merchant) |
| 165 | +merchant_id = result['id'] |
| 166 | +print(f'Created Braspag Split Subordinate Merchant: {merchant_id}') |
| 167 | +``` |
| 168 | + |
| 169 | +### Other |
| 170 | + |
| 171 | +```python |
| 172 | +from braspag_sdk import Braspag |
| 173 | + |
| 174 | +# Configuration. |
| 175 | +braspagSdk: Braspag = ... |
| 176 | + |
| 177 | +# Payments |
| 178 | +payment_id = ... |
| 179 | +braspagSdk.payments.get_sale(payment_id) |
| 180 | +braspagSdk.payments.capture_sale(payment_id, amount=...) |
| 181 | +braspagSdk.payments.cancel_sale(payment_id, amount=...) |
| 182 | + |
| 183 | +# Generate access token for EMV 3DS. |
| 184 | +# Learn more: https://braspag.github.io//manual/emv3ds#1.-criando-o-token-de-acesso |
| 185 | +braspagSdk.emv3ds.get_access_token() |
| 186 | + |
| 187 | +# Generate access token for Silent Order Post. |
| 188 | +# Learn more: https://braspag.github.io//manualp/braspag-silent-order-post#2.-obtendo-accesstoken-sop |
| 189 | +braspagSdk.sop.get_access_token() |
| 190 | +``` |
| 191 | + |
| 192 | +# License |
| 193 | +Licensed under the MIT license, see [`LICENSE`](LICENSE). |
0 commit comments