Exemple #1
0
 def after_model_change(self, form, new_donation, is_created):
     if is_created:
         send_receipt(
             new_donation.email,
             new_donation.payment_date,
             new_donation.amount,
             '%s %s' % (new_donation.first_name, new_donation.last_name),
             new_donation.editor_name,
         )
 def after_model_change(self, form, new_donation, is_created):
     if is_created:
         send_receipt(
             new_donation.email,
             new_donation.payment_date,
             new_donation.amount,
             "%s %s" % (new_donation.first_name, new_donation.last_name),
             new_donation.editor_name,
         )
Exemple #3
0
    def log_stripe_charge(cls, charge):
        """Log successful Stripe charge.

        Args:
            charge: The charge object from Stripe. More information about it is
                available at https://stripe.com/docs/api/python#charge_object.
        """
        logging.debug('Processing Stripe charge...')

        bt = stripe.BalanceTransaction.retrieve(charge.balance_transaction)

        new_donation = cls(
            first_name=charge.source.name,
            last_name='',
            amount=bt.net / 100,  # cents should be converted
            fee=bt.fee / 100,  # cents should be converted
            transaction_id=charge.id,
            payment_method=PAYMENT_METHOD_STRIPE,

            address_street=charge.source.address_line1,
            address_city=charge.source.address_city,
            address_state=charge.source.address_state,
            address_postcode=charge.source.address_zip,
            address_country=charge.source.address_country,

            email=charge.metadata.email,
            can_contact=charge.metadata.can_contact == u'True',
            anonymous=charge.metadata.anonymous == u'True',
        )

        if 'editor' in charge.metadata:
            new_donation.editor_name = charge.metadata.editor

        db.session.add(new_donation)
        db.session.commit()
        logging.info('Stripe: Payment added. ID: %s.', new_donation.id)

        send_receipt(
            new_donation.email,
            new_donation.payment_date,
            new_donation.amount,
            new_donation.first_name,  # Last name is not used with Stripe
            new_donation.editor_name,
        )
Exemple #4
0
    def log_stripe_charge(cls, charge):
        """Log successful Stripe charge.

        Args:
            charge: The charge object from Stripe. More information about it is
                available at https://stripe.com/docs/api/python#charge_object.
        """
        logging.debug('Processing Stripe charge...')

        bt = stripe.BalanceTransaction.retrieve(charge.balance_transaction)

        new_donation = cls(
            first_name=charge.source.name,
            last_name='',
            amount=bt.net / 100,  # cents should be converted
            fee=bt.fee / 100,  # cents should be converted
            transaction_id=charge.id,
            payment_method=PAYMENT_METHOD_STRIPE,
            address_street=charge.source.address_line1,
            address_city=charge.source.address_city,
            address_state=charge.source.address_state,
            address_postcode=charge.source.address_zip,
            address_country=charge.source.address_country,
            email=charge.metadata.email,
            can_contact=charge.metadata.can_contact == u'True',
            anonymous=charge.metadata.anonymous == u'True',
        )

        if 'editor' in charge.metadata:
            new_donation.editor_name = charge.metadata.editor

        db.session.add(new_donation)
        db.session.commit()
        logging.info('Stripe: Payment added. ID: %s.', new_donation.id)

        send_receipt(
            new_donation.email,
            new_donation.payment_date,
            new_donation.amount,
            new_donation.first_name,  # Last name is not used with Stripe
            new_donation.editor_name,
        )
