Skip to content

Commit 8089070

Browse files
Merge pull request #131 from starkbank/feature/merchant-purchase
Add merchant session and merchant purchase
2 parents d21c221 + e4873df commit 8089070

34 files changed

+1394
-1
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ Given a version number MAJOR.MINOR.PATCH, increment:
1313

1414

1515
## [Unreleased]
16+
### Added
17+
- MerchantSession and MerchantSessionPurchase resources
1618

1719
## [2.26.1] - 2025-02-18
1820
### Fixed

README.md

+208
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ is as easy as sending a text message to your client!
4949
- [CorporateBalance](#get-your-corporatebalance): View your corporate balance
5050
- [CorporateTransactions](#query-corporatetransactions): View the transactions that have affected your corporate balance
5151
- [CorporateEnums](#corporate-enums): Query enums related to the corporate purchases, such as merchant categories, countries and card purchase methods
52+
- [MerchantCard](#query-merchantcards): Stores information about approved purchase cards for reuse
53+
- [MerchantSession](#create-a-merchantsession): Manages a session to create a purchase with a new card
54+
- [MerchantPurchase](#create-a-merchantpurchase): Allows a merchant to charge their customers using debit or credit cards
55+
- [MerchantInstallment](#query-merchantinstallments): Tracks the lifecycle of purchase installments
5256
- [Split](#query-splits): Split received Invoice payments between different receivers
5357
- [SplitReceiver](#create-splitreceivers): Receiver of an Invoice split
5458
- [Webhooks](#create-a-webhook-subscription): Configure your webhook endpoints and subscriptions
@@ -2333,6 +2337,210 @@ log = starkbank.splitreceiver.log.get("5155165527080960")
23332337
print(log)
23342338
```
23352339

2340+
## Query MerchantCards
2341+
2342+
Get a list of merchant cards in chunks of at most 100. If you need smaller chunks, use the limit parameter.
2343+
2344+
```python
2345+
import starkbank
2346+
2347+
merchant_cards = starkbank.merchantcard.query(limit=3)
2348+
for merchant_card in merchant_cards:
2349+
print(merchant_card)
2350+
```
2351+
2352+
## Get a MerchantCard
2353+
2354+
Retrieve detailed information about a specific card by its id.
2355+
2356+
```python
2357+
import starkbank
2358+
2359+
merchantcard = starkbank.merchantcard.get('5950134772826112')
2360+
print(merchantcard)
2361+
```
2362+
2363+
## Create a MerchantSession
2364+
2365+
The Merchant Session allows you to create a session prior to a purchase.
2366+
Sessions are essential for defining the parameters of a purchase, including funding type, expiration, 3DS, and more.
2367+
2368+
```python
2369+
import starkbank
2370+
2371+
merchant_session = starkbank.merchantsession.create({
2372+
"allowedFundingTypes": [
2373+
"debit",
2374+
"credit"
2375+
],
2376+
"allowedInstallments": [
2377+
{
2378+
"totalAmount": 0,
2379+
"count": 1
2380+
},
2381+
{
2382+
"totalAmount": 12000,
2383+
"count": 2
2384+
},
2385+
{
2386+
"totalAmount": 18000,
2387+
"count": 12
2388+
}
2389+
],
2390+
"expiration": 3600,
2391+
"challengeMode": "disabled",
2392+
"tags": [
2393+
"your-tags"
2394+
]
2395+
})
2396+
2397+
print(merchant_session)
2398+
```
2399+
2400+
You can create a MerchantPurchase through a MerchantSession by passing its UUID.
2401+
**Note**: This method must be implemented in your front-end to ensure that sensitive card data does not pass through the back-end of the integration.
2402+
2403+
## Create a MerchantSession Purchase
2404+
2405+
This route can be used to create a Merchant Purchase directly from the payer's client application.
2406+
The UUID of a Merchant Session that was previously created by the merchant is necessary to access this route.
2407+
2408+
```python
2409+
import starkbank
2410+
2411+
merchant_session_purchase = starkbank.merchantsession.purchase(
2412+
uuid="0bb894a2697d41d99fe02cad2c00c9bc",
2413+
amount=18000,
2414+
installment_count=12,
2415+
card_expiration="2035-01",
2416+
card_number="5448280000000007",
2417+
card_security_code="123",
2418+
holder_name="Margaery Tyrell",
2419+
holder_email="[email protected]",
2420+
holder_phone="11998663456",
2421+
funding_type="credit",
2422+
billing_country_code="BRA",
2423+
billing_city="São Paulo",
2424+
billing_state_code="SP",
2425+
billing_street_line1="Rua do Jardim de cima, 123",
2426+
billing_street_line2="1 andar",
2427+
billing_zip_code="11111-111",
2428+
metadata={
2429+
"extraData": "extraData",
2430+
"language": "pt-BR",
2431+
"timezoneOffset": 3,
2432+
"userAgent": "Mozilla",
2433+
"userIp": "255.255.255.255"
2434+
}
2435+
)
2436+
2437+
print(merchant_session_purchase)
2438+
```
2439+
2440+
## Query MerchantSessions
2441+
2442+
Get a list of merchant sessions in chunks of at most 100. If you need smaller chunks, use the limit parameter.
2443+
2444+
```python
2445+
import starkbank
2446+
2447+
merchant_sessions = starkbank.merchantsession.query(limit=3)
2448+
for merchant_session in merchant_sessions:
2449+
print(merchant_session)
2450+
```
2451+
2452+
## Get a MerchantSession
2453+
2454+
Retrieve detailed information about a specific session by its id.
2455+
2456+
```python
2457+
import starkbank
2458+
2459+
merchant_session = starkbank.merchantsession.get('5950134772826112')
2460+
print(merchant_session)
2461+
```
2462+
2463+
## Create a MerchantPurchase
2464+
2465+
The Merchant Purchase resource can be used to charge customers with credit or debit cards.
2466+
If a card hasn't been used before, a Merchant Session Purchase must be created and approved with that specific card before it can be used directly in a Merchant Purchase.
2467+
2468+
```python
2469+
import starkbank
2470+
2471+
merchant_purchase = starkbank.merchantpurchase.create(
2472+
starkbank.MerchantPurchase(
2473+
amount=10000,
2474+
installment_count=5,
2475+
card_id="6295415968235520",
2476+
funding_type="credit",
2477+
challenge_mode="disabled",
2478+
billing_city="Sao Paulo",
2479+
billing_country_code="BRA",
2480+
billing_state_code="SP",
2481+
billing_street_line_1="Rua Casterly Rock, 2000",
2482+
billing_street_line_2="1 andar",
2483+
billing_zip_code="11111-111",
2484+
holder_email="[email protected]",
2485+
holder_phone="11985923451",
2486+
metadata={
2487+
"userAgent": "userAgent",
2488+
"userIp": "255.255.255.255",
2489+
"language": "pt-BR",
2490+
"timezoneOffset": 3,
2491+
"extraData": "extraData"
2492+
},
2493+
tags=["teste"]
2494+
)
2495+
)
2496+
```
2497+
2498+
## Query MerchantPurchases
2499+
2500+
Get a list of merchant purchases in chunks of at most 100. If you need smaller chunks, use the limit parameter.
2501+
2502+
```python
2503+
import starkbank
2504+
2505+
merchant_purchases = starkbank.merchantpurchase.query(limit=3)
2506+
for merchant_purchase in merchant_purchases:
2507+
print(merchant_purchase)
2508+
```
2509+
2510+
## Get a MerchantPurchase
2511+
2512+
Retrieve detailed information about a specific purchase by its id.
2513+
2514+
```python
2515+
import starkbank
2516+
2517+
merchant_purchase = starkbank.merchantpurchase.get('5950134772826112')
2518+
print(merchant_purchase)
2519+
```
2520+
2521+
## Query MerchantInstallments
2522+
2523+
Get a list of merchant installments in chunks of at most 100. If you need smaller chunks, use the limit parameter.
2524+
2525+
```python
2526+
import starkbank
2527+
2528+
merchant_installments = starkbank.merchantinstallment.query(limit=3)
2529+
for merchant_installment in merchant_installments:
2530+
print(merchant_installment)
2531+
```
2532+
2533+
## Get a MerchantInstallment
2534+
2535+
Retrieve detailed information about a specific installment by its id.
2536+
2537+
```python
2538+
import starkbank
2539+
2540+
merchant_installment = starkbank.merchantinstallment.get('5950134772826112')
2541+
print(merchant_installment)
2542+
```
2543+
23362544
## Create a webhook subscription
23372545

23382546
To create a webhook subscription and be notified whenever an event occurs, run:

starkbank/__init__.py

+12
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,24 @@
5858
from . import corporaterule
5959
from .corporaterule.__corporaterule import CorporateRule
6060

61+
from . import merchantcard
62+
from .merchantcard.__merchantcard import MerchantCard
63+
6164
from . import merchantcategory
6265
from .merchantcategory.__merchantcategory import MerchantCategory
6366

6467
from . import merchantcountry
6568
from .merchantcountry.__merchantcountry import MerchantCountry
6669

70+
from . import merchantinstallment
71+
from .merchantinstallment.__merchantinstallment import MerchantInstallment
72+
73+
from . import merchantpurchase
74+
from .merchantpurchase.__merchantpurchase import MerchantPurchase
75+
76+
from . import merchantsession
77+
from .merchantsession.__merchantsession import MerchantSession
78+
6779
from . import cardmethod
6880
from .cardmethod.__cardmethod import CardMethod
6981

starkbank/merchantcard/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .__merchantcard import get, query, page
2+
from .log.__log import Log
3+
from . import log
+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
from ..utils import rest
2+
from starkcore.utils.resource import Resource
3+
from starkcore.utils.checks import check_date, check_datetime, check_datetime_or_date
4+
5+
class MerchantCard(Resource):
6+
"""# MerchantCard object
7+
Check out our API Documentation at https://starkbank.com/docs/api#merchant-card
8+
"""
9+
10+
def __init__(self, id=None, ending=None , funding_type=None, holder_name=None, network=None, status=None, tags=None,
11+
expiration=None, created=None, updated=None):
12+
Resource.__init__(self, id=id)
13+
self.ending = ending
14+
self.funding_type = funding_type
15+
self.holder_name = holder_name
16+
self.network = network
17+
self.status = status
18+
self.tags = tags
19+
self.expiration = check_datetime_or_date(expiration)
20+
self.created = check_datetime(created)
21+
self.updated = check_datetime(updated)
22+
23+
24+
_resource = {"class": MerchantCard, "name": "MerchantCard"}
25+
26+
27+
def get(id, user=None):
28+
return rest.get_id(resource=_resource, id=id, user=user)
29+
30+
31+
def query(limit=None, after=None, before=None, status=None, tags=None, ids=None, user=None):
32+
return rest.get_stream(
33+
resource=_resource,
34+
limit=limit,
35+
after=check_date(after),
36+
before=check_date(before),
37+
status=status,
38+
tags=tags,
39+
ids=ids,
40+
user=user,
41+
)
42+
43+
44+
def page(cursor=None, limit=None, after=None, before=None, status=None, tags=None, ids=None, user=None):
45+
return rest.get_page(
46+
resource=_resource,
47+
cursor=cursor,
48+
limit=limit,
49+
after=check_date(after),
50+
before=check_date(before),
51+
status=status,
52+
tags=tags,
53+
ids=ids,
54+
user=user,
55+
)
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from .__log import query, page, get

starkbank/merchantcard/log/__log.py

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
from ...utils import rest
2+
from starkcore.utils.resource import Resource
3+
from starkcore.utils.api import from_api_json
4+
from starkcore.utils.checks import check_date, check_datetime
5+
from ..__merchantcard import _resource as _merchant_card_resource
6+
7+
8+
class Log(Resource):
9+
"""# merchantcard.Log object
10+
Check out our API Documentation at https://starkbank.com/docs/api#merchant-card
11+
"""
12+
13+
def __init__(self, id, created, updated, type, errors, card):
14+
Resource.__init__(self, id=id)
15+
self.created = check_datetime(created)
16+
self.updated = check_datetime(updated)
17+
self.type = type
18+
self.errors = errors
19+
self.card = from_api_json(_merchant_card_resource, card)
20+
21+
22+
_resource = {"class": Log, "name": "MerchantCardLog"}
23+
24+
25+
def get(id, user=None):
26+
return rest.get_id(resource=_resource, id=id, user=user)
27+
28+
29+
def query(limit=None, status=None, tags=None, ids=None, after=None, before=None, user=None):
30+
return rest.get_stream(
31+
resource=_resource,
32+
limit=limit,
33+
after=check_date(after),
34+
before=check_date(before),
35+
status=status,
36+
tags=tags,
37+
ids=ids,
38+
user=user,
39+
)
40+
41+
42+
def page(cursor=None, limit=None, after=None, before=None, types=None, ids=None, user=None):
43+
return rest.get_page(
44+
resource=_resource,
45+
cursor=cursor,
46+
limit=limit,
47+
after=check_date(after),
48+
before=check_date(before),
49+
types=types,
50+
ids=ids,
51+
user=user,
52+
)
53+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .__merchantinstallment import get, query, page
2+
from .log.__log import Log
3+
from . import log

0 commit comments

Comments
 (0)