1
1
import grpc
2
- import io .single_use_coupons .offer_pb2 as offer_pb2
3
- import io .single_use_coupons .a_rpc_pb2_grpc as a_rpc_pb2_grpc
4
- import io .core .a_rpc_templates_pb2_grpc as a_rpc_templates_pb2_grpc
5
- import io .common .template_pb2 as template_pb2
2
+ import passkit_io .single_use_coupons .offer_pb2 as offer_pb2
3
+ import passkit_io .single_use_coupons .a_rpc_pb2_grpc as a_rpc_pb2_grpc
4
+ import passkit_io .core .a_rpc_templates_pb2_grpc as a_rpc_templates_pb2_grpc
5
+ import passkit_io .common .template_pb2 as template_pb2
6
6
import google .protobuf .timestamp_pb2 as timestamp_pb2
7
7
import datetime
8
8
9
9
10
10
def create_offer ():
11
- # Create channel credentials
11
+ # Read the CA, certificate, and private key files
12
+ with open ('../certs/ca-chain.pem' , 'rb' ) as ca_file :
13
+ root_certificates = ca_file .read ()
14
+
15
+ with open ('../certs/certificate.pem' , 'rb' ) as cert_file :
16
+ certificate_chain = cert_file .read ()
17
+
18
+ with open ('../certs/key.pem' , 'rb' ) as key_file :
19
+ private_key = key_file .read ()
20
+
21
+ # Create SSL credentials for gRPC
12
22
credentials = grpc .ssl_channel_credentials (
13
- root_certificates = 'certs/certificate.pem' , private_key_file = 'certs/key.pem' , certificate_chain_file = 'certs/ca-chain.pem' )
23
+ root_certificates = root_certificates ,
24
+ private_key = private_key ,
25
+ certificate_chain = certificate_chain
26
+ )
14
27
15
- # Create a secure channel
16
- channel = grpc .secure_channel (
17
- 'grpc.pub1.passkit.io' + ':' + '443' , credentials )
28
+ # Create a secure gRPC channel
29
+ channel = grpc .secure_channel ('grpc.pub1.passkit.io:443' , credentials )
18
30
19
31
# Create a stub
20
32
couponsStub = a_rpc_pb2_grpc .SingleUseCouponsStub (channel )
@@ -24,50 +36,56 @@ def create_offer():
24
36
25
37
# Create template
26
38
templateRequest = template_pb2 .DefaultTemplateRequest ()
27
- templateRequest .Protocol = "SINGLE_USE_COUPON"
28
- templateRequest .Revision = 1
39
+ templateRequest .protocol = "SINGLE_USE_COUPON"
40
+ templateRequest .revision = 1
29
41
30
42
template = templatesStub .getDefaultTemplate (templateRequest )
31
43
32
- template .Name = "Quickstart Base Offer"
33
- template .Description = "Quickstart Base Offer Pass"
34
- template .Timezone = "Europe/London"
44
+ template .name = "Quickstart Base Offer"
45
+ template .description = "Quickstart Base Offer Pass"
46
+ template .timezone = "Europe/London"
35
47
36
- templateId = templatesStub .createTemplate (template )
48
+ try :
49
+ response = templatesStub .createTemplate (template )
50
+ print ("Template " + response .id + " successfully created" )
51
+ except grpc .RpcError as e :
52
+ print ("Failed to create template" , e .details ())
37
53
38
54
# Create offer
39
55
offer = offer_pb2 .CouponOffer ()
40
- offer .Id = "Base Offer"
41
- offer .OfferTitle = "Base Offer Title"
42
- offer .OfferShortTitle = "Base Offer"
43
- offer .OfferDetails = "Base Offer Details"
44
- offer .OfferFinePrint = "Base Offer fine print"
45
- offer .BeforeRedeemPassTemplateId (templateId .Id )
46
-
47
- issueStartDate = datetime .datetime .strptime (
48
- "10/9/2023" , "%d/%m/%Y" ).timestamp ()
49
- issueEndDate = datetime .datetime .strptime (
50
- "10/11/2023" , "%d/%m/%Y" ).timestamp ()
51
- redemptionStartDate = datetime .datetime .strptime (
52
- "10/9/2023" , "%d/%m/%Y" ).timestamp ()
53
- redemptionEndDate = datetime .datetime .strptime (
54
- "10/11/2023" , "%d/%m/%Y" ).timestamp ()
55
-
56
+ offer .id = "Base Offer"
57
+ offer .offerTitle = "Base Offer Title"
58
+ offer .offerShortTitle = "Base Offer"
59
+ offer .offerDetails = "Base Offer Details"
60
+ offer .offerFinePrint = "Base Offer fine print"
61
+ offer .beforeRedeemPassTemplateId = response .id
62
+
63
+ # Get current UTC time as timezone-aware datetime
64
+ issueStartDate = datetime .datetime .now (datetime .timezone .utc )
65
+ issueEndDate = datetime .datetime .now (datetime .timezone .utc )
66
+ redemptionStartDate = datetime .datetime .now (datetime .timezone .utc )
67
+ redemptionEndDate = datetime .datetime .now (datetime .timezone .utc )
68
+
69
+ # Convert the datetime objects to Timestamp
56
70
redemptionSettings = offer_pb2 .RedemptionSettings ()
57
- redemptionSettings .RedemptionStartDate = timestamp_pb2 .Timestamp (
58
- redemptionStartDate )
59
- redemptionSettings .RedemptionEndDate = timestamp_pb2 .Timestamp (
60
- redemptionEndDate )
61
-
62
- couponExpirySettings = offer_pb2 .CouponExpirySettings
63
- couponExpirySettings .CouponExpiryType = "AUTO_EXPIRE_REDEMPTION_END_DATE"
64
-
65
- offer .IssueStartDate = timestamp_pb2 .Timestamp (issueStartDate )
66
- offer .IssueEndDate = timestamp_pb2 .Timestamp (issueEndDate )
67
- offer .RedemptionSettings = redemptionSettings
68
- offer .CouponExpirySettings = couponExpirySettings
69
- offer .CampaignId = "" # Get from campaignId from createCampaign call or dashboard
70
- offer .IanaTimezone = "Europe/London"
71
+ redemptionSettings .redemptionStartDate = redemptionStartDate
72
+
73
+ redemptionSettings .redemptionEndDate = redemptionEndDate
74
+
75
+ couponExpirySettings = offer_pb2 .CouponExpirySettings ()
76
+ couponExpirySettings .couponExpiryType = "AUTO_EXPIRE_REDEMPTION_END_DATE"
77
+
78
+ offer .issueStartDate = issueStartDate
79
+
80
+ offer .issueEndDate = issueEndDate
81
+
82
+ # Assign settings
83
+ offer .redemptionSettings .CopyFrom (redemptionSettings )
84
+ offer .couponExpirySettings .CopyFrom (couponExpirySettings )
85
+ offer .campaignId = "" # Get from campaignId from createCampaign call or dashboard
86
+ offer .ianaTimezone = "Europe/London"
87
+
88
+ # Create coupon offer
71
89
response = couponsStub .createCouponOffer (offer )
72
90
print (response )
73
91
0 commit comments