Beispiel #1
0
def update_user_data(env, pledge_type, pledge_time):
    """ Use a deferred task to batch update stripe user data """

    PAGE_SIZE = 500

    # Get the next PAGE_SIZE pledges
    query = getattr(model, pledge_type).all().order('-donationTime')
    if pledge_time:
        # Filter instead of using 'offset' because offset is very inefficient,
        # according to https://developers.google.com/appengine/articles/paging
        query = query.filter('donationTime <= ', pledge_time)
    pledges = query.fetch(PAGE_SIZE + 1)
    next_pledge_time = None
    if len(pledges) == PAGE_SIZE + 1:
        next_pledge_time = pledges[-1].donationTime
    pledges = pledges[:PAGE_SIZE]

    # Loop through the current pledges and update the associated user with data
    # pulled from Stripe or Paypal
    for pledge in pledges:
        try:
            user = model.User.all().filter('email =', pledge.email).get()
            if user.zipCode and user.address and user.address != 'None':
                continue
            if hasattr(pledge,
                       'paypalTransactionID') and pledge.paypalTransactionID:
                request_data = {
                    'METHOD': 'GetTransactionDetails',
                    'TRANSACTIONID': pledge.paypalTransactionID
                }
                rc, txn_data = paypal.send_request(request_data)
                if not rc:
                    logging.warning('Error retrieving PayPal transaction: %s',
                                    txn_data)
                    continue
                user.zipCode = txn_data['SHIPTOZIP'][0]
                user.address = txn_data['SHIPTOSTREET'][0]
                if 'SHIPTOSTREET2' in txn_data:
                    user.address += ', %s' % txn_data['SHIPTOSTREET2'][0]
                user.city = txn_data['SHIPTOCITY'][0]
                user.state = txn_data['SHIPTOSTATE'][0]
            elif pledge.stripeCustomer:
                card_data = env.stripe_backend.RetrieveCardData(
                    pledge.stripeCustomer)
                user.zipCode = card_data['address_zip']
                address = card_data['address_line1']
                if card_data['address_line2']:
                    address += ', %s' % card_data['address_line2']
                user.address = address
                user.city = card_data['address_city']
                user.state = card_data['address_state']
            user.put()
        except Exception, e:
            logging.warning('Error updating user %s with error, %s',
                            user.email, e)
