createSubscription method

Future<Subscription> createSubscription(
  1. SubscriptionRequest request, {
  2. String? payPalRequestId,
  3. Prefer? prefer,
})

Creates a subscription.

Parameter request: The create subscription request object

Parameter paypalRequestId: The server stores keys for 72 hours.

Parameter prefer: 'minimal', The server returns a minimal response to optimize communication between the API caller and the server. A minimal response includes the id, status and HATEOAS links. 'representation', The server returns a complete resource representation, including the current state of the resource.

Implementation

Future<Subscription> createSubscription(
  SubscriptionRequest request, {
  String? payPalRequestId,
  Prefer? prefer,
}) async {
  var url = _payPalHttpClient.getUrl('/v1/billing/subscriptions');

  Map<String, String> headers = {};

  if (prefer != null) {
    headers['Prefer'] = preferTypeEnumMap[prefer]!;
  }

  if (payPalRequestId != null) {
    headers['PayPal-Request-Id'] = payPalRequestId;
  }

  var body = jsonEncode(request.toJson());

  var response =
      await _payPalHttpClient.post(url, headers: headers, body: body);
  return Subscription.fromJson(jsonDecode(response.body));
}