def dict_to_stripe_object(data): dataDict = ast.literal_eval(data) stripeObj = _stripe.convert_to_stripe_object(dataDict, _stripe.api_key) return stripeObj
def retrieve(cls, bt_id): return convert_to_stripe_object( { "id": u"txn_161MS22eZvKYlo2CD20DfbfM", "object": u"balance_transaction", "amount": 4995, "currency": u"usd", "net": 4820, "type": u"charge", "created": 1431372030, "available_on": 1431907200, "status": u"pending", "fee": 175, "fee_details": [ { "amount": 175, "currency": u"usd", "type": u"stripe_fee", "description": u"Stripe processing fees", "application": None } ], "source": u"ch_161MS22eZvKYlo2CcuXkbZS8", "description": u"Donation to MetaBrainz Foundation", "sourced_transfers": { "object": "list", "total_count": 0, "has_more": None, "url": u"/v1/transfers?source_transaction=ch_161MS22eZvKYlo2CcuXkbZS8", "data": [] } }, api_key=None, account=None )
def add_stripe_event(request): event_data = _get_event(request) if not event_data: return log.debug("Stripe event: %s" % event_data) return stripe.convert_to_stripe_object(event_data, stripe.api_key, None)
def add_stripe_event(request): event_data = _get_event(request) if not event_data: return # NOTE(jkoelker) Events coming from webhooks are not convertable # by default event_data[u"object"] = u"event" log.debug("Stripe event: %s" % event_data) return stripe.convert_to_stripe_object(event_data, stripe.api_key)
def retrieve_payment_source(self): """ """ source = stripe.convert_to_stripe_object(self.source["source"], None, None) class_name = source.class_name() if class_name == "card": s = Card.objects.get(stripe_id=source["id"]) elif class_name == "bankaccount": s = BankAccount.objects.get(stripe_id=source["id"]) else: raise NotImplementedError(class_name) return s
def retrieve(cls, bt_id): return convert_to_stripe_object( { "id": u"txn_161MS22eZvKYlo2CD20DfbfM", "object": u"balance_transaction", "amount": 4995, "currency": u"usd", "net": 4820, "type": u"charge", "created": 1431372030, "available_on": 1431907200, "status": u"pending", "fee": 175, "fee_details": [{ "amount": 175, "currency": u"usd", "type": u"stripe_fee", "description": u"Stripe processing fees", "application": None }], "source": u"ch_161MS22eZvKYlo2CcuXkbZS8", "description": u"Donation to MetaBrainz Foundation", "sourced_transfers": { "object": "list", "total_count": 0, "has_more": None, "url": u"/v1/transfers?source_transaction=ch_161MS22eZvKYlo2CcuXkbZS8", "data": [] } }, api_key=None, account=None)
def post(self, request, *args, **kwargs): post = request.POST.keys()[0] message = json.loads(post) event = message.get('type') del message['type'] if event not in self.event_signals: raise Http404 msg = {'type':event} for key, value in message.iteritems(): if isinstance(value, dict) and 'object' in value: msg[key] = convert_to_stripe_object(value, STRIPE_SECRET_KEY) msg = {'message':msg} signal = self.event_signals.get(event) signal.send_robust(sender=StripeWebhook, **msg) stripe_broadcast_signal.send_robust(sender=StripeWebhook, **msg) return HttpResponse()
def post(self, request, *args, **kwargs): if 'json' not in request.POST: raise Http404 message = json.loads(request.POST.get('json')) event = message.get('event') del message['event'] if event not in self.event_signals: raise Http404 for key, value in message.iteritems(): if isinstance(value, dict) and 'object' in value: message[key] = convert_to_stripe_object(value, STRIPE_SECRET_KEY) signal = self.event_signals.get(event) signal.send_robust(sender=StripeWebhook, **message) return HttpResponse()
def post(self, request, *args, **kwargs): #View accepts only POST, so: post = request.raw_post_data try: message = json.loads(post) except Exception: # writting to logs wrong json f = open('/tmp/err_log', 'w+') f.write(request.body) f.write('\n') f.write(str(request.POST)) f.write('\n') f.write(str(post)) f.write('\nend\n') f.close() #Make that admins know about err raise event = message.get('type') del message['type'] if event not in self.event_signals: return HttpResponse() msg = {'type':event} for key, value in message.iteritems(): if isinstance(value, dict) and 'object' in value: msg[key] = convert_to_stripe_object(value, STRIPE_SECRET_KEY) msg = {'message':msg} signal = self.event_signals.get(event, sSignal) signal.send_robust(sender=StripeWebhook, **msg) stripe_broadcast_signal.send_robust(sender=StripeWebhook, **msg) return HttpResponse()
def get_mock_resource(resource_string, **overrides): mock = MOCKS[resource_string].copy() mock = util.recursive_mapping_update(mock, **overrides) return stripe.convert_to_stripe_object(mock, stripe.api_key, None)
def post(self): # type of message is passed through "type" parameter json_response = json.loads(self.request.body) body_str = json.dumps(json_response).replace("\n", "\\n") stripe_customer_id = None period_start = None period_end = None checkout_id = None status = None subscription_id = None charge_id = None amount = 0 operation = None evt = stripe.convert_to_stripe_object(json_response, options.stripe_secret_key, None) if evt.type == 'invoice.payment_failed': # subscription failed to be paid due to a problem # with the member's credit card or something # for now, just email [email protected] about this stripe_customer_id = evt.data.object.customer subscriber = User.get("stripe_customer_id=%s and deleted=0", stripe_customer_id) if subscriber and options.postmark_api_key: pm = postmark.PMMail( api_key=options.postmark_api_key, sender="*****@*****.**", to="*****@*****.**", subject="%s has a subscription failure" % (subscriber.display_name()), text_body= "Subscription ID: %s\nBuyer Name:%s\nBuyer Email:%s\nUser ID:%s\n" % (subscription_id, subscriber.display_name(), subscriber.email, subscriber.id)) pm.send() return self.finish("OK") elif evt.type == 'customer.subscription.created': # important properties # customer - should be recorded already in account.stripe_customer_id # current_period_start # current_period_end # plan.id (mltshp-annual) stripe_customer_id = evt.data.object.customer period_start = evt.data.object.current_period_start period_end = evt.data.object.current_period_end checkout_id = evt.data.object.id status = "subscribed" # evt.type operation = "subscribe" subscription_id = evt.data.object.id amount = evt.data.object.plan.amount elif evt.type == "customer.subscription.deleted": stripe_customer_id = evt.data.object.customer period_start = evt.data.object.current_period_start period_end = evt.data.object.current_period_end status = "canceled" # evt.type operation = "cancel" subscription_id = evt.data.object.id elif evt.type == 'invoice.payment_succeeded': # customer # date # lines.subscriptions[0].plan.id # period.start # period.end # total line_items = [ item for item in evt.data.object.lines.data if item.type == "subscription" and item.plan.id in (options.stripe_annual_plan_id, options.stripe_monthly_plan_id) ] if line_items: line_item = line_items[0] stripe_customer_id = evt.data.object.customer period_start = line_item.period.start period_end = line_item.period.end checkout_id = evt.data.object.id status = "payment" # evt.type operation = "pay" subscription_id = evt.data.object.subscription charge_id = evt.data.object.charge amount = evt.data.object.total else: # unsupported event type; just ignore it return self.finish("OK") subscriber = None if stripe_customer_id: subscriber = User.get("stripe_customer_id=%s and deleted=0", stripe_customer_id) if subscriber is None: # raise an exception for this... #raise Exception("failed to locate user for stripe_customer_id %s" # % stripe_customer_id) return self.finish("OK") #create a payment log record amount = "USD %0.2f" % (amount / 100.0) pl = PaymentLog(user_id=subscriber.id, status=status, reference_id=checkout_id, transaction_id=charge_id, operation=operation, transaction_date=datetime.datetime.fromtimestamp( period_start).strftime("%Y-%m-%d %H:%M:%S"), next_transaction_date=datetime.datetime.fromtimestamp( period_end).strftime("%Y-%m-%d %H:%M:%S"), buyer_email=subscriber.email, buyer_name=subscriber.display_name(), recipient_email="*****@*****.**", recipient_name="MLTSHP, LLC", payment_reason="MLTSHP Paid Account", transaction_serial_number=1, subscription_id=subscription_id, payment_method='CC', transaction_amount=amount, processor=PaymentLog.STRIPE) pl.save() if evt.type == "customer.subscription.deleted": subscriber.is_paid = 0 else: subscriber.is_paid = 1 subscriber.save() return self.finish("OK")
def test_log_stripe_charge(self): # Function should execute without any exceptions charge = convert_to_stripe_object( { "id": u"ch_15AjX1F21qH57QtHT6avvqrM", "object": u"charge", "created": 1418829367, "livemode": False, "paid": True, "status": u"succeeded", "amount": 99999900, "currency": u"usd", "refunded": False, "source": { "id": u"card_15AjWxF21qH57QtHHVNgaHOP", "object": u"card", "last4": u"4242", "brand": u"Visa", "funding": u"credit", "exp_month": 11, "exp_year": 2016, "country": u"US", "name": u"Uh Oh", "address_line1": u"test 12", "address_line2": None, "address_city": u"Schenectady", "address_state": u"NY", "address_zip": u"12345", "address_country": u"United States", "cvc_check": u"pass", "address_line1_check": u"pass", "address_zip_check": u"pass", "dynamic_last4": None, "metadata": {}, "customer": None }, "captured": True, "balance_transaction": u"txn_159qthF21qH57QtHBksXX3tN", "failure_message": None, "failure_code": None, "amount_refunded": 0, "customer": None, "invoice": None, "description": u"Donation to MetaBrainz Foundation", "dispute": None, "metadata": { "anonymous": u"True", # passed as a string "can_contact": u"False", # passed as a string "email": u"*****@*****.**", "editor": u"null" }, "statement_descriptor": None, "fraud_details": {}, "receipt_email": None, "receipt_number": None, "shipping": None, "application_fee": None, "refunds": { "object": "list", "total_count": 0, "has_more": False, "url": "/v1/charges/ch_15AjX1F21qH57QtHT6avvqrM/refunds", "data": [] } }, api_key=None, account=None ) Donation.log_stripe_charge(charge)
def test_log_stripe_charge_payment(self): # Function should execute without any exceptions charge = convert_to_stripe_object( { "id": u"ch_15AjX1F21qH57QtHT6avvqrM", "object": u"charge", "created": 1418829367, "livemode": False, "paid": True, "status": u"succeeded", "amount": 99999900, "currency": u"usd", "refunded": False, "source": { "id": u"card_15AjWxF21qH57QtHHVNgaHOP", "object": u"card", "last4": u"4242", "brand": u"Visa", "funding": u"credit", "exp_month": 11, "exp_year": 2016, "country": u"US", "name": u"Uh Oh", "address_line1": u"test 12", "address_line2": None, "address_city": u"Schenectady", "address_state": u"NY", "address_zip": u"12345", "address_country": u"United States", "cvc_check": u"pass", "address_line1_check": u"pass", "address_zip_check": u"pass", "dynamic_last4": None, "metadata": {}, "customer": None }, "captured": True, "balance_transaction": u"txn_159qthF21qH57QtHBksXX3tN", "failure_message": None, "failure_code": None, "amount_refunded": 0, "customer": None, "invoice": None, "description": u"Donation to MetaBrainz Foundation", "dispute": None, "metadata": { "is_donation": False, "email": u"*****@*****.**", "invoice_number": 42, }, "statement_descriptor": None, "fraud_details": {}, "receipt_email": None, "receipt_number": None, "shipping": None, "application_fee": None, "refunds": { "object": "list", "total_count": 0, "has_more": False, "url": "/v1/charges/ch_15AjX1F21qH57QtHT6avvqrM/refunds", "data": [] } }, api_key=None, account=None) Payment.log_stripe_charge(charge)