Exemple #5
0
    def verify_and_log_wepay_checkout(cls, checkout_id, editor, anonymous,
                                      can_contact):
        logging.debug('Processing WePay checkout...')

        # Looking up updated information about the object
        wepay = WePay(production=current_app.config['PAYMENT_PRODUCTION'],
                      access_token=current_app.config['WEPAY_ACCESS_TOKEN'])
        details = wepay.call('/checkout', {'checkout_id': checkout_id})

        if 'error' in details:
            logging.warning('WePay: Error: %s', details['error_description'])
            return False

        if 'gross' not in details:
            logging.warning('WePay: The total dollar amount paid is missing')
            return False

        if details['gross'] < 0.50:
            # Tiny donation
            logging.info('WePay: Tiny donation ($%s).', details['gross'])
            return True

        if details['state'] in ['settled', 'captured']:
            # Payment has been received

            # Checking that txn_id has not been previously processed
            if cls.get_by_transaction_id(details['checkout_id']) is not None:
                logging.info('WePay: Transaction ID %s has been used before.',
                             details['checkout_id'])
                return

            new_donation = cls(
                first_name=details['payer_name'],
                last_name='',
                email=details['payer_email'],
                editor_name=editor,
                can_contact=can_contact,
                anonymous=anonymous,
                amount=details['gross'] - details['fee'],
                fee=details['fee'],
                transaction_id=checkout_id,
                payment_method=PAYMENT_METHOD_WEPAY,
            )

            if 'shipping_address' in details:
                address = details['shipping_address']
                new_donation.address_street = "%s\n%s" % (address['address1'],
                                                          address['address2'])
                new_donation.address_city = address['city']
                if 'state' in address:  # US address
                    new_donation.address_state = address['state']
                else:
                    new_donation.address_state = address['region']
                if 'zip' in address:  # US address
                    new_donation.address_postcode = address['zip']
                else:
                    new_donation.address_postcode = address['postcode']

            db.session.add(new_donation)
            db.session.commit()
            logging.info('WePay: Payment added. ID: %s.', new_donation.id)

            send_receipt(
                new_donation.email,
                new_donation.payment_date,
                new_donation.amount,
                '%s %s' % (new_donation.first_name, new_donation.last_name),
                new_donation.editor_name,
            )

        elif details['state'] in ['authorized', 'reserved']:
            # Payment is pending
            logging.info('WePay: Payment is pending. State: "%s".',
                         details['state'])
            pass

        elif details['state'] in [
                'expired', 'cancelled', 'failed', 'refunded', 'chargeback'
        ]:
            # Payment has failed
            logging.warning('WePay: Payment has failed. State: "%s".',
                            details['state'])
            pass

        else:
            # Unknown status
            logging.warning('WePay: Unknown status.')
            return False

        return True
