paypal订阅

  1. 包。
composer require paypal/rest-api-sdk-php
  1. paypal 沙箱账号。https://developer.paypal.com/developer/applications
  2. 创建升级计划,并激活
curl -v -X POST https://api.sandbox.paypal.com/v1/payments/billing-plans/ \
-H "Content-Type: application/json" \
-H "Authorization: Bearer Access-Token" \
-d '{
  "name": "The plan name,Maximum length: 128.",
  "description": "The plan description. Maximum length is 127 single-byte alphanumeric characters.",
  "type": "Allowed values: FIXED, INFINITE",
  "payment_definitions": [
    {
      "name": "The payment definition name",
      "type": "Possible values: TRIAL, REGULAR.",
      "frequency": "Possible values: WEEK, DAY, YEAR, MONTH.",
      "frequency_interval": "向客户收取费用的间隔时间。值不能大于12个月。",
      "amount": {
        "value": "100",
        "currency": "USD"
      },
      "cycles": "支付周期的数量。对于具有常规支付定义的无限计划,将周期设置为0。",
      "charge_models": [
        {
          "type": "SHIPPING",
          "amount": {
            "value": "10",
            "currency": "USD"
          }
        },
        {
          "type": "TAX",
          "amount": {
            "value": "12",
            "currency": "USD"
          }
        }
      ]
    },
    {
      "name": "Trial payment definition",
      "type": "TRIAL",
      "frequency": "WEEK",
      "frequency_interval": "5",
      "amount": {
        "value": "9.19",
        "currency": "USD"
      },
      "cycles": "2",
      "charge_models": [
        {
          "type": "SHIPPING",
          "amount": {
            "value": "1",
            "currency": "USD"
          }
        },
        {
          "type": "TAX",
          "amount": {
            "value": "2",
            "currency": "USD"
          }
        }
      ]
    }
  ],
  "merchant_preferences": {
    "setup_fee": {  协议设立费用的币种和金额。该费用是初始的、非经常性的付款金额,在账单协议创建时立即到期。可以作为初始量来触发initial_fail_amount_action。该金额的默认值是0。
      "value": "1",
      "currency": "USD"
    },
    "return_url": "The URL where the customer can approve the agreement.",
    "cancel_url": "The URL where the customer can cancel the agreement.",
    "auto_bill_amount": "指示PayPal是否在下一个账单周期自动支付未付余额。未偿余额是任何以前未偿付的预定付款的总额。Possible values: YES, NO",
    "initial_fail_amount_action": "客户的初始付款失败时的操作。值是:继续下去。协议保持活跃,失败的付款金额被添加到未付余额中。如果自动计费是启用的,贝宝自动帐单余额在下一个结算周期。取消贝宝创建协议,但将其状态设置为等待,直到初始支付清除。如果初始付款清除,待定协议就会激活。如果首次付款失败,待付款协议将被取消。Possible values: CONTINUE, CANCEL.",
    "max_fail_attempts": "允许失败的付款尝试的最大次数。默认值0定义了无限次失败的支付尝试。"
  }
}'

{
  "id": "P-7DC96732KA7763723UOPKETA", //The ID of the plan
  "state": "CREATED",   //The plan status.Possible values: CREATED, ACTIVE, INACTIVE.
  "name": "Plan with Regular and Trial Payment Definitions",    //The plan name.
  "description": "Plan with regular and trial payment definitions.",   //The plan description. Maximum length is 127 single-byte alphanumeric characters.
  "type": "FIXED",   //The plan type. Indicates whether the payment definitions in the plan have a fixed number of or infinite payment cycles. Value is:FIXED, INFINITE
  "payment_definitions": [
    {
      "id": "PD-0MF87809KK310750TUOPKETA",  //The ID of the payment definition.
      "name": "Regular payment definition",   //The payment definition name.
      "type": "REGULAR",   //Possible values: TRIAL, REGULAR.
      "frequency": "MONTH",
      "amount": {
        "currency": "USD",
        "value": "100"
      },
      "charge_models": [
        {
          "id": "CHM-89H01708244053321UOPKETA",
          "type": "SHIPPING",
          "amount": {
            "currency": "USD",
            "value": "10"
          }
        },
        {
          "id": "CHM-1V202179WT9709019UOPKETA",
          "type": "TAX",
          "amount": {
            "currency": "USD",
            "value": "12"
          }
        }
      ],
      "cycles": "12",
      "frequency_interval": "2"
    },
    {
      "id": "PD-03223056L66578712UOPKETA",
      "name": "Trial payment definition",
      "type": "TRIAL",
      "frequency": "WEEK",
      "amount": {
        "currency": "USD",
        "value": "9.19"
      },
      "charge_models": [
        {
          "id": "CHM-7XN63093LF858372XUOPKETA",
          "type": "SHIPPING",
          "amount": {
            "currency": "USD",
            "value": "1"
          }
        },
        {
          "id": "CHM-6JY06508UT8026625UOPKETA",
          "type": "TAX",
          "amount": {
            "currency": "USD",
            "value": "2"
          }
        }
      ],
      "cycles": "2",
      "frequency_interval": "5"
    }
  ],
  "merchant_preferences": {
    "setup_fee": {
      "currency": "USD",
      "value": "1"
    },
    "max_fail_attempts": "0",
    "return_url": "https://example.com",
    "cancel_url": "https://example.com/cancel",
    "auto_bill_amount": "YES",
    "initial_fail_amount_action": "CONTINUE"
  },
  "create_time": "2017-06-16T07:40:20.940Z",
  "update_time": "2017-06-16T07:40:20.940Z",
  "links": [
    {
      "href": "https://api.sandbox.paypal.com/v1/payments/billing-plans/P-7DC96732KA7763723UOPKETA",
      "rel": "self",
      "method": "GET"
    }
  ]
}
  1. 创建订阅(创建Agreement),然后将跳转到Paypal的网站等待用户同意;
