def post(self, request): # TODO: make this not require sign-in. form = SubscriptionForm(request.user, request.DATA) if not form.is_valid(): return error_400(response=form.errors) # TODO: remove this after # https://github.com/mozilla/solitude/issues/466 product = self.api.generic.product.get_object_or_404(public_id=form.cleaned_data["plan_id"]) # Check if the user is already subscribed to this plan. result = self.api.braintree.mozilla.subscription.get( paymethod__braintree_buyer__buyer=form.user.pk, seller_product=product["resource_pk"] ) if len(result): log.info( "buyer {buyer} is already subscribed to product {product}".format( buyer=form.user.pk, product=product["resource_pk"] ) ) return error_400(response="user is already subscribed to this product") try: pay_method_uri = self.get_pay_method( form.user, form.cleaned_data["pay_method_uri"], form.cleaned_data["pay_method_nonce"] ) self.api.braintree.subscription.post( { "paymethod": pay_method_uri, "plan": form.cleaned_data["plan_id"], "amount": form.cleaned_data["amount"], } ) except HttpClientError, exc: log.debug("caught bad request from solitude: {e}".format(e=exc)) return error_400(exception=exc)
def post(self, request): form = SaleForm(request.user, request.DATA) if not form.is_valid(): return error_400(response=form.errors) try: self.api.braintree.sale.post({ 'amount': form.cleaned_data['amount'], 'product_id': form.cleaned_data['product_id'], 'nonce': form.cleaned_data['nonce'], 'paymethod': form.cleaned_data['paymethod'], }) log.info('posted a sale for user/product: {}/{}' .format(request.user, form.cleaned_data['product_id'])) except HttpClientError, exc: log.debug('caught bad request from solitude: {}'.format(exc)) return error_400(exception=exc)
def post(self, request): form = ManageSubscriptionForm(request.user, request.DATA) if not form.is_valid(): return error_400(response=form.errors) self.api.braintree.subscription.cancel.post({"subscription": form.cleaned_data["subscription_uri"]}) log.info("user {} cancelled subscription {}".format(request.user, form.cleaned_data["subscription_uri"])) return Response({}, status=204)
def post(self, request): api = solitude.api() data = request.data.copy() data['buyer_uuid'] = request.user.uuid try: result = api.braintree.paymethod.post(data) except HttpClientError, exc: log.warn('post: solitude returned 400: {}'.format(exc)) return error_400(exception=exc)
def post(self, request): form = DeletePayMethodForm(request.user, request.data) if not form.is_valid(): return error_400(response=form.errors) pay_method = form.cleaned_data.get('pay_method_uri') log.info('deleting payment method for user: {} {}' .format(pay_method, request.user)) # This is a workaround for accessing the /delete/ path without slumber # thinking it's an HTTP DELETE. solitude.api().braintree.paymethod('delete').post({ 'paymethod': pay_method, }) payment_methods = get_active_user_pay_methods(request.user) return Response({'payment_methods': payment_methods}, status=200)
def post(self, request): form = ChangeSubscriptionPayMethodForm(request.user, request.DATA) if not form.is_valid(): return error_400(response=form.errors) self.api.braintree.subscription.paymethod.change.post( { "paymethod": form.cleaned_data["new_pay_method_uri"], "subscription": form.cleaned_data["subscription_uri"], } ) log.info( "changed paymethod to {} for subscription {} belonging to " "user {}".format( form.cleaned_data["new_pay_method_uri"], form.cleaned_data["subscription_uri"], request.user ) ) return Response({}, status=204)