Exemple #6
0
    def process_paypal_ipn(cls, form):
        """Processor for PayPal IPNs (Instant Payment Notifications).

        Should be used only after IPN request is verified. See PayPal documentation for
        more info about the process.

        Args:
            form: The form parameters from IPN request that contains IPN variables.
                See https://developer.paypal.com/docs/classic/ipn/integration-guide/IPNandPDTVariables/
                for more info about them.
        """
        logging.debug('Processing PayPal IPN...')

        # Only processing completed donations
        if form['payment_status'] != 'Completed':
            logging.info('PayPal: Payment not completed. Status: "%s".',
                         form['payment_status'])
            return

        # We shouldn't process transactions to address for payments
        # TODO: Clarify what this address is
        if form['business'] == current_app.config['PAYPAL_BUSINESS']:
            logging.info('PayPal: Recieved payment to address for payments.')
            return

        if form['receiver_email'] != current_app.config['PAYPAL_PRIMARY_EMAIL']:
            logging.warning('PayPal: Not primary email. Got "%s".',
                            form['receiver_email'])
            return
        if float(form['mc_gross']) < 0.50:
            # Tiny donation
            logging.info('PayPal: Tiny donation ($%s).', form['mc_gross'])
            return

        # Checking that txn_id has not been previously processed
        if cls.get_by_transaction_id(form['txn_id']) is not None:
            logging.info('PayPal: Transaction ID %s has been used before.',
                         form['txn_id'])
            return

        new_donation = cls(
            first_name=form['first_name'],
            last_name=form['last_name'],
            email=form['payer_email'],
            editor_name=form.get('custom'),
            address_street=form.get('address_street'),
            address_city=form.get('address_city'),
            address_state=form.get('address_state'),
            address_postcode=form.get('address_zip'),
            address_country=form.get('address_country'),
            amount=float(form['mc_gross']) - float(form['mc_fee']),
            fee=float(form['mc_fee']),
            transaction_id=form['txn_id'],
            payment_method=PAYMENT_METHOD_PAYPAL,
        )

        if 'option_name1' in form and 'option_name2' in form:
            if (form['option_name1'] == 'anonymous' and form['option_selection1'] == 'yes') or \
                    (form['option_name2'] == 'anonymous' and form['option_selection2'] == 'yes') or \
                            form['option_name2'] == 'yes':
                new_donation.anonymous = True
            if (form['option_name1'] == 'contact' and form['option_selection1'] == 'yes') or \
                    (form['option_name2'] == 'contact' and form['option_selection2'] == 'yes') or \
                            form['option_name2'] == 'yes':
                new_donation.can_contact = True

        db.session.add(new_donation)
        db.session.commit()
        logging.info('PayPal: Payment added. ID: %s.', new_donation.id)

        send_receipt(
            new_donation.email,
            new_donation.payment_date,
            new_donation.amount,
            '%s %s' % (new_donation.first_name, new_donation.last_name),
            new_donation.editor_name,
        )
    def verify_and_log_wepay_checkout(cls, checkout_id, editor, anonymous, can_contact):
        logging.debug("Processing WePay checkout...")

        # Looking up updated information about the object
        wepay = WePay(
            production=current_app.config["PAYMENT_PRODUCTION"], access_token=current_app.config["WEPAY_ACCESS_TOKEN"]
        )
        details = wepay.call("/checkout", {"checkout_id": checkout_id})

        if "error" in details:
            logging.warning("WePay: Error: %s", details["error_description"])
            return False

        if "gross" not in details:
            logging.warning("WePay: The total dollar amount paid is missing")
            return False

        if details["gross"] < 0.50:
            # Tiny donation
            logging.info("WePay: Tiny donation ($%s).", details["gross"])
            return True

        if details["state"] in ["settled", "captured"]:
            # Payment has been received

            # Checking that txn_id has not been previously processed
            if cls.get_by_transaction_id(details["checkout_id"]) is not None:
                logging.info("WePay: Transaction ID %s has been used before.", details["checkout_id"])
                return

            new_donation = cls(
                first_name=details["payer_name"],
                last_name="",
                email=details["payer_email"],
                editor_name=editor,
                can_contact=can_contact,
                anonymous=anonymous,
                amount=details["gross"] - details["fee"],
                fee=details["fee"],
                transaction_id=checkout_id,
                payment_method=PAYMENT_METHOD_WEPAY,
            )

            if "shipping_address" in details:
                address = details["shipping_address"]
                new_donation.address_street = "%s\n%s" % (address["address1"], address["address2"])
                new_donation.address_city = address["city"]
                if "state" in address:  # US address
                    new_donation.address_state = address["state"]
                else:
                    new_donation.address_state = address["region"]
                if "zip" in address:  # US address
                    new_donation.address_postcode = address["zip"]
                else:
                    new_donation.address_postcode = address["postcode"]

            db.session.add(new_donation)
            db.session.commit()
            logging.info("WePay: Payment added. ID: %s.", new_donation.id)

            send_receipt(
                new_donation.email,
                new_donation.payment_date,
                new_donation.amount,
                "%s %s" % (new_donation.first_name, new_donation.last_name),
                new_donation.editor_name,
            )

        elif details["state"] in ["authorized", "reserved"]:
            # Payment is pending
            logging.info('WePay: Payment is pending. State: "%s".', details["state"])
            pass

        elif details["state"] in ["expired", "cancelled", "failed", "refunded", "chargeback"]:
            # Payment has failed
            logging.warning('WePay: Payment has failed. State: "%s".', details["state"])
            pass

        else:
            # Unknown status
            logging.warning("WePay: Unknown status.")
            return False

        return True
    def process_paypal_ipn(cls, form):
        """Processor for PayPal IPNs (Instant Payment Notifications).

        Should be used only after IPN request is verified. See PayPal documentation for
        more info about the process.

        Args:
            form: The form parameters from IPN request that contains IPN variables.
                See https://developer.paypal.com/docs/classic/ipn/integration-guide/IPNandPDTVariables/
                for more info about them.
        """
        logging.debug("Processing PayPal IPN...")

        # Only processing completed donations
        if form["payment_status"] != "Completed":
            logging.info('PayPal: Payment not completed. Status: "%s".', form["payment_status"])
            return

        # We shouldn't process transactions to address for payments
        # TODO: Clarify what this address is
        if form["business"] == current_app.config["PAYPAL_BUSINESS"]:
            logging.info("PayPal: Recieved payment to address for payments.")
            return

        if form["receiver_email"] != current_app.config["PAYPAL_PRIMARY_EMAIL"]:
            logging.warning('PayPal: Not primary email. Got "%s".', form["receiver_email"])
            return
        if float(form["mc_gross"]) < 0.50:
            # Tiny donation
            logging.info("PayPal: Tiny donation ($%s).", form["mc_gross"])
            return

        # Checking that txn_id has not been previously processed
        if cls.get_by_transaction_id(form["txn_id"]) is not None:
            logging.info("PayPal: Transaction ID %s has been used before.", form["txn_id"])
            return

        new_donation = cls(
            first_name=form["first_name"],
            last_name=form["last_name"],
            email=form["payer_email"],
            editor_name=form.get("custom"),
            address_street=form.get("address_street"),
            address_city=form.get("address_city"),
            address_state=form.get("address_state"),
            address_postcode=form.get("address_zip"),
            address_country=form.get("address_country"),
            amount=float(form["mc_gross"]) - float(form["mc_fee"]),
            fee=float(form["mc_fee"]),
            transaction_id=form["txn_id"],
            payment_method=PAYMENT_METHOD_PAYPAL,
        )

        if "option_name1" in form and "option_name2" in form:
            if (
                (form["option_name1"] == "anonymous" and form["option_selection1"] == "yes")
                or (form["option_name2"] == "anonymous" and form["option_selection2"] == "yes")
                or form["option_name2"] == "yes"
            ):
                new_donation.anonymous = True
            if (
                (form["option_name1"] == "contact" and form["option_selection1"] == "yes")
                or (form["option_name2"] == "contact" and form["option_selection2"] == "yes")
                or form["option_name2"] == "yes"
            ):
                new_donation.can_contact = True

        db.session.add(new_donation)
        db.session.commit()
        logging.info("PayPal: Payment added. ID: %s.", new_donation.id)

        send_receipt(
            new_donation.email,
            new_donation.payment_date,
            new_donation.amount,
            "%s %s" % (new_donation.first_name, new_donation.last_name),
            new_donation.editor_name,
        )
