Skip to content

Commit e4ea32e

Browse files
committed
More explicit about apple payment session request
- fixed the certkeypath - Added the request JSON (optional) to specify the URL (domain) - Added minor details about the opaque response
1 parent 6f14061 commit e4ea32e

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

donation-api/Dockerfile

+2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ ENV APPLEPAY_DISPLAYNAME=
4040
ENV APPLEPAY_PAYMENT_SESSION_INITIATIVE=
4141
ENV APPLEPAY_PAYMENT_SESSION_INITIATIVE_CONTEXT=
4242
# ENV APPLEPAY_PAYMENT_SESSION_REQ_TIMEOUT_SEC=5
43+
ENV APPLEPAY_MERCHANT_CERTIFICATE=
44+
ENV APPLEPAY_MERCHANT_CERTIFICATE_KEY=
4345
ENV APPLEPAY_MERCHANT_CERTIFICATE_PATH=/etc/ssl/certs/applepay_merchant.pem
4446
ENV APPLEPAY_MERCHANT_CERTIFICATE_KEY_PATH=/etc/ssl/certs/applepay_merchant.key
4547

donation-api/src/donation_api/constants.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def __post_init__(self):
7676
self.applepay_merchant_certificate_path = pathlib.Path(certpath)
7777
certkeypath = os.getenv("APPLEPAY_MERCHANT_CERTIFICATE_KEY_PATH") or ""
7878
if certkeypath:
79-
self.applepay_merchant_certificate_key_path = pathlib.Path(certpath)
79+
self.applepay_merchant_certificate_key_path = pathlib.Path(certkeypath)
8080

8181
@property
8282
def stripe_secret_api_key(self) -> str:

donation-api/src/donation_api/stripe.py

+26-2
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,17 @@ class StripeWebhookResponse(BaseModel):
6161
status: str
6262

6363

64+
class ApplePayPaymentSessionRequest(BaseModel):
65+
# defaulting to test gateway
66+
validation_url: str = "apple-pay-gateway-cert.apple.com"
67+
68+
6469
class OpaqueApplePayPaymentSession(BaseModel):
6570
model_config = ConfigDict(extra="allow")
6671

72+
initiative: str
73+
initiativeContext: str
74+
6775

6876
async def get_body(request: Request):
6977
"""raw request body"""
@@ -285,7 +293,23 @@ def webhook_received(
285293
},
286294
status_code=HTTPStatus.OK,
287295
)
288-
async def create_payment_session():
296+
async def create_payment_session(ps_payload: ApplePayPaymentSessionRequest):
297+
allowed_domains: list[str] = [
298+
# Global
299+
"apple-pay-gateway.apple.com",
300+
# China
301+
"cn-apple-pay-gateway.apple.com",
302+
# Testing (Global)
303+
"apple-pay-gateway-cert.apple.com",
304+
# Testing (China)
305+
"cn-apple-pay-gateway-cert.apple.com",
306+
]
307+
if ps_payload.validation_url not in allowed_domains:
308+
raise HTTPException(
309+
status_code=HTTPStatus.FORBIDDEN,
310+
detail="Validation URL is not in Apple's whitelist",
311+
)
312+
289313
payload = {
290314
"merchantIdentifier": conf.applepay_merchant_identifier,
291315
"displayName": conf.applepay_displayname,
@@ -295,7 +319,7 @@ async def create_payment_session():
295319

296320
data: dict[str, Any] = {}
297321
resp = requests.post(
298-
url="https://apple-pay-gateway.apple.com/paymentservices/paymentSession",
322+
url=f"https://{ps_payload.validation_url}/paymentservices/paymentSession",
299323
cert=(
300324
str(conf.applepay_merchant_certificate_path),
301325
str(conf.applepay_merchant_certificate_key_path),

0 commit comments

Comments
 (0)