Beispiel #2
0
def update_user_data(env, pledge_type, pledge_time):
  """ Use a deferred task to batch update stripe user data """

  PAGE_SIZE = 500

  # Get the next PAGE_SIZE pledges
  query = getattr(model, pledge_type).all().order('-donationTime')
  if pledge_time:
    # Filter instead of using 'offset' because offset is very inefficient,
    # according to https://developers.google.com/appengine/articles/paging
    query = query.filter('donationTime <= ', pledge_time)
  pledges = query.fetch(PAGE_SIZE + 1)
  next_pledge_time = None
  if len(pledges) == PAGE_SIZE + 1:
    next_pledge_time = pledges[-1].donationTime
  pledges = pledges[:PAGE_SIZE]

  # Loop through the current pledges and update the associated user with data
  # pulled from Stripe or Paypal
  for pledge in pledges:
    try:
      user = model.User.all().filter('email =', pledge.email).get()
      if user.zipCode and user.address and user.address != 'None':
        continue
      if hasattr(pledge, 'paypalTransactionID') and pledge.paypalTransactionID:
        request_data = {
            'METHOD': 'GetTransactionDetails',
            'TRANSACTIONID': pledge.paypalTransactionID
        }
        rc, txn_data = paypal.send_request(request_data)
        if not rc:
          logging.warning('Error retrieving PayPal transaction: %s', txn_data)
          continue
        user.zipCode = txn_data['SHIPTOZIP'][0]
        user.address = txn_data['SHIPTOSTREET'][0]
        if 'SHIPTOSTREET2' in txn_data:
          user.address += ', %s' % txn_data['SHIPTOSTREET2'][0]
        user.city = txn_data['SHIPTOCITY'][0]
        user.state = txn_data['SHIPTOSTATE'][0]
      elif pledge.stripeCustomer:
        card_data = env.stripe_backend.RetrieveCardData(pledge.stripeCustomer)
        user.zipCode = card_data['address_zip']
        address = card_data['address_line1']
        if card_data['address_line2']:
          address += ', %s' % card_data['address_line2']
        user.address = address
        user.city = card_data['address_city']
        user.state = card_data['address_state']
      user.put()
    except Exception, e:
      logging.warning('Error updating user %s with error, %s', user.email, e)
  def get(self):
    token = self.request.get("token")
    if not token:
      token = self.request.get("TOKEN")

    payer_id = self.request.get("PayerID")
    if not payer_id:
      payer_id = self.request.get("PAYERID")

    if not token or not payer_id:
      logging.warning("Paypal completion missing data: " + self.request.url)
      self.error(400)
      self.response.write(
          "Unusual error: no token or payer id from Paypal.  Please contact " + error_notify_email + " and report these details:")
      self.response.write(self.request.url)
      return

    # Fetch the details of this pending transaction
    form_fields = {
        "METHOD": "GetExpressCheckoutDetails",
        "TOKEN": token
    }
    rc, results = paypal.send_request(form_fields)
    if not rc:
      self.error(400)
      self.response.write(
          "Unusual error: Could not get payment details from Paypal.  Please contact " + error_notify_email + " and report these details:")
      self.response.write(pprint.pformat(results))
      return

    data = dict()

    name = ""
    if 'FIRSTNAME' in results:
      data['first_name'] = results['FIRSTNAME'][0]
      name += results['FIRSTNAME'][0]
    if 'MIDDLENAME' in results:
      name += " " + results['FIRSTNAME'][0]
    if 'LASTNAME' in results:
      data['last_name'] = results['LASTNAME'][0]
      if len(name) > 0:
        name += " "
      name += results['LASTNAME'][0]
    data['name'] = name

    paypal_email = results['EMAIL'][0]
    amount = results['PAYMENTREQUEST_0_AMT'][0]
    cents = int(float(amount)) * 100
    data['amountCents'] = cents
    payer_id = results['PAYERID'][0]
    logging.info("In Results CUSTOM got: " + results['CUSTOM'][0])
    custom = self._get_custom_data(results['CUSTOM'][0])

    cemail = custom['email'].lower() if custom['email'] else ''
    ppemail = paypal_email.lower() if paypal_email else ''

    if cemail != ppemail:
      logging.warning("User entered email [%s], but purchased with email [%s]" % (
          custom['email'], paypal_email))

    logging.info('Got back Custom: ' + str(custom))
    for v in {'email', 'phone', 'occupation', 'employer', 'target', 'subscribe', 'anonymous', 'pledgeType', 'team', 'source', 'surveyResult'}:
      if v in custom:
        data[v] = custom[v]
      else:
        data[v] = None

    if 'team' not in custom:
      data['team'] = self.request.cookies.get("last_team_key")

    data['subscribe'] = data['subscribe'] == 'True'

    rc, results = paypal.DoExpressCheckoutPayment(
        token, payer_id, amount, custom)
    if rc:
      request_data = {
          'METHOD': 'GetTransactionDetails',
          'TRANSACTIONID': results['PAYMENTINFO_0_TRANSACTIONID'][0]
      }
      rc, txn_data = paypal.send_request(request_data)
      if rc:
        if 'SHIPTOSTREET' in txn_data:
          data['address'] = txn_data['SHIPTOSTREET'][0]
          if 'SHIPTOSTREET2' in txn_data:
            data['address'] += ', %s' % txn_data['SHIPTOSTREET2'][0]
        if 'SHIPTOCITY' in txn_data:
          data['city'] = txn_data['SHIPTOCITY'][0]
        if 'SHIPTOSTATE' in txn_data:
          data['state'] = txn_data['SHIPTOSTATE'][0]
        if 'SHIPTOZIP' in txn_data:
          data['zipCode'] = txn_data['SHIPTOZIP'][0]

      id, auth_token, uut, receipt_url = pledge_helper(
          self, data, None, None, payer_id, results['PAYMENTINFO_0_TRANSACTIONID'][0])
      logging.info('Paypal Pledge handler finished')
      # forcing recurring true for paypal
      self.redirect(thank_you_redirect_url = "?amountCents=" +
                    str(data['amountCents']) + '&recurring=true')

    else:
      self.error(400)
      self.response.write(
          "Unusual error: Could not get complete payment from Paypal.  Please contact " + error_notify_email + " and report these details:")
      self.response.write(pprint.pformat(results))
      return
