def create_order(order_settings, passenger, ride=None, discount_data=None):
    """
    Returns a created Order or None

    @param order_settings:
    @param passenger:
    @param ride:
    @param discount_data: a DiscountData instance
    @return:
    """
    ride_id = ride.id if ride else NEW_ORDER_ID

    # get ride data from algo: don't trust the client
    candidates = [ride] if ride else []
    matching_rides = get_matching_rides(candidates, order_settings)
    ride_data = first(lambda match: match.ride_id == ride_id, matching_rides)

    if not ride_data:
        return None

    order = Order.fromOrderSettings(order_settings, passenger, commit=False)

    if ride:  # if joining a ride, order departure is as shown in offer, not what was submitted in order_settings
        ride_departure = compute_new_departure(ride, ride_data)
        new_order_pickup_point = ride_data.order_pickup_point(NEW_ORDER_ID)
        order.depart_time = ride_departure + datetime.timedelta(seconds=new_order_pickup_point.offset)

    if order_settings.private:
        order.type = OrderType.PRIVATE
    else:
        order.type = OrderType.SHARED

    order.price_data = ride_data.order_price_data(NEW_ORDER_ID)

    if discount_data:
        order = apply_discount_data(order, order_settings, discount_data)
        if not order:
            return None

    order.save()
    logging.info("created new %s order [%s]" % (OrderType.get_name(order.type), order.id))

    billing_trx = BillingTransaction(order=order, amount=order.get_billing_amount(), debug=order.debug)
    billing_trx.save()
    billing_trx.commit(callback_args={
        "ride_id": ride_id,
        "ride_data": ride_data,
        "discount_data": DiscountData.dump(discount_data)
    })

    return order
def get_billing_redirect_url(request, order, passenger):
    """
    returns a url the passenger should be redirected to continue registration/billing process
    """
    if hasattr(passenger, "billing_info"):
        if order:
            billing_trx = BillingTransaction(order=order, amount=order.get_billing_amount(), debug=order.debug)
            billing_trx.save()
            billing_trx.commit()
            return reverse("bill_order", args=[billing_trx.id])
        else:
            return reverse("wb_home")

    else:
        # redirect to credit guard
        # if there is an order we'll get here again by tx_ok with passenger.billing_info ('if' condition will be met)
        return get_token_url(request)
Exemple #3
0
def bill_order(request, trx_id, passenger):
    billing_trx = BillingTransaction.by_id(trx_id)

    request.session[CURRENT_ORDER_KEY] = None

    page_specific_class = "transaction_page"
    pending = BillingStatus.PENDING
    processing = BillingStatus.PROCESSING
    approved = BillingStatus.APPROVED
    failed = BillingStatus.FAILED

    return custom_render_to_response("transaction_page.html", locals(), context_instance=RequestContext(request))
Exemple #4
0
def get_trx_status(request, passenger):
    trx_id = request.GET.get("trx_id")
    trx = BillingTransaction.by_id(trx_id)

    if trx and trx.passenger == passenger:
        response = {'status': trx.status}
        if trx.status == BillingStatus.FAILED:
            msg = get_custom_message(trx.provider_status, trx.comments)
            if msg:
                response.update({'error_message': msg})
        return JSONResponse(response)

    return JSONResponse({'status': BillingStatus.FAILED})
Exemple #5
0
def billing_task(request, token, card_expiration, billing_transaction_id, action):
    logging.info("billing task: transaction_id=%s" % billing_transaction_id)
    action = int(action)

    # update billing transaction amount
    billing_transaction = BillingTransaction.by_id(billing_transaction_id)
    billing_transaction.amount = billing_transaction.order.get_billing_amount()
    if billing_transaction.dirty_fields:
        logging.info("billing_task [%s]: updating billing transaction amount: %s --> %s" % (BillingAction.get_name(action), billing_transaction.dirty_fields.get("amount"), billing_transaction.amount))
        billing_transaction.save()

    callback_args = request.POST.get("callback_args")
    if callback_args:
        callback_args = pickle.loads(callback_args.encode("utf-8"))

    amount = billing_transaction.amount_in_cents

    if action == BillingAction.COMMIT:
        return billing_backend.do_J5(token, amount, card_expiration, billing_transaction, callback_args=callback_args)
    elif action == BillingAction.CHARGE:
        return billing_backend.do_J4(token, amount, card_expiration, billing_transaction)
    else:
        raise InvalidOperationError("Unknown action for billing: %s" % action)
def get_billing_redirect_url(request, order, passenger):
    """
    returns a url the passenger should be redirected to continue registration/billing process
    """
    if hasattr(passenger, "billing_info"):
        if order:
            billing_trx = BillingTransaction(order=order,
                                             amount=order.get_billing_amount(),
                                             debug=order.debug)
            billing_trx.save()
            billing_trx.commit()
            return reverse("bill_order", args=[billing_trx.id])
        else:
            return reverse("wb_home")

    else:
        # redirect to credit guard
        # if there is an order we'll get here again by tx_ok with passenger.billing_info ('if' condition will be met)
        return get_token_url(request)