Exemple #9
0
    def verify_and_log_wepay_checkout(cls, checkout_id, editor, anonymous, can_contact):
        logging.debug('Processing WePay checkout...')

        # Looking up updated information about the object
        wepay = WePay(production=current_app.config['PAYMENT_PRODUCTION'],
                      access_token=current_app.config['WEPAY_ACCESS_TOKEN'])
        details = wepay.call('/checkout', {'checkout_id': checkout_id})

        if 'error' in details:
            logging.warning('WePay: Error: %s', details['error_description'])
            return False

        if 'gross' not in details:
            logging.warning('WePay: The total dollar amount paid is missing')
            return False

        if details['gross'] < 0.50:
            # Tiny donation
            logging.info('WePay: Tiny donation ($%s).', details['gross'])
            return True

        if details['state'] in ['settled', 'captured']:
            # Payment has been received

            # Checking that txn_id has not been previously processed
            if cls.get_by_transaction_id(details['checkout_id']) is not None:
                logging.info('WePay: Transaction ID %s has been used before.', details['checkout_id'])
                return

            new_donation = cls(
                first_name=details['payer_name'],
                last_name='',
                email=details['payer_email'],
                editor_name=editor,
                can_contact=can_contact,
                anonymous=anonymous,
                amount=details['gross'] - details['fee'],
                fee=details['fee'],
                transaction_id=checkout_id,
                payment_method=PAYMENT_METHOD_WEPAY,
            )

            if 'shipping_address' in details:
                address = details['shipping_address']
                new_donation.address_street = "%s\n%s" % (address['address1'], address['address2'])
                new_donation.address_city = address['city']
                if 'state' in address:  # US address
                    new_donation.address_state = address['state']
                else:
                    new_donation.address_state = address['region']
                if 'zip' in address:  # US address
                    new_donation.address_postcode = address['zip']
                else:
                    new_donation.address_postcode = address['postcode']

            db.session.add(new_donation)
            db.session.commit()
            logging.info('WePay: Payment added. ID: %s.', new_donation.id)

            send_receipt(
                new_donation.email,
                new_donation.payment_date,
                new_donation.amount,
                '%s %s' % (new_donation.first_name, new_donation.last_name),
                new_donation.editor_name,
            )

        elif details['state'] in ['authorized', 'reserved']:
            # Payment is pending
            logging.info('WePay: Payment is pending. State: "%s".', details['state'])
            pass

        elif details['state'] in ['expired', 'cancelled', 'failed', 'refunded', 'chargeback']:
            # Payment has failed
            logging.warning('WePay: Payment has failed. State: "%s".', details['state'])
            pass

        else:
            # Unknown status
            logging.warning('WePay: Unknown status.')
            return False

        return True
