Sandbox Paypal Checkout (PHP, Laravel) - php

I'm trying to implement Paypal's checkout to my Laravel Api (connected to an Ionic app) and it gets stuck when in the app I press the button to checkout and it goes to Paypal (so far so good) in the login screen. I found it weird because it wouldn't let me login with my sandbox account or even my real account, the error is the same: "Some of your info isn't correct. Please try again." By opening developer tools, those are the errors I get (see screenshots). I really couldn't find where I'm making a mistake here. Maybe you can help me. Below are the screenshots and the code that makes takes the checkout to Paypal. Let me know if I should add any extra info here! Thanks a lot!
error 1: ,
investigating one of the console errors:
Route::middleware('auth:api')->post('/paypal', function (Request $request) {
$user = $request->user();
$data = $request->all();
$list_products_id = $data;
$products = [];
$total = 0;
$titles = '';
foreach($list_products_id as $key => $value) {
$product = Product::find($value);
if($product){
$products[$key] = $product;
$total += $product->price;
$titles .= $product->title." ";
}
}
if($total){
$paypal = config('app.paypal', "sandbox");
if($paypal == "sandbox"){
$userProvider = 'In my app I have the sandbox business credentials here';
$pwdProvider = 'In my app I have the sandbox business credentials here';
$signProvider = 'In my app I have the sandbox business credentials here';
$url = 'https://api-3t.sandbox.paypal.com/nvp';
$url2 = 'https://www.sandbox.paypal.com/cgi-bin/webscr?%s';
} else {
$userProvider = '';
$pwdProvider = '';
$signProvider = '';
$url = 'https://api-3t.paypal.com/nvp';
$url2 = 'https://www.paypal.com/cgi-bin/webscr?%s';
}
$data = [];
$data['USER'] = $userProvider;
$data['PWD'] = $pwdProvider;
$data['SIGNATURE'] = $signProvider;
$data['METHOD'] = 'SetExpressCheckout';
$data['VERSION'] = '108';
$data['LOCALECODE'] = 'en_US';
$data['L_PAYMENTREQUEST_0_NAME0'] = "Products Orders";
$data['L_PAYMENTREQUEST_0_DESC0'] = $titles;
$data['PAYMENTREQUEST_0_AMT'] = number_format($total, 2).'';
$data['PAYMENTREQUEST_0_CURRENCYCODE'] = 'EUR';
$data['PAYMENTREQUEST_0_PAYMENTACTION'] = 'Sale';
$data['L_PAYMENTREQUEST_0_QTY0'] = '1'; //number of the same product the user is ordering
$data['L_PAYMENTREQUEST_0_AMT0'] = number_format($total, 2).'';
$data['L_BILLINGAGREEMENTDESCRIPTION0'] = $titles;
$data['CANCELURL'] = url('/');
$data['RETURNURL'] = url('/');
// curl
$data = http_build_query($data);
$curl = curl_init();
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($curl);
curl_close($curl);
$nvp = array();
if (preg_match_all('/(?<name>[^\=]+)\=(?<value>[^&]+)&?/', $response, $matches)) {
foreach ($matches['name'] as $offset => $name) {
$nvp[$name] = urldecode($matches['value'][$offset]);
}
}
if(isset($nvp['ACK']) && $nvp['ACK'] == "Success" ){
$query = array(
'cmd' => '_express-checkout',
'token' => $nvp['TOKEN']
);
$redirectURL = sprintf($url2, http_build_query($query));
return ['date'=>$redirectURL];
}else{
return ['status'=>'error purchasing! - 1'];
}
}
echo "total: " . $total;
return ['status'=>'error purchasing! - 2'];
});

so I did a password reset on my sandboxes account and it worked!

Related

how do i integrate paynow zimbabwe api with a localhost system?

