簡體   English   中英

PHP curl - 從 API 獲得響應

[英]PHP curl - get response from API

所以,當談到 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);

如何執行“獲取”,以便如果付款被拒絕,我能夠以某種方式向用戶展示?

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

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