Example #1
0
def _do_expire_frozen_cheque(tx, cheque_record):
    event_ids = []
    event_id = zyt_bookkeeping(EventType.UNFREEZE, tx.sn,
                               cheque_record.from_id, cheque_record.amount)
    event_ids.append(event_id)
    transit_transaction_state(tx.id, ChequeTxState.FROZEN,
                              ChequeTxState.EXPIRED, event_ids)
Example #2
0
def succeed_paid_out(vas_name, tx, payer_id, amount):
    event_id = bookkeeping(EventType.TRANSFER_OUT, tx.sn, payer_id, vas_name,
                           amount)
    transit_transaction_state(tx.id, PaymentTxState.CREATED,
                              PaymentTxState.PAID_OUT, event_id)

    # real payer
    add_tx_user(tx.id, user_roles.from_user(payer_id))
Example #3
0
def expire_cheque(tx, cheque_record):
    if datetime.utcnow() > cheque_record.expired_at:
        if cheque_record.type == ChequeType.INSTANT:
            _do_expire_frozen_cheque(tx, cheque_record)
        elif cheque_record.type == ChequeType.LAZY:
            transit_transaction_state(tx.id, ChequeTxState.CREATED,
                                      ChequeTxState.EXPIRED)
        return True
    return False
Example #4
0
def _draw_cheque(tx, cheque_record):
    if cheque_record.type == ChequeType.INSTANT:
        event_ids = []
        event_id = zyt_bookkeeping(EventType.FREEZE, tx.sn,
                                   cheque_record.from_id, cheque_record.amount)
        event_ids.append(event_id)
        transit_transaction_state(tx.id, ChequeTxState.CREATED,
                                  ChequeTxState.FROZEN, event_ids)
    elif cheque_record.type == ChequeType.LAZY:
        pass
Example #5
0
def _confirm_payment(payment_record):
    secure_user_id = get_system_account_user_id(SECURE_USER_NAME)
    # 确认金额为订单金额 - 已退款金额
    amount = payment_record.amount - payment_record.refunded_amount
    event_id1 = zyt_bookkeeping(EventType.TRANSFER_OUT_FROZEN,
                                payment_record.sn, secure_user_id, amount)
    event_id2 = zyt_bookkeeping(EventType.TRANSFER_IN, payment_record.sn,
                                payment_record.payee_id, amount)

    transit_transaction_state(payment_record.tx_id, PaymentTxState.SECURED,
                              PaymentTxState.SUCCESS, [event_id1, event_id2])
Example #6
0
def succeed_payment(vas_name,
                    tx,
                    payment_record,
                    state=PaymentTxState.SUCCESS,
                    bk=None):
    payee_id = payment_record.payee_id
    amount = payment_record.real_amount
    if not in_to_pay_state(tx.state):
        raise TransactionStateError(msg='state error: [{0}]'.format(tx.state))

    if amount == 0:
        # 不用记账
        event_id = None
    else:
        event_id = bk(tx.sn, amount) if bk else bookkeeping(
            EventType.TRANSFER_IN, tx.sn, payee_id, vas_name, amount)
    transit_transaction_state(tx.id, tx.state, state, event_id)
Example #7
0
def _freeze_withdraw(tx, withdraw_record):
    from_user_id = withdraw_record.from_user_id
    fee = withdraw_record.fee
    actual_amount = withdraw_record.actual_amount

    event_ids = []
    # 冻结相关资金
    if fee > 0:
        # 手续费
        event_id = zyt_bookkeeping(EventType.FREEZE, tx.sn, from_user_id, fee)
        event_ids.append(event_id)
    # 提现金额
    event_id = zyt_bookkeeping(EventType.FREEZE, tx.sn, from_user_id,
                               actual_amount)
    event_ids.append(event_id)

    transit_transaction_state(tx.id, WithdrawTxState.CREATED,
                              WithdrawTxState.FROZEN, event_ids)
Example #8
0
File: models.py Project: webee/pay
    def add_refund(self, refund_record, event_id):
        from api_x.constant import PaymentTxState
        from api_x.zyt.biz.transaction import transit_transaction_state
        self.refunded_amount += refund_record.amount
        # 全部金额都退款,则状态为已退款
        is_refunded = self.paid_amount == self.refunded_amount

        if self.tx.state == PaymentTxState.REFUNDING:
            if is_refunded:
                transit_transaction_state(self.tx_id, PaymentTxState.REFUNDING,
                                          PaymentTxState.REFUNDED, event_id)
            else:
                transit_transaction_state(self.tx_id, PaymentTxState.REFUNDING,
                                          refund_record.payment_state,
                                          event_id)
        db.session.add(self)

        super_tx = self.tx.super
        if super_tx and super_tx.type == TransactionType.PAYMENT:
            super_payment = super_tx.record
            super_payment.add_refund(refund_record, event_id)