Can you please help me to integrate paynow zimbabwe gateway with my localhost system.I have tried to follow their documentation https://developers.paynow.co.zw/docs/quickstart.html but I failed. I want the user to be redirected to the paynow page to pay penalties.Also the result or status must be obtained in order to update the system database. Is it possible to link a localhost system to the paynow api or my system have to be live?. Thank you in advance
<?php
include "./includes/tables_header.php";
include "./includes/db.php";
require_once "./paynow/autoloader.php";
use Paynow\Payments\Paynow;
if(isset($_POST['Paynow']))
{
class Payow{
public function paynows($amount)
{
$siteurl="http://localhost/online_offenceTracking_system/payment1.php?";//substitute with your own return url
define('ps_error', 'Error');
define('ps_ok','Ok');
define('ps_created_but_not_paid','created but not paid');
define('ps_cancelled','cancelled');
define('ps_failed','failed');
define('ps_paid','paid');
define('ps_awaiting_delivery','awaiting delivery');
define('ps_delivered','delivered');
define('ps_awaiting_redirect','awaiting redirect');
define('site_url', $siteurl);
$int_key="###########";//get from paynow.co.zw
$int_id=#######;//get from paynow.co.zw, it should be an intenger
$paymentid="testID1234hs";
$url="https://www.paynow.co.zw/interface/initiatetransaction/?";
$reference=sha1(Paynow\Payments\Paynow::$app->user->identity->email);
$amount=6.25;
$returnurl="http://localhost/online_offenceTracking_system/payment1.php?r=credit/index"; //substitute with your own return urls
$resulturl="http://localhost/online_offenceTracking_system/payment1.php?r=credit/index"; //substitute with your own return urls
$authemail="acmwamuka#gmail.com";//This is the buyer's email address
$additionalinfo="Paying for canteen meals.";
$concat=$int_key.$int_id.$paymentid.$url.$reference.$returnurl.$resulturl.$authemail.$additionalinfo;
$concat=$concat.$int_key;
$values = array('resulturl' => $resulturl,
'returnurl' => $returnurl,
'reference' => $reference,
'amount' => $amount,
'id' => $int_id,
'additionalinfo' => $additionalinfo,
'authemail' => $authemail,
'authphone' => "07777777777",
'status' => 'Message'); //just a simple message
$fields_string = $this->CreateMsg($values,$int_key);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false); //need fixing
$result = curl_exec($ch);
if($result)
{
$msg = $this->ParseMsg($result);
if ($msg["status"] == ps_error){
header("Location: $checkout_url");
exit;
}
else if ($msg["status"] == "Ok"){
$validateHash = $this->CreateHash($msg, $int_key);
if($validateHash != $msg["hash"]){
$error = "Paynow reply hashes do not match : " . $validateHash . " - " . $msg["hash"];
echo $error;
}
else
{
$theProcessUrl = $msg["browserurl"];
//echo $theProcessUrl;
//header("Location: ".$theProcessUrl);
Paynow\Payments\Paynow::$app->response->redirect($theProcessUrl);
$orders_array = array();
}
}
else {
//unknown status or one you dont want to handle locally
$error = "Invalid status from Paynow, cannot continue.";
}
}
else
{
$error = curl_error($ch);
echo $error;
}
//print_r($result);
//close connection
curl_close($ch);
}
public function ParseMsg($msg) {
$parts = explode("&",$msg);
$result = array();
foreach($parts as $i => $value) {
$bits = explode("=", $value, 2);
$result[$bits[0]] = urldecode($bits[1]);
}
return $result;
}
function CreateMsg($values, $MerchantKey){
$fields = array();
foreach($values as $key=>$value) {
$fields[$key] = urlencode($value);
}
$fields["hash"] = urlencode($this->CreateHash($values, $MerchantKey));
$fields_string = $this->UrlIfy($fields);
return $fields_string;
}
public function UrlIfy($fields) {
$delim = "";
$fields_string = "";
foreach($fields as $key=>$value) {
$fields_string .= $delim . $key . '=' . $value;
$delim = "&";
}
return $fields_string;
}
public function CreateHash($values, $MerchantKey){
$string = "";
foreach($values as $key=>$value) {
if( strtoupper($key) != "HASH" ){
$string .= $value;
}
}
$string .= $MerchantKey;
$hash = hash("sha512", $string);
return strtoupper($hash);
}
}}
?>
You can use a free service like ngrok to expose your localhost environment to the world wide web. Just make sure your returnurl and resulturl are using your ngrok address so that Paynow can callback your application.

Read JSON with php from instagram __a=1