Exemple #10
0
    def process_paypal_ipn(cls, form):
        """Processor for PayPal IPNs (Instant Payment Notifications).

        Should be used only after IPN request is verified. See PayPal documentation for
        more info about the process.

        Args:
            form: The form parameters from IPN request that contains IPN variables.
                See https://developer.paypal.com/docs/classic/ipn/integration-guide/IPNandPDTVariables/
                for more info about them.
        """
        logging.debug('Processing PayPal IPN...')

        # Only processing completed donations
        if form['payment_status'] != 'Completed':
            logging.info('PayPal: Payment not completed. Status: "%s".', form['payment_status'])
            return

        # We shouldn't process transactions to address for payments
        # TODO: Clarify what this address is
        if form['business'] == current_app.config['PAYPAL_BUSINESS']:
            logging.info('PayPal: Recieved payment to address for payments.')
            return

        if form['receiver_email'] != current_app.config['PAYPAL_PRIMARY_EMAIL']:
            logging.warning('PayPal: Not primary email. Got "%s".', form['receiver_email'])
            return
        if float(form['mc_gross']) < 0.50:
            # Tiny donation
            logging.info('PayPal: Tiny donation ($%s).', form['mc_gross'])
            return

        # Checking that txn_id has not been previously processed
        if cls.get_by_transaction_id(form['txn_id']) is not None:
            logging.info('PayPal: Transaction ID %s has been used before.', form['txn_id'])
            return

        new_donation = cls(
            first_name=form['first_name'],
            last_name=form['last_name'],
            email=form['payer_email'],
            editor_name=form.get('custom'),
            address_street=form.get('address_street'),
            address_city=form.get('address_city'),
            address_state=form.get('address_state'),
            address_postcode=form.get('address_zip'),
            address_country=form.get('address_country'),
            amount=float(form['mc_gross']) - float(form['mc_fee']),
            fee=float(form['mc_fee']),
            transaction_id=form['txn_id'],
            payment_method=PAYMENT_METHOD_PAYPAL,
        )

        if 'option_name1' in form and 'option_name2' in form:
            if (form['option_name1'] == 'anonymous' and form['option_selection1'] == 'yes') or \
                    (form['option_name2'] == 'anonymous' and form['option_selection2'] == 'yes') or \
                            form['option_name2'] == 'yes':
                new_donation.anonymous = True
            if (form['option_name1'] == 'contact' and form['option_selection1'] == 'yes') or \
                    (form['option_name2'] == 'contact' and form['option_selection2'] == 'yes') or \
                            form['option_name2'] == 'yes':
                new_donation.can_contact = True

        db.session.add(new_donation)
        db.session.commit()
        logging.info('PayPal: Payment added. ID: %s.', new_donation.id)

        send_receipt(
            new_donation.email,
            new_donation.payment_date,
            new_donation.amount,
            '%s %s' % (new_donation.first_name, new_donation.last_name),
            new_donation.editor_name,
        )