示例#1
0
def donate(request):
    if request.method == 'GET':
        rec_id = request.GET['user']
        user = get_object_or_404(User, id=rec_id)
        return render(request, 'donate-page.html', {
            'id': user.id,
            'name': user.full_name
        })

    if request.method == 'POST':
        amount = int(request.POST['amount'])
        token = request.POST['token']
        userid = request.POST['id']
        if amount == '' or token == '':
            return render(request, 'donate-page.html',
                          {'BlankError': 'All fields are required'})
        else:
            api = sdk.get_api(
                secret_key='sk_test_f2a38861-4256-413b-a95c-4c3822b8f508')

            try:
                payment = api.payments.request(
                    source={
                        'token': token,
                    },
                    amount=amount * 100,
                    currency=sdk.Currency.USD,
                )

                if payment.approved == True and payment.status == "Authorized":
                    try:

                        user_bal = UserBalance.objects.get(
                            user__id=int(userid))
                        curr_bal = user_bal.balance
                        new_bal = float(curr_bal + int(amount))
                        user_bal.balance = new_bal
                        user_bal.save()
                        return render(request, 'donate-page.html',
                                      {'credited': 'Donation successful !'})
                    except UserBalance.DoesNotExist:
                        UserBalance.objects.create(user_id=userid,
                                                   balance=int(amount))
                        return render(request, 'donate-page.html',
                                      {'credited': 'Donation successful !'})

                else:
                    return render(
                        request, 'donate-page.html',
                        {'NoPayment': 'your card can not be authorized !'})
            except sdk.errors.CheckoutSdkError as e:

                return render(
                    request, 'donate-page.html',
                    {'PaymentError': 'Payment Failed ! Try again later'})
示例#2
0
def add_fund(request):
    if request.method == 'GET':
        return render(request, 'dashboard/add-fund.html')

    if request.method == 'POST':
        amount = int(request.POST['amount'])
        token = request.POST['token']
        if amount == '' or token == '':
            return render(request, 'dashboard/add-fund.html',
                          {'BlankError': 'All fields are required'})
        else:
            api = sdk.get_api(
                secret_key='sk_test_f2a38861-4256-413b-a95c-4c3822b8f508')

            try:
                payment = api.payments.request(
                    source={
                        'token': token,
                    },
                    amount=amount * 100,
                    currency=sdk.Currency.USD,
                )

                if payment.approved == True and payment.status == "Authorized":
                    try:
                        user_bal = UserBalance.objects.get(user=request.user)
                        curr_bal = user_bal.balance
                        new_bal = float(curr_bal + amount)
                        user_bal.balance = new_bal
                        user_bal.save()
                        return render(
                            request, 'dashboard/add-fund.html',
                            {'credited': 'Fund added successfully !'})
                    except UserBalance.DoesNotExist:
                        UserBalance.objects.create(user=request.user,
                                                   balance=amount)
                        return render(
                            request, 'dashboard/add-fund.html',
                            {'credited': 'Fund added successfully !'})

                else:
                    return render(
                        request, 'dashboard/add-fund.html',
                        {'NoPayment': 'your card can not be authorized !'})
            except sdk.errors.CheckoutSdkError as e:
                return render(
                    request, 'dashboard/add-fund.html',
                    {'PaymentError': 'Payment Failed ! Try again later'})
示例#3
0
import checkout_sdk as sdk

api = sdk.get_api(secret_key='<your secret key>')

try:
    action = api.payments.capture('pay_...', amount=100, reference='CAPTURE')

    print(action.id)
    print(action.get_link('payment').href)
except sdk.errors.CheckoutSdkError as e:
    print(
        '{0.http_status} {0.error_type} {0.elapsed} {0.request_id}'.format(e))
示例#4
0
    def test_init_checkout_api(self):
        api = sdk.get_api()

        self.assertTrue(isinstance(api.payments, PaymentsClient))
        self.assertTrue(isinstance(api.tokens, TokensClient))
        self.assertTrue(isinstance(api.payments._http_client, HttpClient))