original
I want to emphasize first that it is my first script in PHP, so many things can be improved, but for now I just need it to work!
I created this script in php to get public profile information from the public instagram json file located at https://www.instagram.com/{{username}}/?__a=1
trying it locally, everything works correctly, but hosting it on a website file_get_contents($ url) doesn't work (line 29) , I tried to use CURL to read the file, but it doesn't work anyway, it doesn't read the json file correctly, trying to do an echo of what he reads the instagram logo appears on the site screen.
how can I solve it?
update
I just noticed that if I try to make file_get_contents () of a link of any profile www.instagram.com/USERNAME, it gives me the exact same result, it may be that trying to read www.instagram.com/USERNAME/?__a= 1 instagram notice and redirect me to the profile page?
I've tried htmlentities() on the data I receive through file_get_contents ... tatan .. actually the script reads a strange html page that is NOT found at the address I gave it!
<?php
$commentiPost;
$likePost;
$postData;
$image;
$urlprofilo;
$followers;
$username;
$follow;
$like;
$commenti;
function getMediaByUsername($count) {
global $image;
global $commentiPost;
global $likePost;
global $urlprofilo;
global $followers;
global $username;
global $follow;
global $postData;
global $like;
global $commenti;
$uname = htmlspecialchars($_GET["name"]);
$username = strtolower(str_replace(' ','_',$uname));
$url = "https://www.instagram.com/".$username."/?__a=1";
$userinfo = file_get_contents($url);
$userdata = json_decode($userinfo,true);
$user = $userdata['graphql']['user'];
$iteration_url = $url;
if(!empty($user)){
$followers = $user['edge_followed_by']['count'];
$follow = $user['edge_follow']['count'];
$fullname = $user['full_name'];
$username = $user['username'];
$profilepic = $user['profile_pic_url'];
$profilepic = (explode("/",$profilepic));
$urlprofilo = "https://scontent-frt3-1.cdninstagram.com/v/t51.2885-19/s150x150/$profilepic[6]";
$limit = $count;
$tryNext = true;
$found = 0;
while ($tryNext) {
$tryNext = false;
$remote = file_get_contents( $iteration_url );
$response = $remote;
if ($response === false) {
return false;
}
$data = json_decode($response, true);
if ( $data === null) {
return false;
}
$media = $data['graphql']['user']['edge_owner_to_timeline_media'];
foreach ( $media['edges'] as $index => $node ) {
if ( $found + $index < $limit ) {
if (isset($node['node']['is_video']) && $node['node']['is_video'] == true) {
$type = 'video';
} else {
$type = 'image';
}
$like = $like + $node['node']['edge_liked_by']['count'];
$commenti = $commenti + $node['node']['edge_media_to_comment']['count'];
$image[] = array( "<a href=".$node['node']['display_url'].">
<img src=".$node['node']['display_url']." alt="." />
<h3>Like: </strong>".$node['node']['edge_liked_by']['count']."</strong> Commenti: <strong>".$node['node']['edge_media_to_comment']['count']."</strong></h3>
</a>");
$postData[] = array(" '".gmdate("d-m-Y",$node['node']['taken_at_timestamp'])."',");
$likePost[] = array(" ".$node['node']['edge_liked_by']['count'].",");
$commentiPost[] = array(" ".$node['node']['edge_media_to_comment']['count'].",");
}
}
$found += count($media['edges']);
if ( $media['page_info']['has_next_page'] && $found < $limit ) {
$iteration_url = $url . '&max_id=' . $media['page_info']['end_cursor'];
$tryNext = true;
}
}
} else{
}
}
getMediaByUsername( 12);
if(isset($image))
{
$postTot = count($image);
}
else {
$postTot = 0;
}
if($postTot > 0 and $followers > 0){
$ER = round(((($like + $commenti)/$postTot)/$followers)*100, 1);
}
else {
$ER = 0;
}
?>
I belive that is SSL certificate problem. When you modify your function to:
function url_get_contents ( $url ) {
if ( ! function_exists( 'curl_init' ) ){
die( 'The cURL library is not installed.' );
}
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
// curl_setopt( $ch, CURLOPT_SSL_VERIFYHOST, false);
// curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, false);
$output = curl_exec( $ch );
if(curl_errno( $ch )) {
die ('Curl error: ' . curl_error($ch));
}
curl_close( $ch );
return $output;
}
Probably you will see as a result: Curl error: SSL certificate problem: unable to get local issuer certificate.
Add that certificate to your system or uncomment lines with options: CURLOPT_SSL_VERIFYHOSTand CURLOPT_SSL_VERIFYHOST.

Error in creating record in zohocrm contacts using php 7.1

I am trying to create a record in zohocrm. i am using API version2 code.
i recieve this following error which i stated below. I tried stackoverflow for solutions but can't find relevant solution. I tried this Stackoverflow answer Zoho API V2 Update Record. It doesn't work for me. Help me with some solution. i use php version
7.1
Here's the Code i used:
public function createRecord($module, $module_fields)
{
global $HelperObj;
$WPCapture_includes_helper_Obj = new WPCapture_includes_helper_PRO();
$activateplugin = $WPCapture_includes_helper_Obj->ActivatedPlugin;
$moduleslug = $this->ModuleSlug = rtrim(strtolower($module), "s");
$zohoapi = new SmackZohoApi();
$module_field['data'] = array($module_fields);
$module_field['Owner']['id'] = $module_fields['SMOWNERID'];
$fields_to_skip = ['Digital_Interaction_s', 'Solution'];
foreach ($module_fields as $fieldname => $fieldvalue) {
if (!in_array($fieldname, $fields_to_skip)) {
continue;
}
$module_fields[$fieldname] = array();
if (is_string($fieldvalue)) {
array_push($module_fields[$fieldname], $fieldvalue);
} else if (is_array($fieldvalue)) {
array_push($module_fields[$fieldname], $fieldvalue);
}
}
//$fields = json_encode($module_fields);
$attachments = $module_fields['attachments'];
$body_json = array();
$body_json["data"] = array();
array_push($body_json["data"], $module_fields);
$record = $zohoapi->Zoho_CreateRecord($module, $body_json, $attachments);
if ($record['code'] == 'INVALID_TOKEN' || $record['code'] == 'AUTHENTICATION_FAILURE') {
$get_access_token = $zohoapi->refresh_token();
if (isset($get_access_token['error'])) {
if ($get_access_token['error'] == 'access_denied') {
$data['result'] = "failure";
$data['failure'] = 1;
$data['reason'] = "Access Denied to get the refresh token";
return $data;
}
}
$exist_config = get_option("wp_wpzohopro_settings");
$config['access_token'] = $get_access_token['access_token'];
$config['api_domain'] = $get_access_token['api_domain'];
$config['key'] = $exist_config['key'];
$config['secret'] = $exist_config['secret'];
$config['callback'] = $exist_config['callback'];
$config['refresh_token'] = $exist_config['refresh_token'];
update_option("wp_wpzohopro_settings", $config);
$this->createRecord($module, $module_fields);
} elseif ($record['data'][0]['code'] == 'SUCCESS') {
$data['result'] = "success";
$data['failure'] = 0;
} else {
$data['result'] = "failure";
$data['failure'] = 1;
$data['reason'] = "failed adding entry";
}
return $data;
}
API Call Code:
public function Zoho_CreateRecord($module = "Lead",$data_array,$extraParams) {
try{
$apiUrl = "https://www.zohoapis.com/crm/v2/$module";
$fields = json_encode($data_array);
$headers = array(
'Content-Type: application/json',
'Content-Length: ' . strlen($fields),
sprintf('Authorization: Zoho-oauthtoken %s', $this->access_token),
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $apiUrl);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
$result = curl_exec($ch);
curl_close($ch);
$result_array = json_decode($result,true);
if($extraParams != "")
{
foreach($extraParams as $field => $path){
$this->insertattachment($result_array,$path,$module);
}
}
}catch(\Exception $exception){
// TODO - handle the error in log
}
return $result_array;
}
error i got:
Array
(
[data] => Array
(
[0] => Array
(
[code] => INVALID_DATA
[details] => Array
(
[expected_data_type] => jsonarray
[api_name] => Solution_Interest
)
[message] => invalid data
[status] => error
)
)
)
By the details which you gave ,
(1)you said you wish to create "Contacts" , but the url you are using to create contact doesn't seems to create "Contacts" either by
**converting leads to account and contact , or
**directly creating contact
(2)you mentioned module name as "Lead" , try changing it to "Leads".
(3)variables $data_array & $extraParams , doesn't seems to hold any value , they seems to be null.
(4)Here is a help doc. for you
Create Contact
If that still doesn't solve your problem ,you could ask your queries at zoho crm community , people will definitely solve your queries Ask here

How to send variable value from one function to other function in a different page

I'm new to php oop and I wanted to send the variable value from one function to another in a different page. So, currently I have this one function in one page that I want to send the data to the other function in a different page. Is that even possible perhaps?
Here's the first function in sendData.php
public function main($data) {
$settings = new Settings();
$hash_code = md5('standard' . '10068' . '08f94110d5697a2497511594c31704d0' .'3.00');
$std_post = array(
'apitype'=>'standard', //fix value
'apiid'=>'10068', //your api id from ibill
'apiorderid'=>'OPC0001#00000282', //your order id
'apihashcode'=>$hash_code, //generate hash code as above
'apiamount'=>'3.00', //your customer transaction amount
'apiemail'=>'alif4arsenal97#gmail.com'); //your customer email
$callbackJSON = json_encode($std_post);
$url = 'https://ibill.my/merchant/?ng=callback_api'; //link need to send data
$ch = curl_init($url); // where to post
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $callbackJSON);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$headers = array();
$headers[] = "Cache-Control: no-cache";
$headers[] = "Content-Type: application/json";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$results = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
//echo $results;
$objJSON = json_decode($results); //decode json result
//should return 'SUCCESS'
$callback_status = $objJSON->{'callback_status'}; //callback Status
$message = $objJSON->{'message'}; //callback Message
//Refer on statuspage.php
$std_status_code = $objJSON->{'std_status_code'}; //payment status code
$std_status = $objJSON->{'std_status'}; //payment status
$std_order_id = $objJSON->{'std_order_id'}; //your order id
$std_purchase_code = $objJSON->{'std_purchase_code'}; //ibill transaction id
$std_amount = $objJSON->{'std_amount'}; //transaction amount
$std_datepaid = $objJSON->{'std_datepaid'}; //transaction date time
//Hash code for security
$std_hash_code = $objJSON->{'std_hash_code'}; //Hash code
$hash_code = md5('08f94110d5697a2497511594c31704d0'.'10068'.$std_order_id.$std_amount); //hash code format
$data = [
'callback_status' => $callback_status,
'message' => $message,
'std_status_code' => $std_status_code,
'std_status' => $std_status,
'std_order_id' => $std_order_id,
'std_purchase_code' => $std_purchase_code,
'std_amount' => $std_amount,
'std_datepaid' => $std_datepaid,
'std_hash_code' => $std_hash_code,
'hash_code' => $hash_code
];
processPayment($data);
}
Here's the second function in a different that I wanted the data in the first page to be send to which is test.php
public function processPayment($data)
{
if (!isset($data['std_status_code'])) return false;
if (!isset($data['std_hash_code'])) return false;
$settings = new Settings();
$sale_id = (int) substr($data['std_order_id'], 8);
$sale = Sales::get($sale_id);
if (empty($sale)) return false;
if ($sale['status'] == 1) return $sale;
if ($sale['payment_method'] !== 'ibill' || $sale['status'] != 0) return false;
$sale_uid = $sale['uid'];
$sale_method = $sale['method'];
$paid_amount = bcadd($sale['total_amount'], $sale['handling_charge'], 2);
// Verify the data integrity sent by iBill
$hash = md5($settings->ibill_secret_key . $settings->ibill_merchant_id . $data['std_order_id'] . $data['std_amount']);
$payment_processor_status = -1;
$sale_status = 0;
// Check provided hash and status
if ($hash === $data['std_hash_code'] && $data['std_status_code'] == 00) {
$payment_processor_status = 1;
$sale_status = 1;
}
if ($sale_status === 0) {
if ($data['std_status_code'] != 00) {
$data['std_status'] = '<span style="color: red">' . $data['std_status'] . '</span>';
}
if ($data['std_hash_code'] !== $hash) {
$data['std_hash_code'] = '<span style="color: red">' . $data['std_hash_code'] . '</span>';
}
}
// Prepare updated sale data
$now = new DateTime();
$sale = [
'payment_processor_status' => $payment_processor_status,
'payment_processor_data' => $data,
'payment_time' => $now->format('g:i:s A'),
'payment_date' => $now->format('d-m-Y')
];
Sales::update($sale_id, $sale);
if ($sale_status === 1) {
Sales::confirmSale($sale_id, false);
}
return ['uid' => $sale_uid, 'method' => $sale_method];
}
Those functions are class methods, not only functions.
you can use them (or pass data from one to another) by creating instances of their classes. for example something like this:
class one {
public function f1($data) {
// do something
$instance = new two();
$instance->f2($data);
}
}
class two {
public function f2($data) {
// do something else
}
}
I hope it would work for you.

