how do I send additional user information in a form after getting stripe token - angular

帅比萌擦擦* 提交于 2020-01-07 06:36:09

问题


So i'm using Stripe to accept payment on a form that also takes other info - name - address etc. I'm using the following library:

https://github.com/gtramontina/stripe-angular

and I have the form set up like this:

<form id="signup-form" ng-submit="processForm()" stripe:form="saveCustomer">
      <!-- our nested state views will be injected here -->
          <div id="form-views" ui-view></div>
</form>

and an example of nested form view (non stripe stuff)

<div class="form-group">
    <label for="name">Name</label>
    <input type="text" class="form-control" name="name" ng-model="formData.name" required>
</div>

and the stripe stuff:

<div class="form-group">
    <label for="card_number">Card Number</label>
      <input type="text" class="form-control" size="20" data-stripe="number" ng-model="formData.card"/>
    </div>

my controller (only relevant function):

// function to process the form
    $scope.saveCustomer = function(status, response) {
            var $form = $('#signup-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 {
                // response contains id and card, which contains additional card details
                var token = response.id;
                var html = '<input type="hidden" name="stripeToken" ng-model=formData.'+token+'/>';
                // Insert the token into the form so it gets submitted to the server
                $form.append($(html));
                // and submit
                $form.get(0).submit();
                console.log(formData);
             }
        }

Here is my question - if the 'saveCustomer' function is the equivalent of the stripeResponseHandler function in the stripe docs, then they are submitting the form after appending the token to it. If i'm adding data into my formData object, how do I add this information into that object. My logic now is that I'm adding the ng-submit attribute linking to another function (processForm) in the controller but this seems rubbish and I can't get the data anyway - How can i get the token and all the other formData data into a single function to send it to the server if I can't see the formData within the saveCustomer function?


回答1:


It dones not match Angular-way to treat DOM directly through using jquery. I recommend you to use model object instead of treating DOM. In your sample code, model object is 'formData'.

If you want to access $scope.formData, the code like below help you to do so.

$scope.formData = {}; 
var formData = $scope.formData;

$scope.saveCustomer = function(status, response) { 
    console.log(formData); // NOT $scope.formData; 
}


来源:https://stackoverflow.com/questions/29134086/how-do-i-send-additional-user-information-in-a-form-after-getting-stripe-token

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!