Example #9
0
def handle_duplicate_pay(vas_name, vas_sn, tx, prepaid_record):
    """处理充值时的重复支付"""
    event_id = bookkeeping(EventType.TRANSFER_IN, tx.source_sn,
                           prepaid_record.to_id, vas_name,
                           prepaid_record.amount)
    # 交易总金额增加
    tx.amount += prepaid_record.amount
    db.session.add(tx)

    # 不改变状态,只是添加一条关联event
    transit_transaction_state(prepaid_record.tx_id, tx.state, tx.state,
                              event_id)

    # 添加一条重复支付记录
    duplicate_payment_record = DuplicatedPaymentRecord(
        tx_id=tx.id,
        sn=tx.sn,
        event_id=event_id,
        vas_name=vas_name,
        vas_sn=vas_sn,
        source=TransactionType.PREPAID)
    db.session.add(duplicate_payment_record)
Example #10
0
def _success_withdraw(tx, withdraw_record):
    from_user_id = withdraw_record.from_user_id
    fee = withdraw_record.fee
    actual_amount = withdraw_record.actual_amount

    event_ids = []
    if fee > 0:
        from api_x.zyt.vas.pattern import transfer_frozen
        from api_x.zyt.user_mapping import get_lvye_corp_account_user_id
        from api_x.constant import LVYE_WITHDRAW_FEE_USER_NAME
        lvye_withdraw_fee_user_id = get_lvye_corp_account_user_id(
            LVYE_WITHDRAW_FEE_USER_NAME)
        # 手续费转账
        event_ids.extend(
            transfer_frozen(tx.sn, from_user_id, lvye_withdraw_fee_user_id,
                            fee))
    # 提现金额转出
    event_id = bookkeeping(EventType.TRANSFER_OUT_FROZEN, tx.sn, from_user_id,
                           tx.vas_name, actual_amount)
    event_ids.append(event_id)

    transit_transaction_state(tx.id, WithdrawTxState.PROCESSING,
                              WithdrawTxState.SUCCESS, event_ids)
Example #11
0
def _cash_cheque(cheque_record, to_id):
    from api_x.zyt.vas import NAME
    tx = cheque_record.tx
    with require_transaction_context():
        tx = update_transaction_info(tx.id, tx.sn, vas_name=NAME)
        if cheque_record.type == ChequeType.INSTANT:
            event_ids = transfer_frozen(tx.sn, cheque_record.from_id, to_id,
                                        cheque_record.amount)
            transit_transaction_state(tx.id, ChequeTxState.FROZEN,
                                      ChequeTxState.CASHED, event_ids)
        elif cheque_record.type == ChequeType.LAZY:
            event_ids = transfer(tx.sn, cheque_record.from_id, to_id,
                                 cheque_record.amount)
            transit_transaction_state(tx.id, ChequeTxState.CREATED,
                                      ChequeTxState.CASHED, event_ids)

        # update to id.
        add_tx_user(tx.id, user_roles.to_user(to_id))
        cheque_record.to_id = to_id
        db.session.add(cheque_record)

    # notify client.
    tx = get_tx_by_id(tx.id)
    _try_notify_client(tx, cheque_record)
Example #12
0
def succeed_prepaid(vas_name, tx, prepaid_record):
    event_id = bookkeeping(EventType.TRANSFER_IN, tx.sn, prepaid_record.to_id,
                           vas_name, prepaid_record.amount)
    transit_transaction_state(tx.id, tx.state, PrepaidTxState.SUCCESS,
                              event_id)
Example #13
0
def _cancel_cheque(tx, cheque_record):
    if cheque_record.type == ChequeType.INSTANT:
        _do_cancel_frozen_cheque(tx, cheque_record)
    elif cheque_record.type == ChequeType.LAZY:
        transit_transaction_state(tx.id, ChequeTxState.CREATED,
                                  ChequeTxState.CANCELED)
Example #14
0
def fail_payment(payment_record):
    transit_transaction_state(payment_record.tx_id, PaymentTxState.CREATED,
                              PaymentTxState.FAILED)
Example #15
0
def _fail_withdraw(tx, withdraw_record):
    event_ids = _unfreeze_withdraw_amount(tx, withdraw_record)
    transit_transaction_state(tx.id, WithdrawTxState.PROCESSING,
                              WithdrawTxState.FAILED, event_ids)
Example #16
0
def fail_prepaid(tx):
    transit_transaction_state(tx.id, PrepaidTxState.CREATED,
                              PrepaidTxState.FAILED)
Example #17
0
def _withdraw_to_processing(tx):
    transit_transaction_state(tx.id, WithdrawTxState.FROZEN,
                              WithdrawTxState.PROCESSING)