【问题标题】:PHP curl - get response from APIPHP curl - 从 API 获取响应
【发布时间】:2020-05-04 20:54:03
【问题描述】:

所以,当谈到 PHP curl/json 时,我几乎是个初学者。我在将 Pinpayments.com 托管字段支付表单集成到我的新网站方面取得了相当不错的进展。 (此模块代码是在 Drupal 中构建的,不会影响我需要做的事情。)

当付款成功或付款被拒绝时,我需要做的是从 pinpayments 的响应中提取相关消息。目前,我的付款表格确实提交了付款,因为它显示在我的 pinpayments 仪表板上 - 但我不知道如何在卡被拒绝时显示错误消息。

这是我目前拥有的代码,这意味着我可以发布表单:

<?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);

我如何执行“GET”,以便在付款被拒绝时我能够以某种方式向用户显示?

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

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

【问题讨论】:

    标签: php curl get


    【解决方案1】:
    $token = "xyz";
    
    $error = array(
        "error" => "card_declined",
        "error_description" => "The card was declined",
        "charge_token"=> $token
        );
    
    echo(json_encode($error,true));
    

    【讨论】:

    • 谢谢,但它给了我 - 三个输出行的意外“:”,即“错误”:“card_declined”,
    • “输出”注释下方的文字是代码的结果,不需要使用...查看我的更新答案。
    • 谢谢,但它只是在 之前插入数组: {"error":"card_declined","error_description":"The card was denied","charge_token":""}
    • 如果您不希望输出出现在页面上,请将您的“echo”替换为“return”。
    • 就是这样 - 我希望在屏幕上向用户显示他们的付款已被拒绝的错误消息。
    猜你喜欢
    • 2015-05-21
    • 2018-07-23
    • 2019-08-15
    • 1970-01-01
    • 2023-01-29
    • 2017-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多