code_legend
code_legend

Reputation: 3285

Working with payment

I have a dilema, quite a big one.

I am working stripe as my payment gateway and it works in a 2 way process.

  1. Collect billing info which generated a token
  2. charge the client using token

problem is that token can only be used once and on step 1 i am unable to verify if client has sufficient fund, so if it doesn't then the card gets declined, and the problem is that i can't reused the token so i cant go back to the customer, and ask for their details again. So my question is how to i verify without charging the client that they have sufficient fund when generating the token.

below is a quick look on how the code is generated:

function onSubmit() {
    var $form = $('#payment-form'); // TODO: give your html-form-tag an "id" attribute and type this id in this line. IMPORTANT: Don't replace the '#'!

    // Disable the submit button to prevent repeated clicks
  // TODO: give your html-submit-input-tag an "id" attribute

    Stripe.card.createToken($form, stripeResponseHandler);
}

update:

   // This identifies your website in the createToken call below
  Stripe.setPublishableKey('CODE');

    var appendedStripeToken = false;

var stripeResponseHandler = function(status, response) {
    var $form = $('#payment-form');

    if (response.error) {
        // Show the errors on the form
        $form.find('.payment-errors').text(response.error.message);
                $form.find('button').prop('disabled', false);

    } else {
        // token contains id, last4, and card type
        var token = response.id;
        handleCall(token);
    }
};

function handleCall(token) { 
   var $form = $('#payment-form');
    if (!appendedStripeToken) { 
        // Insert the token into the form so it gets submitted to the server
$form.append($('<input type="hidden" id="courseToken" name="stripeToken" />').val(token));
        appendedStripeToken = true; 
        phpCall(); 

    } 
}

function onSubmit() {
    var $form = $('#payment-form'); // TODO: give your html-form-tag an "id" attribute and type this id in this line. IMPORTANT: Don't replace the '#'!

    // Disable the submit button to prevent repeated clicks
  // TODO: give your html-submit-input-tag an "id" attribute

    Stripe.card.createToken($form, stripeResponseHandler);
}


function phpCall() {
 if( appendedStripeToken === true ){
   $.ajax({
    type: "POST",
    data: {run: true, priceFinal: $('#priceFinal').val(), courseProvider: $('#courseProvider').val(), 
    userEmail: $('#userEmail').val(), courseDelivery: $('#courseDelivery').val(), courseTitle: $('#courseTitle').val(), courseDate: $('#courseDate').val(), 
    courseToken: $('#courseToken').val(), cardname: $('#billingcardName').val(), finalCoupon: $('#finalCoupon').val(), couponDiscount: $('#couponDiscount').val() },
    url: 'functions/paymentEmail.php',
    success: function (response) {//response is value returned from php (for    your example it's "bye bye"
                  $('#payment-form').prop('disabled', true); // TODO: give your html-submit-input-tag an "id" attribute

window.location = response;
    }
   });
 }
} 

Upvotes: 1

Views: 225

Answers (1)

Pedro Lobito
Pedro Lobito

Reputation: 99011

From stripe-payments api documentation :

account_balance integer

Current balance, if any, being stored on the customer’s account. If negative, the customer has credit to apply to the next invoice. If positive, the customer has an amount owed that will be added to the next invoice. The balance does not refer to any unpaid invoices; it solely takes into account amounts that have yet to be successfully applied to any invoice. This balance is only taken into account for recurring charges.

You need to retrieve the customer object and check the balance element.

Request Endpoint:

POST https://api.stripe.com/v1/customers

Example Request:

curl https://api.stripe.com/v1/customers \
   -u sk_test_BQokikJOvBiI2HlWgH4olfQ2: \
   -d description="Customer for [email protected]" \
   -d source=tok_15tOII2eZvKYlo2CIU6PJLtY

Example Response:

Stripe\Customer JSON: {
  "object": "customer",
  "created": 1429505322,
  "id": "cus_65iGlrQ2E95Vct",
  "livemode": false,
  "description": null,
  "email": "[email protected]",
  "delinquent": false,
  "metadata": {
  },
  "subscriptions": {
    "object": "list",
    "total_count": 0,
    "has_more": false,
    "url": "/v1/customers/cus_65iGlrQ2E95Vct/subscriptions",
    "data": [

    ]
  },
  "discount": null,
  "account_balance": 0,
   etc...

This is what you need:

"account_balance": 0

Upvotes: 2

Related Questions