-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathcreate_order_automatic_payment.py
More file actions
139 lines (126 loc) · 5.2 KB
/
Copy pathcreate_order_automatic_payment.py
File metadata and controls
139 lines (126 loc) · 5.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
"""Example: Automatic Payments flow with the MercadoPago Orders API.
Demonstrates both the first payment (storing the card credential) and a
subsequent recurring charge (referencing the previous transaction).
"""
import dataclasses
import mercadopago
from mercadopago.resources.order_automatic_payments import OrderAutomaticPayments
from mercadopago.resources.order_stored_credential import OrderStoredCredential
from mercadopago.resources.order_subscription_data import (
OrderInvoicePeriod,
OrderSubscriptionData,
OrderSubscriptionSequence,
)
sdk = mercadopago.SDK("YOUR_ACCESS_TOKEN")
# ── First payment ──────────────────────────────────────────────────────────
# Use first_payment=True and no prev_transaction_ref.
# The API stores the credential for future charges.
first_payment_order = {
"type": "online",
"total_amount": "100.00",
"external_reference": "subscription-001-payment-1",
"payer": {
"email": "customer@example.com",
"customer_id": "CUSTOMER_ID",
},
"transactions": {
"payments": [
{
"amount": "100.00",
"payment_method": {
"id": "master",
"type": "credit_card",
"token": "CARD_TOKEN",
"installments": 1,
},
"automatic_payments": dataclasses.asdict(
OrderAutomaticPayments(
payment_profile_id="PAYMENT_PROFILE_ID",
schedule_date="2026-07-01T00:00:00.000-04:00",
due_date="2026-07-05T00:00:00.000-04:00",
retries=3,
)
),
"stored_credential": dataclasses.asdict(
OrderStoredCredential(
payment_initiator="merchant",
reason="recurring",
store_payment_method=True,
first_payment=True,
# prev_transaction_ref not required on first payment
)
),
"subscription_data": dataclasses.asdict(
OrderSubscriptionData(
invoice_id="INVOICE_001",
billing_date="2026-06-01",
subscription_sequence=OrderSubscriptionSequence(number=1, total=12),
invoice_period=OrderInvoicePeriod(type="monthly", period=1),
)
),
}
]
},
}
result = sdk.order().create(first_payment_order)
first_order = result["response"]
print("First payment order:", first_order.get("id"), "→", first_order.get("status"))
# Save the transaction ID for use in the next charge.
first_transaction_id = (
first_order.get("transactions", {})
.get("payments", [{}])[0]
.get("id", "")
)
# ── Subsequent recurring charge ────────────────────────────────────────────
# Use first_payment=False and include prev_transaction_ref pointing to the
# previous charge. The API links this payment to the original authorization.
recurring_order = {
"type": "online",
"total_amount": "100.00",
"external_reference": "subscription-001-payment-2",
"payer": {
"email": "customer@example.com",
"customer_id": "CUSTOMER_ID",
},
"transactions": {
"payments": [
{
"amount": "100.00",
"payment_method": {
"id": "master",
"type": "credit_card",
"token": "CARD_TOKEN",
"installments": 1,
},
"automatic_payments": dataclasses.asdict(
OrderAutomaticPayments(
payment_profile_id="PAYMENT_PROFILE_ID",
schedule_date="2026-08-01T00:00:00.000-04:00",
due_date="2026-08-05T00:00:00.000-04:00",
retries=3,
)
),
"stored_credential": dataclasses.asdict(
OrderStoredCredential(
payment_initiator="merchant",
reason="recurring",
store_payment_method=False,
first_payment=False,
previous_transaction_reference=first_transaction_id, # required
)
),
"subscription_data": dataclasses.asdict(
OrderSubscriptionData(
invoice_id="INVOICE_002",
billing_date="2026-07-01",
subscription_sequence=OrderSubscriptionSequence(number=2, total=12),
invoice_period=OrderInvoicePeriod(type="monthly", period=1),
)
),
}
]
},
}
result = sdk.order().create(recurring_order)
recurring = result["response"]
print("Recurring charge order:", recurring.get("id"), "→", recurring.get("status"))