We are working with docusign Composite Template API with PHP. We Tried to add a custom document with base64 encoded format with signHereTabs object with values. We are able to create envelope and views for recipient successfully.But the issue is it's not generating signHereTabs which we are tried for recipient. Can you please help us to resolve this issue?
Sample Request in json to create envelope:
{
"status": "sent",
"compositeTemplates": [
{
"compositeTemplateId": "1",
"inlineTemplates": [
{
"sequence": "1",
"recipients": {
"signers": [
{
"clientUserId": "1000",
"name": "Full Name",
"email": "my email",
"recipientId": "1",
"roleName": "Sender",
"tabs": {
"signHereTabs": [
{
"anchorString": "\/sig1\/",
"anchorUnits": "pixels",
"anchorXOffset": "20",
"anchorYOffset": "10"
}
]
}
}
]
},
"customFields": {
"textCustomFields": [
{
"name": "MyOwnField",
"required": "true",
"show": "true",
"value": "MyValue"
}
]
}
}
],
"document": {
"documentBase64": "<base64 encoded string>",
"documentId": "1",
"fileExtension": "pdf",
"name": "Agreement.pdf",
"transformPdfFields": false
}
}
]
}
View Recipient sample request:
{
"document": {
"documentBase64": "<base64 encoded string>",
"documentId": "1",
"fileExtension": "pdf",
"name": "Agreement.pdf",
"transformPdfFields": false
}
}
The fields (tabs) need to be a part of the signers object.
Here's a working example from the API Request Builder:
{
"emailSubject": "Please sign the attached document",
"status": "sent",
"compositeTemplates": [
{
"compositeTemplateId": "1",
"document": {
"filename": "anchorfields.pdf",
"name": "Example document",
"fileExtension": "pdf",
"documentId": "1"
},
"inlineTemplates": [
{
"sequence": "1",
"recipients": {
"signers": [
{
"email": "signer_email#example.com",
"name": "Signer's name",
"recipientId": "1",
"clientUserId": "1000",
"tabs": {
"signHereTabs": [
{
"anchorString": "/sig1/",
"anchorXOffset": "20",
"anchorUnits": "pixels"
}
]
}
}
]
}
}
]
}
]
}
Here it is with the PHP SDK (auto-generated by the API Request Builder):
<?php # DocuSign Builder example. Generated: Sat, 03 Sep 2022 18:34:12 GMT
# DocuSign Ⓒ 2022. MIT License -- https://opensource.org/licenses/MIT
# #see DocuSign Developer Center
require_once ('vendor/autoload.php');
require_once ('vendor/docusign/esign-client/autoload.php');
# Note: the access_token is for testing and is temporary. It is only good for 8 hours from the time you
# authenticated with API Request Builder.
const base_uri = 'https://demo.docusign.net/';
const access_token = '';
const account_id = '';
const document_directory = '.'; # The directory with your documents, relative to this script's directory
#
function sendDocuSignEnvelope() {
$docs_path = getcwd() . '/' . document_directory . '/';
$document1 = new \DocuSign\eSign\Model\Document([
'document_id' => "1",
'file_extension' => "pdf",
'document_base64' => base64_encode(file_get_contents($docs_path.'anchorfields.pdf')), # filename is anchorfields.pdf
'name' => "Example document"
]);
$sign_here_tab1 = new \DocuSign\eSign\Model\SignHere([
'anchor_string' => "/sig1/",
'anchor_units' => "pixels",
'anchor_x_offset' => "20"
]);
$sign_here_tabs1 = [$sign_here_tab1];
$tabs1 = new \DocuSign\eSign\Model\Tabs([
'sign_here_tabs' => $sign_here_tabs1
]);
$signer1 = new \DocuSign\eSign\Model\Signer([
'client_user_id' => "1000",
'email' => "signer_email#example.com",
'name' => "Signer's name",
'recipient_id' => "1",
'tabs' => $tabs1
]);
$signers1 = [$signer1];
$recipients1 = new \DocuSign\eSign\Model\Recipients([
'signers' => $signers1
]);
$inline_template1 = new \DocuSign\eSign\Model\InlineTemplate([
'recipients' => $recipients1,
'sequence' => "1"
]);
$inline_templates1 = [$inline_template1];
$composite_template1 = new \DocuSign\eSign\Model\CompositeTemplate([
'composite_template_id' => "1",
'document' => $document1,
'inline_templates' => $inline_templates1
]);
$composite_templates1 = [$composite_template1];
$envelope_definition = new \DocuSign\eSign\Model\EnvelopeDefinition([
'composite_templates' => $composite_templates1,
'email_subject' => "Please sign the attached document",
'status' => "sent"
]);
try {
$config = new \DocuSign\eSign\Configuration();
$config->setHost(base_uri . 'restapi');
$config->addDefaultHeader('Authorization', 'Bearer ' . access_token);
$api_client = new \DocuSign\eSign\Client\ApiClient($config);
$envelope_api = new \DocuSign\eSign\Api\EnvelopesApi($api_client);
$result = $envelope_api->createEnvelope(account_id, $envelope_definition);
$envelope_id = $result->getEnvelopeId();
printf("\nEnvelope status: %s. Envelope ID: %s\n", $result->getStatus(), $result->getEnvelopeId());
return $envelope_id;
} catch (Exception $e) {
printf ("\n\nException from createEnvelope!\n%s", $e->getMessage());
if ($e instanceof DocuSign\eSign\Client\ApiException) {
printf ("\nAPI error information: \n%s", $e->getResponseBody());
}
return FALSE;
}
}
function recipientView ($envelope_id) {
$recipient_view_request = new \DocuSign\eSign\Model\RecipientViewRequest([
'authentication_method' => "None",
'client_user_id' => "1000",
'email' => "signer_email#example.com",
'return_url' => "https://docusign.com",
'user_name' => "Signer's name"
]);
if (!$recipient_view_request || !$envelope_id) {return;}
try {
$config = new \DocuSign\eSign\Configuration();
$config->setHost(base_uri . 'restapi');
$config->addDefaultHeader('Authorization', 'Bearer ' . access_token);
$api_client = new \DocuSign\eSign\Client\ApiClient($config);
$envelope_api = new \DocuSign\eSign\Api\EnvelopesApi($api_client);
$result = $envelope_api->createRecipientView(account_id, $envelope_id,
$recipient_view_request);
print ("\nCreate recipient view succeeded.");
printf ("Open the signing ceremony's long URL within 5 minutes: \n%s\n\n", $result->getUrl());
} catch (Exception $e) {
printf ("\n\nException from createRecipientView!\n%s", $e->getMessage());
if ($e instanceof DocuSign\eSign\Client\ApiException) {
printf ("\nAPI error information: \n%s", $e->getResponseBody());
}
}
}
# The mainline
$envelope_id = sendDocuSignEnvelope();
recipientView($envelope_id);
print("Done.\n");
Related
can anybody give example of how to code POST, PUT, Delete Hateoas PHP in Codeigniter, I have 2 tables, author(AuthorID is primary key, BookID I foreign key), book(BookID is primary key)
I have Index_get
public function index_get(){
$id = $this->get('AuthorID');
$author =[];
if ($id ==""){
$data = $this->db->get("author")->result();
foreach($data as $row=>$key):
$author[]=["AuthorID"=>$key->AuthorID,
"AuthorName"=>$key->AuthorName,
"_link"=>[(object)["href"=>"book/{$key->BookID}",
"rel"=>"book",
"type"=>"GET"]],
"BirthPlace"=>$key->BirthPlace,
"Email"=>$key->Email,
"Education"=>$key->Education,
"Phone"=>$key->Phone,
"Address"=>$key->Address,
"Nationality"=>$key->Nationality
];
endforeach;
}
else {
$this->db->where("AuthorID","$id");
$data = $this->db->get("author")->result();
}
$result = ["took"=>$_SERVER["REQUEST_TIME_FLOAT"],
"code"=>200,
"message"=>"Show Data Success",
"author"=>$author];
$this->response($result, 200);
}
and it success
"took": 1637045427.669679,
"code": 200,
"message": "Show Data Success",
"author": [
{
"AuthorID": "A1",
"AuthorName": "Jack Dane",
"_link": [
{
"href": "book/1",
"rel": "book",
"type": "GET"
}
],
"BirthPlace": "London",
"Email": "Jackdane#gmail.com",
"Education": "Bachelor At Univercity Of Canada",
"Phone": "09199191",
"Address": "4457 Grey Fox Farm Road, Houston, Texas",
"Nationality": "America"
},
{
"AuthorID": "A2",
"AuthorName": "Michael Dendy",
"_link": [
{
"href": "book/2",
"rel": "book",
"type": "GET"
}
],
"BirthPlace": "New York",
"Email": "Michaeldendy#gmail.com",
"Education": "Bachelor At Univercity Of California",
"Phone": "09188181",
"Address": "2602 Farnum Road, New York, New York",
"Nationality": "America"
},
{
"AuthorID": "A3",
"AuthorName": "Dumando",
"_link": [
{
"href": "book/3",
"rel": "book",
"type": "GET"
}
],
"BirthPlace": "Jakarta",
"Email": "Dumando#gmail.com",
"Education": "Bachelor At Indonesia Univercity",
"Phone": "012929919",
"Address": "Jl Cendrawaih 919 Jakarta",
"Nationality": "Indonesia"
},
{
"AuthorID": "A4",
"AuthorName": "dono",
"_link": [
{
"href": "book/4",
"rel": "book",
"type": "GET"
}
],
"BirthPlace": "Beijing",
"Email": "dono#gmail.com",
"Education": "Bachelor at ITS",
"Phone": "0919191",
"Address": "Jl. MTHaryono",
"Nationality": "china"
}
]
}
But when I try to POST in postman it gives me the error "Column 'AuthorID' cannot be null". AuthorID is the primary key.it give me the error "Column 'AuthorID' cannot be null" but AuthorID already has input, I just wanna change BirthPlace.
function index_post() {
$author = array(
'AuthorID' => $this->post('AuthorID'),
'AuthorName' => $this->post('AuthorName'),
'BookID' => $this->post('BookID'),
'BirthPlace' => $this->post('BirthPlace'),
'Email' => $this->post('Email'),
'Education' => $this->post('Education'),
'Phone' => $this->post('Phone'),
'Address' => $this->post('Address'),
'Nationality' => $this->post('Nationality'));
$insert = $this->db->insert('author', $author);
if ($insert) {
$result = ["took"=>$_SERVER["REQUEST_TIME_FLOAT"],
"code"=>200,
"message"=>"Add Data Success",
"author"=>$author];
$this->response($result, 200);
} else {
$result = ["took"=>$_SERVER["REQUEST_TIME_FLOAT"],
"code"=>200,
"message"=>"Failed Add Data",
"author"=>$author];
$this->response($result, 502);
}
}
when I try PUT in postman it give me success message but the data is not update. AuthorID is null, BoodID is null, i already input AuthorID, BookID in postman and still data cannot update,
function index_put() {
$id = $this->put('AuthorID');
$author = array(
'AuthorID' => $this->put('AuthorID'),
'AuthorName' => $this->put('AuthorName'),
'BookID' => $this->put('BookID'),
'BirthPlace' => $this->put('BirthPlace'),
'Email' => $this->put('Email'),
'Education' => $this->put('Education'),
'Phone' => $this->put('Phone'),
'Address' => $this->put('Address'),
'Nationality' => $this->put('Nationality'));
$this->db->where('AuthorID', $id);
$update = $this->db->update('author', $author);
if ($update) {
$result = ["took"=>$_SERVER["REQUEST_TIME_FLOAT"],
"code"=>200,
"message"=>"Data Update Success",
"author"=>$author];
$this->response($result, 200);
} else {
$result = ["took"=>$_SERVER["REQUEST_TIME_FLOAT"],
"code"=>200,
"message"=>"Failed Update Data",
"author"=>$author];
$this->response($result, 502);
}
}
result index_put
{
"took": 1637045848.425828,
"code": 200,
"message": "Data Update Success",
"author": {
"AuthorID": null,
"AuthorName": "Jack Dane",
"BookID": null,
"BirthPlace": "Jakarta",
"Email": "Jackdane#gmail.com",
"Education": "Bachelor At Univercity Of Canada",
"Phone": "09199191",
"Address": "4457 Grey Fox Farm Road, Houston, Texas",
"Nationality": "America"
}
}
and when I try DELETE it gives me "status": "Success Delete Data" but the data is not removed when I check in PHPMyAdmin.
function index_delete() {
$id = $this->delete('AuthorID');
$this->db->where('AuthorID', $id);
$delete = $this->db->delete('author');
if ($delete) {
$this->response(array('status' => 'Success Delete Data'), 200);
}
else{
$this->response(array('status' => 'fail Delete Data', 502));
}
}
I'm trying to update user through api, and this is the function.
PUT request http://20a11140.ngrok.io/api/userregister/24 for user with id 24 for example.
I'm using postman for testing
When I pass the edited details, with name inclusive
public function update(Request $request, $id)
{
$validator = Validator::make($request->all(), [
'name' => 'required|string|max:255',
'email' => 'required|string|email|max:255|unique:users',
'password' => 'string|min:6|confirmed',
'phone' => 'string|min:6',
'Age' => 'string',
'Blood' => 'string',
'Gender' => 'string',
'Height' => 'string',
'Weight' => 'string',
'record' => 'string'
]);
if($validator->fails()){
return response()->json($validator->errors()->toJson(), 400);
}
$doc = User::find($id);
if($request->hasFile('picture')){
// Get filename with the extension
$filenameWithExt = $request->file('picture')->getClientOriginalName();
// Get just filename
$filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
// Get just ext
$extension = $request->file('picture')->getClientOriginalExtension();
// Filename to store
$fileNameToStore= $filename.'_'.time().'.'.$extension;
// Upload Image
$path = $request->file('picture')->storeAs('public/images', $fileNameToStore);
} else {
$fileNameToStore = 'noimage.jpg';
}
$doc->name = $request->input('name');
$doc->email = $request->input('email');
$doc->phone = $request->input('phone');
if($request->hasFile('picture')){
$doc->picture = $fileNameToStore;
}
$doc->save();
return response()->json([
'message' => 'Success',
]);
}
When I pass the edited details, with name inclusive and email, and every required info.
When I run this code, I get this
"{\"name\":[\"The name field is required.\"],\"email\":[\"The email field is required.\"]}"
What is the issue here?
Somewhere you are wrong with sending data.
If you are using API middlewares then you will have to send data with Body Raw Json using Postman.
{
"info": {
"_postman_id": "9bc0d91b-83e7-4ccf-a561-ac191c21e869",
"name": "Laravel",
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
},
"item": [
{
"name": "http://20a11140.ngrok.io/api/userregister/24",
"request": {
"method": "PUT",
"header": [
{
"key": "Content-Type",
"name": "Content-Type",
"value": "application/json",
"type": "text"
}
],
"body": {
"mode": "raw",
"raw": "{\n\t\"name\" : \"BHaskar Rajoriya\",\n\t\"email\" : \"brn.rajoriya#gmail.com\"\n}",
"options": {
"raw": {
"language": "json"
}
}
},
"url": {
"raw": "http://20a11140.ngrok.io/api/userregister/24",
"protocol": "http",
"host": [
"20a11140",
"ngrok",
"io"
],
"path": [
"api",
"userregister",
"24"
]
}
},
"response": []
}
],
"protocolProfileBehavior": {}
}
Use this postman collection.
My json has two objects one is user_profile and other one is privileges but my validation is working only on user_profile object.
My json:
{
"user_profile": {
"email": "shahzad11#ovadamd.com",
"password": "admin123",
"password_confirmation": "admin123",
"status": 0,
"first_name": "Shahzad",
"middle_name": "Hussain",
"last_name": "Shah",
"date_of_birth": "2015-01-01",
"gender": "M",
"area_id": 1,
"address": "Minhatten NY",
"city": "New York",
"state": "Washington",
"zip": "12312",
"fax": "111-111-1111",
"phone_extension": "2471",
"work_phone": "111-111-1111",
"phone_no": "111-111-1111",
"emergency_contact": "111-111-1111",
"social_security": "111-11-1111",
"module_id": 2
},
"privileges": {
"is_super_admin": 1,
"facilities": [
{
"facility_id": 1,
"is_facility_supervisor": 0,
"speciality_id": 1,
"priv_id": "",
"role_id": 1
}
]
}
}
I want to validate my "privileges" object as well.
My controller:
public function register(Request $request) {
$body = $request->all();
$userProfile = $body['user_profile'];
$userPrev = $body['privileges'];
$userProfile['is_super_admin'] = $userPrev['is_super_admin'];
$facilities = $userPrev['facilities'];
$bodyObj = array_merge($userProfile, $userPrev);
$validator = UserValidations::validateUser($bodyObj);
if ($validator->fails()) {
return response([
'status' => false,
'message' => __('messages.validation_errors'),
'errors' => $validator->errors()->all()
], 200);
}
and my validateUser method:
public static function validateUser($data = [], $update = false) {
$rules = [
'first_name' => 'required|max:25',
'last_name' => 'required|max:25',
"address" => 'required|max:197',
"city" => 'required|max:20',
"zip" => 'required|max:5',
"phone_no" => 'required|max:12',
"area_id" => 'exists:areas,id',
"phone_extension" => 'min:3|max:5',
'gender' => 'in:M,F,X',
"work_phone" => 'max:12',
"emergency_contact" => 'max:12',
"fax" => "max:12",
"social_security" => 'required|max:11',
"module_id" => 'exists:modules,id',
// privileges object fields
"role_id" => 'required|exists:roles,id',
"facility_id" => 'required',
"speciality_id" => 'required'
];
if($update) {
$rules['id'] = 'required|exists:users,id';
} else {
$rules['email'] = 'required|email|unique:users';
$rules['password'] = 'required|min:6';
$rules['password_confirmation'] = 'required_with:password|min:6|same:password';
}
return $validator = Validator::make($data, $rules);
}
How i can put validation on privileges object?
Your help will be highly appreciated!
// privileges object fields
"facilities.*.role_id" => 'required|exists:roles,id',
"facilities.*.facility_id" => 'required',
"facilities.*.speciality_id" => 'required'
I think it will solve your problem
you can do this
Validator::make($request->all(),[
'your rules'])
I need to connect to an API to claim a voucher. At API doc I see:
POST /api/v1/vouchers/{voucherId}/claim
{
"Participant": {
"FirstName": "John",
"LastName": "James,
"Telephone": "08456 127 127",
"EmailAddress": "tim#asdasd.net",
"PostCode": "ASD 9HX",
"HouseNameNumber": "2",
"Street": "Bridge Road2",
"Locality": "LONDON",
"Town": "Aylesbury",
"County": "Bucks"
},
"ExperienceDate": "2017-10-01T00:00:00"
}
Based on this I write my function using Laravel framework:
public function testclaim()
{
$client = new GuzzleHttp\Client;
$headers = ['Content-Type' => 'application/json'];
try {
$res = $client->post('https://api.example.com/api/v1/vouchers/244775_2-H8SC/claim', [
'headers'=>$headers,
'auth' => [
'JAMES-JJ', 'ajhsdajsdhaj32423'
],
'json' => [
'Participant' => [
"FirstName"=> "asdasd",
"LastName"=> "asdasd",
"Telephone"=> "08456 127 127",
"EmailAddress"=> "tim#asdasd.net",
"PostCode"=> "HP18 9HX",
"HouseNameNumber"=> "1",
"Street"=> "Bridge Road",
"Locality"=> "Ickford",
"Town"=> "Aylesbury",
"County"=> "Bucks"
],
'ExperienceDate' => '2017-11-01T00:00:00'
]
]);
$res = json_decode($res->getBody()->getContents(), true);
return response()->json(['data' => $res]);
//dd($res);
}
catch (GuzzleHttp\Exception\ClientException $e) {
$response = $e->getResponse();
$result = json_decode($response->getBody()->getContents());
return response()->json(['data' => $result]);
}
}
Now when I run this function I just get:
{"data":null}
ANybody see what is wrong in my code?
How to solve this issue?
I also try without header to send a request but again I get the same response from API.
You don't need getContents() if you want to convert the body to an Array.
I am implementing an API using Laravel 5.4 . I want to send the title, description, time and user_id as a JSON and after that to get the JSON response with the input data.
Here is my code:
$title = $request->input('title');
$description = $request->input('description');
$time = $request->input('time');
$user_id = $request->input('user_id');
$meeting = [
'title' => $title,
'description' => $description,
'time' => $time,
'user_id' => $user_id,
'view_meeting' => [
'href' => 'api/v1/meeting/1',
'method' => 'GET1'
]
];
$response = [
'msg' => 'Meeting created',
'meeting' => $meeting
];
return response()->json($response, 201);
After running the server, I make a post request using POSTMAN (body->raw:)
{
"time": "201601301330CET",
"title": "Test meeting 2",
"description": "Test",
"user_id": 2
}
But it return this:
{
"msg": "Meeting created",
"meeting": {
"title": null,
"description": null,
"time": null,
"user_id": null,
"view_meeting": {
"href": "api/v1/meeting/1",
"method": "GET1"
}
}
}
Why the title, description, time and user_id fields are null?
You need to set Postman's content-type dropdown to JSON (application/json). Changing that setting changed the null values in the response to:
{
"msg": "Meeting created",
"meeting": {
"title": "Test meeting 2",
"description": "Test",
"time": "201601301330CET",
"user_id": 2,
"view_meeting": {
"href": "api/v1/meeting/1",
"method": "GET1"
}
}
}