11import 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
66import google .protobuf .timestamp_pb2 as timestamp_pb2
77import datetime
88
99
1010def 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
1222 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+ )
1427
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 )
1830
1931 # Create a stub
2032 couponsStub = a_rpc_pb2_grpc .SingleUseCouponsStub (channel )
@@ -24,50 +36,56 @@ def create_offer():
2436
2537 # Create template
2638 templateRequest = template_pb2 .DefaultTemplateRequest ()
27- templateRequest .Protocol = "SINGLE_USE_COUPON"
28- templateRequest .Revision = 1
39+ templateRequest .protocol = "SINGLE_USE_COUPON"
40+ templateRequest .revision = 1
2941
3042 template = templatesStub .getDefaultTemplate (templateRequest )
3143
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"
3547
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 ())
3753
3854 # Create offer
3955 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
5670 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
7189 response = couponsStub .createCouponOffer (offer )
7290 print (response )
7391
0 commit comments