def check_status(yandex_client_id, yandex_instance_id_key, payment_request_id, yandex_auth_success_uri, yandex_auth_fail_uri): external_payment = ExternalPayment(yandex_instance_id_key) response_external_payment_process = methods.external_payment_process( external_payment, payment_request_id, yandex_auth_success_uri, yandex_auth_fail_uri, yandex_client_id, request_token=False) if response_external_payment_process["key"] is "status": return response_external_payment_process else: return
def makeRequest(self): instance_id = ExternalPayment.get_instance_id(CLIENT_ID)['instance_id'] api = ExternalPayment(instance_id) options = { "instance_id": instance_id, "pattern_id": "p2p", "to": "410011161616877", "amount_due": "0.02", "comment": "test payment comment from yandex-money-python", "message": "test payment message from yandex-money-python", "label": "testPayment", "test_payment": True, "test_result": "success" } return api.request(options), api
def process_external_payment(instance_id, request_id, auth_success_uri, auth_fail_uri): logger.debug('Processing exteranl payment') external_payment = ExternalPayment(instance_id) process_options = { "request_id": request_id, 'instance_id': instance_id, 'ext_auth_success_uri': auth_success_uri, 'ext_auth_fail_uri': auth_fail_uri } response = external_payment.process(process_options) logger.debug('Response from external_payment.process : {0}'.format(response)) if response['status'] == 'ext_auth_required': acs_uri = response['acs_uri'] acs_params = response['acs_params'] return response['status'], '{0}?{1}'.format(acs_uri, urllib.parse.urlencode(acs_params)) return response['status'], None
def request_external_payment(instance_id, amount, wallet): logger.debug('Requesting exteranl payment') external_payment = ExternalPayment(instance_id) payment_options = { 'pattern_id': 'p2p', 'instance_id': instance_id, 'to': wallet, 'amount': amount, 'message': 'Оплата бота' } request_id = None response = external_payment.request(payment_options) logger.debug('Reposnse from external_payment.request : {0}'.format(response)) if response['status'] == "success": request_id = response['request_id'] else: logger.error('Failed to get request_id. Reason: {0}'.format(response)) return request_id
def pay(amount, yandex_access): # определяем необходимые переменные для работы с yandex-money yandex_wallet = yandex_access['yandex_wallet'] yandex_client_id = yandex_access['yandex_client_id'] yandex_auth_success_uri = yandex_access['yandex_auth_success_uri'] yandex_auth_fail_uri = yandex_access['yandex_auth_fail_uri'] yandex_instance_id_key = methods.get_yandex_instance_id_key( yandex_client_id) # создаем обьект платежного клиента try: external_payment = ExternalPayment(yandex_instance_id_key) except: return try: payment_request_id = methods.external_payment_request( external_payment, yandex_instance_id_key, yandex_wallet, amount, message=None) except: return if payment_request_id is None: return try: response_external_payment_process = methods.external_payment_process( external_payment, payment_request_id, yandex_auth_success_uri, yandex_auth_fail_uri, yandex_client_id, request_token=False) except: return if response_external_payment_process["key"] is not "access_pay": return redirect_url_for_access_pay = response_external_payment_process["value"] return redirect_url_for_access_pay, payment_request_id, yandex_instance_id_key
def get_ym_params(self, amount, pattern, order_id, success_uri, fail_uri, type=PAYMENT_BY_CARD): """Запрос параметров от ym""" request_options = { "pattern_id": pattern, "sum": amount, "customerNumber": order_id, } if type == PaymentCard.PAYMENT_BY_CARD: ym = ExternalPayment(YandexMoneyConfig.INSTANCE_ID) payment = ym.request(request_options) elif type == PaymentCard.PAYMENT_BY_YM: return False else: return False if payment['status'] == "success": request_id = payment['request_id'] else: return False process_options = { "request_id": request_id, 'ext_auth_success_uri': success_uri, 'ext_auth_fail_uri': fail_uri } result = ym.process(process_options) return dict(url=result['acs_uri'], params=result['acs_params'])
from yandex_money.api import Wallet, ExternalPayment client_id = '6D3FD82502F95CC490D4297F871AB2B934597432FE48B681025011F50C079' response = ExternalPayment.get_instance_id(client_id) if reponse.status == "success": instance_id = response.instance_id; else: # throw exception with reponse->error message # make instance external_payment = ExternalPayment(instance_id); payment_options = { # pattern_id, etc.. } response = external_payment.request(payment_options) if response.status == "success": request_id = response.request_id else: # throw exception with response->message process_options = { "request_id": request_id # other params.. } result = external_payment.process(process_options) # process result according to docs
def background_payment(report_id): """Проводим фоновый платеж""" log = logging.getLogger('payment') report = Report.query.get(report_id) if not report: message = 'Payment: Not found report with id %s' % report_id log.error(message) return message if report.is_payment_busy(): return False wallet = PaymentWallet.get_by_payment_id(report.payment_id) if not wallet: message = 'Payment: Not found wallet with pid %s' % report.payment_id log.error(message) return message PaymentHistory.remove_braked(report.id) history = PaymentHistory.query.filter_by(report_id=report.id).first() if history: return False history = PaymentHistory().from_report(report, wallet) if not history: message = 'Payment: Fail in history add, report_id=%s' % report_id log.error(message) return message card = PaymentCard.get_payment_card(wallet.id) if not card: PaymentTask.set_fail(report_id, wallet) history.delete() message = 'Payment: Not found card, for wallet_id=%s' % wallet.id log.error(message) return False term = Term.query.get(report.term_id) if not term: log.error('Payment: Not found term %s' % report.term_id) firm = Firm.query.get(report.term_firm_id) if not firm: log.error( 'Payment: Not found firm, with term %s' % report.term_id) request_options = { "pattern_id": firm.pattern_id, "sum": float(report.amount) / int(Term.DEFAULT_FACTOR), "customerNumber": history.id, } try: if card.system == PaymentCard.SYSTEM_MPS: history.system = PaymentHistory.SYSTEM_MPS ym = ExternalPayment(YandexMoneyConfig.INSTANCE_ID) payment = ym.request(request_options) elif card.system == PaymentCard.SYSTEM_YANDEX: history.system = PaymentHistory.SYSTEM_YANDEX ym = Wallet(card.token) payment = ym.request_payment(request_options) else: return False except Exception as e: log.error(e) return False else: if not payment or 'request_id' not in payment: PaymentTask.set_fail(report_id, wallet) message = 'Payment: Fail in request payment, report_id %s, request %s' % ( report_id, payment) log.error(message) history.delete() return message history.request_id = payment['request_id'] history.save() return True
def check_status(history_id): log = logging.getLogger('payment') history = PaymentHistory.query.get(history_id) if not history: message = 'Check: Not found history, history_id=%s' % history_id log.error(message) return message wallet = PaymentWallet.query.get(history.wallet_id) if not wallet: message = 'Check: Not found wallet, wallet_id=%s' % wallet.id log.error(message) return message card = PaymentCard.get_payment_card(history.wallet_id) if not card: PaymentTask.set_fail(history.report_id, wallet) message = 'Payment: Not found card, for history_id=%s' % history.id log.error(message) return False try: request_options = dict( request_id=history.request_id, ext_auth_success_uri=YandexMoneyConfig.SUCCESS_URI, ext_auth_fail_uri=YandexMoneyConfig.FAIL_URI, ) if card.system == PaymentCard.SYSTEM_MPS: request_options['money_source_token'] = card.token ym = ExternalPayment(YandexMoneyConfig.INSTANCE_ID) result = ym.process(request_options) elif card.system == PaymentCard.SYSTEM_YANDEX: ym = Wallet(card.token) result = ym.process_payment(request_options) else: return False except Exception as e: log.error(e) return False else: if not result or 'status' not in result: PaymentTask.set_fail(history.report_id, wallet) history.delete() message = 'Check: Fail, report_id=%s, request_id=%s' % \ (history.report_id, history.request_id) log.error(message) log.error(result) return message if result['status'] in ('refused', 'ext_auth_required'): PaymentTask.set_fail(history.report_id, wallet) history.delete() message = 'Check: Fail, report_id=%s, request_id=%s' % \ (history.report_id, history.request_id) log.error(message) log.error(result) return message if result['status'] == 'in_progress': history.status = PaymentHistory.STATUS_IN_PROGRESS history.save() elif result['status'] == 'success': if not 'invoice_id' in result: message = 'Check: Fail, not found invoice_id, history_id=%s' % history_id log.error(message) log.error(result) return message history.invoice_id = result['invoice_id'] history.status = PaymentHistory.STATUS_COMPLETE history.save() PaymentTask.set_success(history.report_id, wallet) report = Report.query.get(history.report_id) report.status = Report.STATUS_COMPLETE report.save() return True