Note that there are some explanatory texts on larger screens.

plurals
  1. POSending POST request with NSURLConnection
    primarykey
    data
    text
    <p>I am sending a post Request with the some parameter in the request body.</p> <p>Here is the iOS code:</p> <pre><code> NSURL *url = [NSURL URLWithString:purchaseURL]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; NSString *requestString = [NSString stringWithString:@"userid=1&amp;couponid=1&amp;Ccnum=7b6cd9a44365752cf39c1edf97890b72&amp;Cctype=Visa&amp;Cvv=434&amp;Billingfirstname=Ankit&amp;Billinglastname=Ankit&amp;Street=some&amp;getCity=mycity&amp;State=mystate&amp;getZip=54355&amp;Ccexpmonth=5&amp;Ccexpyear=2012&amp;Phone=43423342"]; NSMutableData *postData=[NSMutableData data]; [postData appendData:[requestString dataUsingEncoding:NSUTF8StringEncoding]]; [request setHTTPMethod:@"POST"]; [request setValue:[NSString stringWithFormat:@"%d", [postData length]] forHTTPHeaderField:@"Content-Length"]; [request addValue:@"application/json" forHTTPHeaderField:@"Accept"]; NSLog(@"content length %d",[postData length]); [request setValue:@"text/html" forHTTPHeaderField:@"Content-Type"]; [request setHTTPBody:postData]; NSLog(@"purchage url string %@",postData); connection=[[NSURLConnection alloc] initWithRequest:request delegate:self]; NSAssert(self.connection != nil, @"Failure to create URL connection."); </code></pre> <p>This is the php file that will handle the post request. It handles the input and the response is in JSON format.</p> <pre><code> public function restpurchaseAction(Request $request) { $userid = $_POST['userid']; $couponid = $POST['couponid']; //Check that email was verified //Get the user object to compare id to the listed deals id if(!$user-&gt;getEmailVerified()) { return "Error: Must verify email before making a purchase."; } // Initialize error varible $error = ""; //get the currently logged in user $repository = $this-&gt;getDoctrine() -&gt;getRepository('FrontendUserBundle:User'); $user = $repository-&gt;findOneByUsername($userid); //get the coupon that is being purchased $repository = $this-&gt;getDoctrine() -&gt;getRepository('FrontendUserBundle:Coupon'); $coupon = $repository-&gt;findOneById($couponid); //get the number of times the user has purchased and compare to maxper $repository = $this-&gt;getDoctrine() -&gt;getRepository('FrontendUserBundle:Purchase'); //if they have already purchased max, redirect to error page $purchases = $repository-&gt;findByCouponAndUser($coupon-&gt;getId(), $user-&gt;getId()); if(count($purchases) &gt;= $coupon-&gt;getMaxper() &amp;&amp; $coupon-&gt;getMaxper() != 0){ "Error: Coupon purchased maximum number of times."; } //get the users profile $profile = $user-&gt;getProfile(); //get the users address $address = $profile-&gt;getAddress(); //initialize the new purchase $purchase = new Purchase(); //generate the coupon verification code $currentdate = new \DateTime(); $currentdate-&gt;setTimestamp(time()); $interval = new \DateInterval('P'.$coupon-&gt;getExpirationdate().'D'); $currentdate-&gt;add($interval); $validationnumber1 = mt_rand(1000000, 9999999); $validationnumber2 = mt_rand(1000000, 9999999); $validationnumber = $validationnumber1 . $validationnumber2; //set up all the values for the purchase object $purchase-&gt;setValidationnumber($validationnumber); $purchase-&gt;setUser($user); $purchase-&gt;setCoupon($coupon); $purchase-&gt;setValid(true); $purchase-&gt;setValidationattempts(1); $purchase-&gt;setExpirationDate($currentdate); $card = $purchase-&gt;getCard(); $card-&gt;setCcnum($_POST['Ccnum']); $card-&gt;setCctype($_POST['Cctype']); $card-&gt;setCvv($_POST['Cvv']); $card-&gt;setUserid($user-&gt;getId()); //If the form has already been submitted then check if it is a valid CC. //If so, then finish the purchase,else return to the start. if($request-&gt;getMethod() == 'POST'){ //Get post variables for credit card. //Live Mode Credentials define("AUTHORIZENET_API_LOGIN_ID", "**********"); define("AUTHORIZENET_TRANSACTION_KEY", "***************"); //Test Mode Credentials //define("AUTHORIZENET_API_LOGIN_ID", "*************"); //define("AUTHORIZENET_TRANSACTION_KEY", "*****************"); define("AUTHORIZENET_SANDBOX", false); $response="declined"; //Make sure maxlimit hasn't been reached before processing. if($coupon-&gt;getMaxLimit() == 0 || $coupon-&gt;getNumPurchased()&lt;=$coupon- &gt;getMaxLimit()) { $sale = new AuthorizeNetAIM; /*Input coupon information. */ $item_id = $coupon-&gt;getID(); $item_name = substr($coupon-&gt;getCouponname(), 0, 31); $item_description = substr($coupon-&gt;getDescription(), 0,255); $item_quantity = 1; $item_unit_price = $coupon-&gt;getPrice(); $item_taxable = FALSE; $billingaddress = $purchase-&gt;getBillingaddress(); $card = $purchase-&gt;getCard(); /*Input customer information. */ $sale-&gt;setField('first_name', $_POST['Billingfirstname']); $sale-&gt;setField('last_name', $_POST['Billinglastname']); $sale-&gt;setField('email', $user-&gt;getEmail); $sale-&gt;setField('address', $_POST['Street']); $sale-&gt;setField('city', $_POST['getCity']); $sale-&gt;setField('state', $_POST['State']); $sale-&gt;setField('zip', $_POST['getZip']); $sale-&gt;setField('phone', $_POST['Phone']); /*Add information to the sale */ $sale-&gt;addLineItem($item_id, $item_name, $item_description, $item_quantity, $item_unit_price, $item_taxable); $sale-&gt;amount = $coupon-&gt;getPrice(); $sale-&gt;card_num = $_POST['Ccnum']; $sale-&gt;exp_date = $_POST['Ccexpmonth'] . '/' . $_POST['Ccexpyear']; $sale-&gt;setField('card_code', $_POST['Cvv']); $response = $sale-&gt;authorizeAndCapture(); if ($response-&gt;approved) { //Prepersist, make sure to remove CVV, and extra CC digits, only store last 4 $card-&gt;setCvv(""); $lastfour = substr($card-&gt;getCcnum(), -4); $card-&gt;setCcnum($lastfour); $transaction_id = $response-&gt;transaction_id; $purchase-&gt;setId = $transaction_id; $coupon-&gt;incrementNumPurchased(); $em = $this-&gt;get('doctrine')-&gt;getEntityManager(); $em-&gt;persist($purchase); $em-&gt;persist($coupon); $em-&gt;flush(); return "Coupon Purchase Successful!"; }else{ $error = $response-&gt;error_message; } } else{ $error = "Maximum number of coupons already purchased!"; } } return $error; } </code></pre> <p>I am unable to find the error or any issue in the code. I have also tried ASIHttpRequest/ASIFormDataRequest but was unable to get that to work. Is there a problem in the way i am calling the web service?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload