简体   繁体   English

PHP curl - 从 API 获得响应

[英]PHP curl - get response from API

So, I'm pretty much a beginner when it comes PHP curl/json.所以,当谈到 PHP curl/json 时,我几乎是一个初学者。 I've made pretty good progress with the integration of the Pinpayments.com hosted fields payment form in to my new site.我在将 Pinpayments.com 托管字段支付表单集成到我的新站点方面取得了不错的进展。 (This module code is built in Drupal, not that should affect what I need to do.) (此模块代码内置在 Drupal 中,不会影响我需要做的事情。)

What I need to do is pull the relevant message from the response from pinpayments when a payment is sucessful or a payment is declined.我需要做的是在付款成功或付款被拒绝时从 pinpayments 的响应中提取相关消息。 At present the payment form I have does submit the payment as it shows up on my pinpayments dashboard - but I can't figure out how to display an error message when the card is declined.目前,我的付款表格确实提交了付款,因为它显示在我的 pinpayments 仪表板上 - 但我无法弄清楚当卡被拒绝时如何显示错误消息。

This is the code I have at present that means I can post the form:这是我目前拥有的代码,这意味着我可以发布表单:

<?php



namespace Drupal\drupalup_simple_form\Form;

use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Ajax\AjaxResponse;
use Drupal\Core\Ajax\HtmlCommand;

/**
 * Our simple form class.
 */
class SimpleAjaxForm extends FormBase {



  /**
   * {@inheritdoc}
   */
  public function getFormId() {
    return 'payment_form';
  }


  /**
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state) {

    $form['#attributes'] = array('id' => array('payment_form'));


    $form['publishkey'] = [
      '#type' => 'textfield',
      '#title' => 'Key',
      '#id' => 'card_token',
      '#value' => 'pk_wkHM76EknXUavjrEYZlvNQ'
    ];

    $form['address_line1'] = [
      '#type' => 'textfield',
      '#title' => 'Address line 1',
      '#id' => 'address_line1'
    ];
      $form['address_line2'] = [
      '#type' => 'textfield',
      '#title' => 'Address line 2',
      '#id' => 'address_line2'
    ];
      $form['address_city'] = [
      '#type' => 'textfield',
      '#title' => 'City',
      '#id' => 'address_city'
    ];
      $form['address_postcode'] = [
      '#type' => 'textfield',
      '#title' => 'Postcode',
      '#id' => 'address_postcode'
    ];
      $form['address_state'] = [
      '#type' => 'textfield',
      '#title' => 'State',
      '#id' => 'address_state'
    ];
      $form['address_country'] = [
      '#type' => 'textfield',
      '#title' => 'Country',
      '#id' => 'address_country'
    ];



    $form['name'] = [
      '#type' => 'markup',
      '#markup' => '<div id="name"><!-- Hosted Fields will populate this with the "name" field --></div>',
    ];
    $form['errors_for_name'] = [
      '#type' => 'markup',
      '#markup' => '<div class="error_message" id="errors_for_name">&nbsp;</div>',
    ];


    $form['number'] = [
      '#type' => 'markup',
      '#markup' => '<div id="number"><!-- Hosted Fields will populate this with the "number" field --></div>',
    ];
    $form['errors_for_number'] = [
      '#type' => 'markup',
      '#markup' => '<div class="error_message" id="errors_for_number">&nbsp;</div>',
    ];


    $form['cvc'] = [
      '#type' => 'markup',
      '#markup' => '<div id="cvc"><!-- Hosted Fields will populate this with the "cvc" field --></div>',
    ];
    $form['errors_for_cvc'] = [
      '#type' => 'markup',
      '#markup' => '<div class="error_message" id="errors_for_cvc">&nbsp;</div',
    ];


    $form['expiry'] = [
      '#type' => 'markup',
      '#markup' => '<div id="expiry"><!-- Hosted Fields will populate this with the "expiry" field --></div>',
    ];  
    $form['errors_for_expiry'] = [
      '#type' => 'markup',
      '#markup' => '<div class="error_message" id="errors_for_expiry">&nbsp;</div>',
    ];  

    $form['actions'] = [
      '#type' => 'button',
      '#value' => 'Submit'
    ];

    return $form;
  }



  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {
  }

}

$ch = curl_init();


curl_setopt($ch, CURLOPT_URL, 'https://test-api.pinpayments.com/1/charges');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "amount=500&currency=AUD&description=test charge&email=roland@pinpayments.com&ip_address=203.192.1.172&card[number]=4100000000000001&card[expiry_month]=05&card[expiry_year]=2021&card[cvc]=123&card[name]=TIGER WOODS&card[address_line1]=$_POST[address_line1]&card[address_line2]=&card[address_city]=$_POST[address_city]&card[address_postcode]=$_POST[address_postcode]&card[address_state]= $_POST[address_state]&card[address_country]=$_POST[address_country]&metadata[OrderNumber]=123456&metadata[CustomerName]=Roland Robot");



curl_setopt($ch, CURLOPT_USERPWD, 'REMOVED MY SECRET KEY' . ':' . '');

$headers = array();
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
if (curl_errno($ch)) {
    echo 'Error:' . curl_error($ch);
}

curl_close($ch);

How do I do a 'GET' so that if the payment is declined I'm somehow able to show this to the user?如何执行“获取”,以便如果付款被拒绝,我能够以某种方式向用户展示?

{
  "error": "card_declined",
  "error_description": "The card was declined",
  "charge_token": "ch_lfUYEBK14zotCTykezJkfg"
}

https://pinpayments.com/developers/api-reference/charges https://pinpayments.com/developers/api-reference/charges

$token = "xyz";

$error = array(
    "error" => "card_declined",
    "error_description" => "The card was declined",
    "charge_token"=> $token
    );

echo(json_encode($error,true));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM