POST data to php server

I’m trying to send data to php using the http library through post, but when php tries to retrieve it, it can’t find it. Here’s the code

import { Component } from '@angular/core';
import { IonicPage, NavController, NavParams, ToastController } from 'ionic-angular';
import { Stripe } from '@ionic-native/stripe';
import { Http, Headers } from '@angular/http';


 @IonicPage()
 @Component({
 	selector: 'page-pay',
 	templateUrl: 'pay.html',
 })
 export class PayPage {


 	cardinfo: any = {
 		number: '4242424242424242',
 		expMonth: '11',
 		expYear: '20',
 		cvc: '110'
 	}


 	constructor(public navCtrl: NavController, public navParams: NavParams, public stripe: Stripe, public http: Http,
 			public toastCtrl  : ToastController) {
 	}

 	ionViewDidLoad() {
 		console.log('ionViewDidLoad PagoPage');
 	}

 	pay(amount){
 		this.stripe.setPublishableKey('[mytoken]');
 		this.stripe.createCardToken(this.cardinfo).then((token) => {
 			var headers = new Headers();
 			headers.append('Content-Type', 'application/json');
 			var body = {
 				stripetoken: token
 			};

 			var myData = JSON.stringify({stripetoken: token});

 			var url = 'http://192.168.1.2/service/pago.php';
 			this.http.post(url, body, {headers: headers})
 			.subscribe( (data) =>{
 				if(data){
 					console.log(data);
 				}
 			});
 		})
 	}
 }

And my php server code

<?php 
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: Origin, Content-Type, X-Auth-Token');
require_once('./Stripe/init.php');
if ($_POST) {
	\Stripe\Stripe::setApiKey("[myprivatekey]");
	$error = '';
	$success = '';
	try {
		if (!isset($_POST['stripetoken'])){
			throw new Exception("The Stripe Token was not generated correctly");
		}
		\Stripe\Charge::create(array(
			"amount" => 1000,
			"currency" => "CRC",
			"source" => $_POST['stripetoken'],
			"description" => "Charge for daniel.wilson@example.com"
			));
		$success = 'Your payment was successful.';
		echo json_encode($success);
	}
	catch (Exception $e) {
		$error = $e->getMessage();
		echo json_encode($error);
	}
}else{
	echo json_encode("nothing was executed");
}
?>

I keep getting the nothing was executed as response in IONIC

If you’re running this on an iOS device, I believe your endpoint must be https, not http.

@rapropos I’m running this on Chrome with the browser platform in order to be able to use the Stripe native library

In that case you should be able to use the network tab of the developer tools to at least see what is going over the wire, which should at least suggest whether the problem is server- or client-side.

It seems to be sending, but php is not retriving

Somebody else is going to have to help, then, because I’m allergic to PHP.

Your php is wrong. The json body is not in the post value. Try somthing like this

1 Like

why https needed for Ios devices. Same problem with iphone. Working fine with android but not success in ios simulator or devices.

Maybe ?