Welcome Guest, Not a member yet? Register   Sign In
PayPal button callback fails to get controllers return value
#11

OK here is my setup:

$route['payment/create'] = "payment/create_pp_payment";
$route['payment/complete'] = "payment/execute_pp_payment";

and the controller code:

class Payment extends CI_Controller
{
//------------------------------------------------------------------------------------------------------------------------------------
public function __construct()
{
parent::__construct();

//Load payment model - sort out DB queries!
//$this->load->model("payment_model");
}


public function get_pp_token()
{
$clientId = "AXpMC46OpB_ZgadjvEalwFUaz5RLMQqWOnI56Y1m6gTg1mH8WbMTakex5LhZjUx72kcHwUgbSiQ5wt04";
$secret = "EIDXiZLGZlXhYwWUEQTn-NF1A0vjfqL_UUyqVwdb1o553Hg70OO_Ihn51uNj5FPFKAb4FlRZBMOcSSi4";
$headers = array("Accept: application/json", "Accept-Language: en_US" );
$postfields = 'grant_type=client_credentials&content-type=application/x-www-form-urlencoded';

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/oauth2/token");
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
//curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $clientId.":".$secret);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postfields);
curl_setopt ($ch, CURLOPT_SSLVERSION, 6);
$result = curl_exec($ch);
curl_close($ch);

error_log("get_pp_token result from curl: <$result>",3,"/tmp/trace.log");
if(empty($result))die("Error: No response.");
else
{
$json = json_decode($result);
//error_log("json decoded result: <$json>",3,"/tmp/trace.log");
return $json->access_token;
}
}

// curl -v https://api.sandbox.paypal.com/v1/oauth2/token \
// -H "Accept: application/json" \
// -H "Accept-Language: en_US" \
// -u "EOJ2S-Z6OoN_le_KS1d75wsZ6y0SFdVsY9183IvxFyZp:EClusMEUk8e9ihI7ZdVLF5cZ6y0SFdVsY9183IvxFyZp" \
// -d "grant_type=client_credentials" \
// -d "content-type=application/x-www-form-urlencoded"
//
// response:
// {"scope":"https://api.paypal.com/v1/payments/.* https://api.paypal.com/v1/vault/credit-card openid https://ap.paypal.com/v1/developer/.* https://api.paypal.com/v1/vault/credit-card/.*","nonce":"2017-04-29T19:10:06Zp9EFUyyxFiLIQJQ5SNt4NkJmSk9Wnrnp45xM0xGo8k",
// "access_token":"A21AAFQO58FtEWwcuU4vO7IOznMP9n0ce-Kw8PszpkMV6DcA9226GyckGu6RQztNeWzDTMFmsiVLHyvZTtRCb7yboCINrUEA",
// "token_type":"Bearer",
// "app_id":"APP-8KK24973T6066201W",
// "expires_n":31850}
//
// }

//need token to create payment
//create the payment to get payment id to pass to client for express checkout

public function create_pp_payment()
{
//print_r("session-userID: ".$_SESSION['userID']);
//print_r("userID passed as parm to create_pp_payment: ".$userID);

// get transaction token
$token = $this->get_pp_token();

error_log( "token passed to create: <$token>",3,"/tmp/trace.log");
// build transaction object



$data = '{
"intent":"sale",
"redirect_urls":{
"return_url":"https://ikulpic.com/home",
"cancel_url":"https://ikulpic.com/home"
},
"payer":{
"payment_method":"paypal"
},
"transactions":[
{
"amount":{
"total":"1.2",
"currency":"GBP",
"details": {
"subtotal": "1",
"tax": "0.2"
}
},
"description":"Your first ikulpic.",
"custom":"ikulpicId userId"
}
]
}';

// configure curl request
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/payments/payment");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_SSLVERSION, 6);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json",
"Authorization: Bearer ".$token,
"Content-length: ".strlen($data))
);
$result = curl_exec($ch);
curl_close($ch);

error_log("create result from curl: <$result>",3,"/tmp/trace.log");
if(empty($result))die("Error: No response.");
else
{
//$_SESSION['result']=$result;
// might want to note stste = created
// and create_time for our records
// id something like: "PAY-3WY97586JG9277715K3H2SOA"
$json = json_decode($result);
error_log("json decoded result for payID: <$json->id>",3,"/tmp/trace.log");
$rval = json_encode(array("payToken" => $json->id),JSON_FORCE_OBJECT);
error_log("json encoded return value: <$rval>",3,"/tmp/trace.log");
return $rval;
}
}

public function execute_pp_payment()
{
// returned from onAuthorize js call in terms ikulcreate/terms view
// payToken: data.paymentID,
// payerId: data.payerID
$paymentID = $_POST["payToken"];
// "payer_id": "payer_id" as curl -d option in execute payment
$this->data1 = array("payer_id" => $_POST["payerId"]);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/payments/$paymentID/payment");
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($ch, CURLOPT_SSLVERSION, 6);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($this->data1));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Content-Type: application/json",
"Authorization: Bearer ".$token,
"Content-length: ".strlen($this->data1))
);
$result = curl_exec($ch);
curl_close($ch);

if(empty($result))die("Error: No response.");
else
{
$json = json_decode($result);
print_r("decoded json value for state: ".$json->state);
redirect("dashboard");
}
}
}

I have never got to the execute function so far because the button dialog always fails.
Reply
#12

You didn't just post your secret keys for the PayPal API did you?
Reply
#13

sandbox yes
Reply




Theme © iAndrew 2016 - Forum software by © MyBB