curl -v -X POST https://api.sandbox.paypal.com/v1/payments/billing-agreements/ \
-H "Content-Type: application/json" \
-H "Authorization: Bearer Access-Token" \
-d '{
  "name": "The agreement name",  
  "description": "The agreement description.",
  "start_date": "2017-12-22T09:13:49Z",   //本协议开始的日期和时间,以互联网日期和时间格式。开始日期必须在当前日期之后不少于24小时,因为协议可能需要24小时才能激活。
  "payer": {
    "payment_method": "paypal",   //Possible values: bank, paypal.
    "payer_info": {
      "email": "payer@example.com"
    }
  },
  "plan": {
    "id": "P-1WJ68935LL406420PUTENA2I"  //The ID of the plan on which this agreement is based.
  },
  "shipping_address": {
    "line1": "Hotel Staybridge",
    "line2": "Crooke Street",
    "city": "San Jose",
    "state": "CA",
    "postal_code": "95112",
    "country_code": "US"
  },
  "override_merchant_preferences": {
    "setup_fee": {
      "value": "3",
      "currency": "GBP"
    },
    "return_url": "https://example.com/",
    "cancel_url": "https://example.com/cancel",
    "auto_bill_amount": "YES",
    "initial_fail_amount_action": "CONTINUE",
    "max_fail_attempts": "11"
  },
  "override_charge_models": [
    {
      "charge_id": "CHM-8373958130821962WUTENA2Q",
      "amount": {
        "value": "1",
        "currency": "GBP"
      }
    }
  ]
}'

{
  "name": "Override Agreement",
  "description": "Agreement that overrides merchant preferences and charge models",
  "payer": {
    "payment_method": "paypal",
    "payer_info": {
      "email": "payer@example.com"
    }
  },
  "plan": {
    "id": "P-1WJ68935LL406420PUTENA2I",
    "state": "ACTIVE",
    "name": "Fast Speed Plan",
    "description": "Vanilla plan",
    "type": "INFINITE",
    "payment_definitions": [
      {
        "id": "PD-9WG6983719571780GUTENA2I",
        "name": "Regular payment definition",
        "type": "REGULAR",
        "frequency": "DAY",
        "amount": {
          "currency": "GBP",
          "value": "10"
        },
        "charge_models": [
          {
            "id": "CHM-8373958130821962WUTENA2Q",
            "type": "SHIPPING",
            "amount": {
              "currency": "GBP",
              "value": "1"
            }
          },
          {
            "id": "CHM-2937144979861454NUTENA2Q",
            "type": "TAX",
            "amount": {
              "currency": "GBP",
              "value": "2"
            }
          }
        ],
        "cycles": "0",
        "frequency_interval": "1"
      },
      {
        "id": "PD-89M493313S710490TUTENA2Q",
        "name": "Trial payment definition",
        "type": "TRIAL",
        "frequency": "MONTH",
        "amount": {
          "currency": "GBP",
          "value": "100"
        },
        "charge_models": [
          {
            "id": "CHM-78K47820SS4923826UTENA2Q",
            "type": "SHIPPING",
            "amount": {
              "currency": "GBP",
              "value": "10"
            }
          },
          {
            "id": "CHM-9M366179U7339472RUTENA2Q",
            "type": "TAX",
            "amount": {
              "currency": "GBP",
              "value": "12"
            }
          }
        ],
        "cycles": "5",
        "frequency_interval": "2"
      }
    ],
    "merchant_preferences": {
      "setup_fee": {
        "currency": "GBP",
        "value": "3"
      },
      "max_fail_attempts": "11",
      "return_url": "https://example.com/",
      "cancel_url": "https://example.com/cancel",
      "auto_bill_amount": "YES",
      "initial_fail_amount_action": "CONTINUE"
    }
  },
  "links": [
    {
      "href": "https://api.sandbox.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token=EC-83C745436S346813F",
      "rel": "approval_url",
      "method": "REDIRECT"
    },
    {
      "href": "https://api.sandbox.paypal.com/v1/payments/billing-agreements/EC-83C745436S346813F/agreement-execute",
      "rel": "execute",
      "method": "POST"
    }
  ],
  "start_date": "2017-12-22T09:13:49Z"
}
  1. 用户同意后,执行订阅