Beispiel #4
0
    def get(self):
        token = self.request.get("token")
        if not token:
            token = self.request.get("TOKEN")

        payer_id = self.request.get("PayerID")
        if not payer_id:
            payer_id = self.request.get("PAYERID")

        if not token or not payer_id:
            logging.warning("Paypal completion missing data: " +
                            self.request.url)
            self.error(400)
            self.response.write(
                "Unusual error: no token or payer id from Paypal.  Please contact [email protected] and report these details:"
            )
            self.response.write(self.request.url)
            return

        # Fetch the details of this pending transaction
        form_fields = {"METHOD": "GetExpressCheckoutDetails", "TOKEN": token}
        rc, results = paypal.send_request(form_fields)
        if not rc:
            self.error(400)
            self.response.write(
                "Unusual error: Could not get payment details from Paypal.  Please contact [email protected] and report these details:"
            )
            self.response.write(pprint.pformat(results))
            return

        data = dict()

        name = ""
        if 'FIRSTNAME' in results:
            data['first_name'] = results['FIRSTNAME'][0]
            name += results['FIRSTNAME'][0]
        if 'MIDDLENAME' in results:
            name += " " + results['FIRSTNAME'][0]
        if 'LASTNAME' in results:
            data['last_name'] = results['LASTNAME'][0]
            if len(name) > 0:
                name += " "
            name += results['LASTNAME'][0]
        data['name'] = name

        paypal_email = results['EMAIL'][0]
        amount = results['PAYMENTREQUEST_0_AMT'][0]
        cents = int(float(amount)) * 100
        data['amountCents'] = cents
        payer_id = results['PAYERID'][0]
        logging.info("In Results CUSTOM got: " + results['CUSTOM'][0])
        custom = self._get_custom_data(results['CUSTOM'][0])

        cemail = custom['email'].lower() if custom['email'] else ''
        ppemail = paypal_email.lower() if paypal_email else ''

        if cemail != ppemail:
            logging.warning(
                "User entered email [%s], but purchased with email [%s]" %
                (custom['email'], paypal_email))

        logging.info('Got back Custom: ' + str(custom))
        for v in {
                'email', 'phone', 'occupation', 'employer', 'target',
                'subscribe', 'anonymous', 'pledgeType', 'team', 'source',
                'surveyResult'
        }:
            if v in custom:
                data[v] = custom[v]
            else:
                data[v] = None

        if 'team' not in custom:
            data['team'] = self.request.cookies.get("last_team_key")

        data['subscribe'] = data['subscribe'] == 'True'

        rc, results = paypal.DoExpressCheckoutPayment(token, payer_id, amount,
                                                      custom)
        if rc:
            request_data = {
                'METHOD': 'GetTransactionDetails',
                'TRANSACTIONID': results['PAYMENTINFO_0_TRANSACTIONID'][0]
            }
            rc, txn_data = paypal.send_request(request_data)
            if rc:
                if 'SHIPTOSTREET' in txn_data:
                    data['address'] = txn_data['SHIPTOSTREET'][0]
                    if 'SHIPTOSTREET2' in txn_data:
                        data[
                            'address'] += ', %s' % txn_data['SHIPTOSTREET2'][0]
                if 'SHIPTOCITY' in txn_data:
                    data['city'] = txn_data['SHIPTOCITY'][0]
                if 'SHIPTOSTATE' in txn_data:
                    data['state'] = txn_data['SHIPTOSTATE'][0]
                if 'SHIPTOZIP' in txn_data:
                    data['zipCode'] = txn_data['SHIPTOZIP'][0]

            id, auth_token, uut, receipt_url = pledge_helper(
                self, data, None, None, payer_id,
                results['PAYMENTINFO_0_TRANSACTIONID'][0])
            logging.info('Paypal Pledge handler finished')
            # forcing recurring true for paypal
            self.redirect("https://lessig2016.us/thank-you/?amountCents=" +
                          str(data['amountCents']) + '&recurring=true')

        else:
            self.error(400)
            self.response.write(
                "Unusual error: Could not get complete payment from Paypal.  Please contact [email protected] and report these details:"
            )
            self.response.write(pprint.pformat(results))
            return
