首先,你需要創建一個計費方案:
billing_plan_attributes = {
"name": PLAN_NAME_HERE,
"description": PLAN_DESCRIPTION,
"merchant_preferences": {
"auto_bill_amount": "yes", # yes if you want auto bill
"cancel_url": "http://www.cancel.com", # redirect uri if user cancels payment
"initial_fail_amount_action": "continue",
"max_fail_attempts": "1",
"return_url": RETURN_URL,
"setup_fee": {
"currency": CURRENCY,
"value": VALUE # how much do you want to charge
}
},
"payment_definitions": [
{
"amount": {
"currency": request.form['currency'],
"value": request.form['amount']
},
"cycles": CYCLES, # how much time this subscription will charge user
"frequency": FREQ, # month, day
"frequency_interval": INTERVAL, # per month or per three month or so on
"name": NAME,
"type": TYPE
}
],
"type": TYPE
}
billing_plan = BillingPlan(billing_plan_attributes)
if billing_plan.create():
print("success")
使用的屬性在這裏有字面意義。現在,您已經創建了一個賬單計劃,您需要爲用戶提供一些界面,以便他們可以訂閱。以下是此示例代碼:
billing_agreement = BillingAgreement({
"name": "Organization plan name",
"description": "Agreement for " + request.args.get('name', ''),
"start_date": (datetime.now() + timedelta(hours=1)).strftime('%Y-%m-%dT%H:%M:%SZ'),
"plan": {
"id": request.args.get('id', '')
},
"payer": {
"payment_method": "paypal"
},
"shipping_address": {
"line1": "StayBr111idge Suites",
"line2": "Cro12ok Street",
"city": "San Jose",
"state": "CA",
"postal_code": "95112",
"country_code": "US"
}
})
if billing_agreement.create():
for link in billing_agreement.links:
if link.rel == "approval_url":
approval_url = link.href
在最後一行中,您將獲得可以提供給用戶的審批鏈接。 接下來,如果用戶批准付款,您必須設置一個終端,它將成爲回撥網址。
billing_agreement_response = BillingAgreement.execute(payment_token)
payment_token通過PayPal發送到您的回調網址。
客戶端集成不適用於訂閱目的。您必須使用服務器端實現才能創建訂閱計劃。而服務器端也是有道理的,因爲你總是希望存儲的細節,你已經收到了多少錢,或多少到期和更多的細節 –
只是FYI:你提供的鏈接有服務器端實現。 –
你是對的。但我無法弄清楚如何更改[服務器端請求]中使用的JSON(https://developer.paypal.com/docs/integration/direct/express-checkout/integration-jsv4/advanced-payments-api/create -express-checkout-payments /#request),以便創建一個訂閱。 – Cijo