curl -v -X POST https://api.sandbox.paypal.com/v1/payments/billing-agreements/EC-7WN97463LN263864T/agreement-execute \
-H "Content-Type: application/json" \
-H "Authorization: Bearer Access-Token"

{
  "id": "I-1TJ3GAGG82Y9",
  "state": "Active",
  "description": "Monthly agreement with free trial payment definition.",
  "payer": {
    "payment_method": "paypal",
    "status": "unverified",
    "payer_info": {
      "email": "johndoe@example.com",
      "first_name": "John",
      "last_name": "Doe",
      "payer_id": "NEW8A85AK4ET4",
      "shipping_address": {
        "recipient_name": "John Doe",
        "line1": "751235 Stout Drive",
        "line2": "0976249 Elizabeth Court",
        "city": "Quimby",
        "state": "IA",
        "postal_code": "51049",
        "country_code": "US"
      }
    }
  },
  "plan": {
    "name": "Plan with Regular and Trial Payment Definitions",
    "description": "Plan with regular and trial payment definitions.",
    "type": "FIXED",
    "payment_definitions": [
      {
        "name": "Trial payment definition",
        "type": "TRIAL",
        "frequency": "MONTH",
        "amount": {
          "value": "0.00",
          "currency": "USD"
        },
        "cycles": "2",
        "charge_models": [
          {
            "type": "TAX",
            "amount": {
              "value": "0.00",
              "currency": "USD"
            }
          },
          {
            "type": "SHIPPING",
            "amount": {
              "value": "0.00",
              "currency": "USD"
            }
          }
        ],
        "frequency_interval": "1"
      },
      {
        "name": "Regular payment definition",
        "type": "REGULAR",
        "frequency": "MONTH",
        "amount": {
          "value": "5.99",
          "currency": "USD"
        },
        "cycles": "10",
        "charge_models": [
          {
            "type": "TAX",
            "amount": {
              "value": "0.29",
              "currency": "USD"
            }
          },
          {
            "type": "SHIPPING",
            "amount": {
              "value": "0.20",
              "currency": "USD"
            }
          }
        ],
        "frequency_interval": "1"
      }
    ],
    "merchant_preferences": {
      "setup_fee": {
        "value": "0.40",
        "currency": "USD"
      },
      "return_url": "https://example.com",
      "cancel_url": "https://example.com/cancel",
      "max_fail_attempts": "2",
      "auto_bill_amount": "YES"
    },
    "links": [],
    "currency_code": "USD"
  },
  "start_date": "2016-12-23T08:00:00Z",
  "shipping_address": {
    "recipient_name": "John Doe",
    "line1": "751235 Stout Drive",
    "line2": "0976249 Elizabeth Court",
    "city": "Quimby",
    "state": "IA",
    "postal_code": "51049",
    "country_code": "US"
  },
  "agreement_details": {
    "outstanding_balance": {
      "value": "0.00",
      "currency": "USD"
    },
    "cycles_remaining": "2",
    "cycles_completed": "0",
    "next_billing_date": "2017-01-23T08:00:00Z",
    "last_payment_date": "2016-12-23T08:00:00Z",
    "last_payment_amount": {
      "value": "0.40",
      "currency": "USD"
    },
    "final_payment_date": "2017-09-23T08:00:00Z",
    "failed_payment_count": "0"
  },
  "links": [
    {
      "href": "https://api.sandbox.paypal.com/v1/payments/billing-agreements/I-1TJ3GAGG82Y9",
      "rel": "self",
      "method": "GET"
    }
  ]
}


  1. 获取扣款帐单
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值