Beispiel #5
0
  def get(self):
    token = self.request.get("token")
    if not token:
      token = self.request.get("TOKEN")

    payer_id = self.request.get("PayerID")
    if not payer_id:
      payer_id = self.request.get("PAYERID")

    if not token or not payer_id:
      logging.warning("Paypal completion missing data: " + self.request.url)
      self.error(400);
      self.response.write("Unusual error: no token or payer id from Paypal.  Please contact [email protected] and report these details:")
      self.response.write(self.request.url)
      return


    # Fetch the details of this pending transaction
    form_fields = {
      "METHOD": "GetExpressCheckoutDetails",
      "TOKEN": token
    }
    rc, results = paypal.send_request(form_fields)
    if not rc:
      self.error(400);
      self.response.write("Unusual error: Could not get payment details from Paypal.  Please contact [email protected] and report these details:")
      self.response.write(pprint.pformat(results))
      return

    data = dict()

    name = ""
    if 'FIRSTNAME' in results:
        data['first_name'] = results['FIRSTNAME'][0]
        name += results['FIRSTNAME'][0]
    if 'MIDDLENAME' in results:
        name += " " + results['FIRSTNAME'][0]
    if 'LASTNAME' in results:
        data['last_name'] = results['LASTNAME'][0]
        if len(name) > 0:
            name += " "
        name += results['LASTNAME'][0]
    data['name'] = name

    paypal_email = results['EMAIL'][0]
    amount = results['PAYMENTREQUEST_0_AMT'][0]
    cents = int(float(amount)) * 100
    data['amountCents'] = cents
    payer_id = results['PAYERID'][0]
    custom = urlparse.parse_qs(results['CUSTOM'][0])
    if custom['email'][0] != paypal_email:
        logging.warning("User entered email [%s], but purchased with email [%s]" % (custom['email'][0], paypal_email))

    for v in { 'email', 'phone', 'occupation', 'employer', 'target', 'subscribe', 'anonymous', 'pledgeType', 'team', 'surveyResult' }:
      if v in custom:
        data[v] = custom[v][0]
      else:
        data[v] = None

    if 'team' not in custom:
        data['team'] = self.request.cookies.get("last_team_key")

    data['subscribe'] =  data['subscribe'] == 'True'

    rc, results = paypal.DoExpressCheckoutPayment(token, payer_id, amount, custom)
    if rc:
      id, auth_token, receipt_url = pledge_helper(self, data, None, None, payer_id, results['PAYMENTINFO_0_TRANSACTIONID'][0])
      self.redirect(receipt_url)

    else:
      self.error(400);
      self.response.write("Unusual error: Could not get complete payment from Paypal.  Please contact [email protected] and report these details:")
      self.response.write(pprint.pformat(results))
      return