Implement Payum/Laravel recurring payment

I have some issues trying to get this working, I've implemented the checkout express (or seems to be) successfully, but also my system needs subscription option, following this example.
Now, my problem is that in Laravel you cannot simply put some random files, so I'm trying to do it in the correct way, sadly, there is no documentation of the classes and methods including on the library.
I've created some functions within controllers (I don't know if this the right way) the problem I'm facing now is trying to createRecurringPayment() to apply the desired amount of the recurring payment, is the final step I guess.
Thanks for yout help.
app/controllers/PaypalController.php
public function prepareExpressCheckout(){
$storage = $this->getPayum()->getStorage('Payum\Core\Model\ArrayObject');
$details = $storage->createModel();
$details['PAYMENTREQUEST_0_CURRENCYCODE'] = 'USD';
$details['PAYMENTREQUEST_0_AMT'] = 1.23;
$storage->updateModel($details);
$captureToken = $this->getTokenFactory()->createCaptureToken('paypal_es', $details, 'payment_done');
$details['RETURNURL'] = $captureToken->getTargetUrl();
$details['CANCELURL'] = $captureToken->getTargetUrl();
$storage->updateModel($details);
return \Redirect::to($captureToken->getTargetUrl());
}
public function prepareSubscribe(){
$storage = $this->getPayum()->getStorage('Payum\Core\Model\ArrayObject');
$details = $storage->createModel();
$details['PAYMENTREQUEST_0_AMT'] = 0;
$details['L_BILLINGTYPE0'] = Api::BILLINGTYPE_RECURRING_PAYMENTS;
$details['L_BILLINGAGREEMENTDESCRIPTION0'] = "SuscripciĆ³n por X meses";
$details['NOSHIPPING'] = 1;
$storage->updateModel($details);
$captureToken = $this->getTokenFactory()->createCaptureToken('paypal_es', $details, 'payment_done');
$storage->updateModel($details);
return \Redirect::to($captureToken->getTargetUrl());
}
public function createRecurringPayment(){
$payum_token = Input::get('payum_token');
$request = \App::make('request');
$request->attributes->set('payum_token', $payum_token);
$token = ($request);
//$this->invalidate($token);
$agreementStatus = new GetHumanStatus($token);
$payment->execute($agreementStatus);
if (!$agreementStatus->isSuccess()) {
header('HTTP/1.1 400 Bad Request', true, 400);
exit;
}
$agreementDetails = $agreementStatus->getModel();
$storage = $this->getPayum()->getStorage('Payum\Core\Model\ArrayObject');
$recurringPaymentDetails = $storage->createModel();
$recurringPaymentDetails['TOKEN'] = $agreementDetails['TOKEN'];
$recurringPaymentDetails['DESC'] = 'Subscribe to weather forecast for a week. It is 0.05$ per day.';
$recurringPaymentDetails['EMAIL'] = $agreementDetails['EMAIL'];
$recurringPaymentDetails['AMT'] = 0.05;
$recurringPaymentDetails['CURRENCYCODE'] = 'USD';
$recurringPaymentDetails['BILLINGFREQUENCY'] = 7;
$recurringPaymentDetails['PROFILESTARTDATE'] = date(DATE_ATOM);
$recurringPaymentDetails['BILLINGPERIOD'] = Api::BILLINGPERIOD_DAY;
$payment->execute(new CreateRecurringPaymentProfile($recurringPaymentDetails));
$payment->execute(new Sync($recurringPaymentDetails));
$doneToken = $this->createToken('paypal_es', $recurringPaymentDetails, 'payment_done');
return \Redirect::to($doneToken->getTargetUrl());
}
app/routes.php
Route::get('/payment', array('as' => 'payment', 'uses' => 'PaymentController#payment'));
Route::get('/payment/done', array('as' => 'payment_done', 'uses' => 'PaymentController#done'));
Route::get('/payment/paypal/express-checkout/prepare', array('as' => 'paypal_es_prepare', 'uses' => 'PaypalController#prepareExpressCheckout'));
Route::get('/payment/paypal/subscribe/prepare', array('as' => 'paypal_re_prepare', 'uses' => 'PaypalController#prepareSubscribe'));
Route::get('/payment/paypal/subscribe/create', array('as' => 'payment_create', 'uses' => 'PaypalController#createRecurringPayment'));
I have found the problem. It is with the parameters we pass to the create recurring payment function. Here are functions for agreement and payment creation. It should work fine.
<?php
namespace App\Http\Controllers;
use Payum\Core\Request\GetHumanStatus;
use Payum\LaravelPackage\Controller\PayumController;
use Payum\Paypal\ExpressCheckout\Nvp\Api;
use Payum\Core\Request\Sync;
use Payum\Paypal\ExpressCheckout\Nvp\Request\Api\CreateRecurringPaymentProfile;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
class PayPalController extends PayumController {
public function prepareSubscribeAgreement() {
$storage = $this->getPayum()->getStorage('Payum\Core\Model\ArrayObject');
$details = $storage->create();
$details['PAYMENTREQUEST_0_AMT'] = 0;
$details['L_BILLINGTYPE0'] = Api::BILLINGTYPE_RECURRING_PAYMENTS;
$details['L_BILLINGAGREEMENTDESCRIPTION0'] = "Weather subscription";
//$details['NOSHIPPING'] = 1;
$storage->update($details);
$captureToken = $this->getPayum()->getTokenFactory()->createCaptureToken('paypal_ec', $details, 'paypal_subscribe');
return \Redirect::to($captureToken->getTargetUrl());
}
public function createSubscribePayment(Request $request) {
$request->attributes->set('payum_token', $request->input('payum_token'));
$token = $this->getPayum()->getHttpRequestVerifier()->verify($request);
$gateway = $this->getPayum()->getGateway($token->getGatewayName());
$agreementStatus = new GetHumanStatus($token);
$gateway->execute($agreementStatus);
if (!$agreementStatus->isCaptured()) {
header('HTTP/1.1 400 Bad Request', true, 400);
exit;
}
$agreement = $agreementStatus->getModel();
$storage = $this->getPayum()->getStorage('Payum\Core\Model\ArrayObject');
$recurringPayment = $storage->create();
$recurringPayment['TOKEN'] = $agreement['TOKEN'];
$recurringPayment['PAYERID'] = $agreement['PAYERID'];
$recurringPayment['PROFILESTARTDATE'] = date(DATE_ATOM);
$recurringPayment['DESC'] = $agreement['L_BILLINGAGREEMENTDESCRIPTION0'];
$recurringPayment['BILLINGPERIOD'] = Api::BILLINGPERIOD_DAY;
$recurringPayment['BILLINGFREQUENCY'] = 7;
$recurringPayment['AMT'] = 0.05;
$recurringPayment['CURRENCYCODE'] = 'USD';
$recurringPayment['COUNTRYCODE'] = 'US';
$recurringPayment['MAXFAILEDPAYMENTS'] = 3;
$gateway->execute(new CreateRecurringPaymentProfile($recurringPayment));
$gateway->execute(new Sync($recurringPayment));
$captureToken = $this->getPayum()->getTokenFactory()->createCaptureToken('paypal_ec', $recurringPayment, 'payment_done');
return \Redirect::to($captureToken->getTargetUrl());
}
public function done(Request $request) {
/** #var Request $request */
//$request = \App::make('request');
$request->attributes->set('payum_token', $request->input('payum_token'));
$token = $this->getPayum()->getHttpRequestVerifier()->verify($request);
$gateway = $this->getPayum()->getGateway($token->getGatewayName());
$gateway->execute($status = new GetHumanStatus($token));
return \Response::json(array(
'status' => $status->getValue(),
'details' => iterator_to_array($status->getFirstModel())
));
}
}
The routes:
Route::get('paypal/agreement', 'PayPalController#prepareSubscribeAgreement');
Route::get('paypal/subscribe', [
'as' => 'paypal_subscribe',
'uses' => 'PayPalController#createSubscribePayment'
]);
Route::get('paydone', [
'as' => 'payment_done',
'uses' => 'PayPalController#done'
]);
Simply open www.example.com/paypal/agreement and it should take you to PayPal
In my project I used the following library and it helped me alot:
https://github.com/amirduran/duranius-paypal-rest-api-php-library
Here are some features:
Easy to install - Just one file
Library is implemented as PHP class
It supports Recurring Payments It supports ExpressCheckout payments
All available PayPal API methods are wrapped in belonging methods
Well documented
Here is my Pay Pal REST API code.
//Request Perms
$cardtype = $request->cardtype;
$account_number = $request->cardnumber;
$expire_date =$request->expire_date;
$cvv = $request->cvv;
$plan_id =$request->plan_id;
$amount = $request->amount;
$userid = $request->user_id;
$payment_type = $request->plan_type;
//Genrate tokens
$ch = curl_init();
$clientId ='Your Client ID';
$clientSecret= 'Your Secret ID';
//you get Clientid and clientSecret
https://developer.paypal.com/developer/applications
curl_setopt($ch, CURLOPT_URL, "https://api.sandbox.paypal.com/v1/oauth2/token");
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $clientId.":".$clientSecret);
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
$result = curl_exec($ch);
if(empty($result))die("Error: No response.");
else
{
$json = json_decode($result);
}
curl_close($ch);
$product_id = '';
//you can create payment plan this link
https://www.sandbox.paypal.com/billing/plans
if ($plan_id == 1) {
$product_id = 'your plan id';
}elseif ($plan_id == 2) {
$product_id = 'your plan id';
}
$ch = curl_init();
$payment_data = '{
"plan_id":"'.$product_id.'",
"start_time":"'.gmdate("Y-m-d\TH:i:s\Z",strtotime("+1 day")).'",
"shipping_amount":{
"currency_code":"USD",
"value":"'.$amount.'"
},
"subscriber":{
"name":{
"given_name":"",
"surname":""
},
"email_address":"'.$users->email.'",
"shipping_address":{
"name":{
"full_name":""
},
"address":{
"address_line_1":"",
"address_line_2":"",
"admin_area_2":"",
"admin_area_1":"",
"postal_code":"",
"country_code":"US"
}
},
"payment_source":{
"card":{
"number":"'.$account_number.'",
"expiry":"'. $expiry_date.'",
"security_code":"'.$cvv.'",
"name":"",
"billing_address":{
"address_line_1":"",
"address_line_2":"",
"admin_area_1":"",
"admin_area_2":"",
"postal_code":"",
"country_code":"US"
}
}
}
}
}';
curl_setopt($ch, CURLOPT_URL, 'https://api.sandbox.paypal.com/v1/billing/subscriptions');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payment_data);
$headers = array();
$headers[] = 'Accept: application/json';
$headers[] = 'Authorization: Bearer '.$json->access_token.'';
$headers[] = 'Paypal-Request-Id: SUBSCRIPTION-'. rand() .'';
$headers[] = 'Prefer: return=representation';
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
$payment_id = json_decode($result);
$data =$headers[2];
$subid = substr($data, strpos($data, ":") + 2);
//save data in database
$payment = new Subscription();
$payment->userid=$userid;
$payment->plan_id=$plan_id;
$payment->price=$amount;
$payment->sub_id=$subid;
$payment->transaction_id=$payment_id->id;
$payment->payment_type='Paypal';
$payment->charge=$paypal_charge;
$payment->plan_type=$plan_type;
$payment->subscription_startdate= $subscription_startdate;
$payment->subscription_enddate= $subscription_enddate;
$payment->subscription_status= 'active';
$payment->save();
return response()->json(['status' => true,'message'=>'Payment has been successfully Done','data'=>$payment]);
It's working fine for me.

Categories