PayPal PHP-SDK:仅创建金额发票

时间:2018-03-26 21:54:33

标签: php paypal

使用PayPal PHP-SDK使用Amount Template创建发票需要花费数小时才能创建发票,但没有成功。

当我运行以下代码时:

$invoice
    ->setMerchantInfo(new MerchantInfo())
    ->setBillingInfo(array(new BillingInfo()))
    ->setNote("Medical Invoice 16 Jul, 2013 PST")
    ->setReference('Ref: 200 OK')
    ->setTotalAmount(new Currency())
    ->setTemplateId('TEMP-9WT50262M1352140L');

$invoice->getTotalAmount()
    ->setValue(99)
    ->setCurrency('EUR');
// setting BillInfo, MerchantInfo, (...) here with no problems

dd($invoice);

我得到以下所需的输出:

Invoice {#613 ▼
  -_propMap: array:16 [▼
    "merchant_info" => MerchantInfo {#627 ▶}
    "billing_info" => array:1 [▶]
    "note" => "Medical Invoice 16 Jul, 2013 PST"
    "reference" => "Ref: 200 OK"
    "total_amount" => Currency {#634 ▼
      -_propMap: array:2 [▼
        "currency" => "EUR"
        "value" => "99"
      ]
    }
    "logo_url" => "https://www.paypalobjects.com/webstatic/i/logo/rebrand/ppcom.svg"
    "id" => "INV2-FL5F-MFLZ-EHSU-LR64"
    "number" => "0031"
    "template_id" => "TEMP-6HG30056XX0172355"
    "status" => "DRAFT"
    "invoice_date" => "2018-03-26 PDT"
    "tax_calculated_after_discount" => false
    "tax_inclusive" => false
    "metadata" => Metadata {#636 ▶}
    "allow_tip" => false
    "links" => array:6 [▶]
  ]
}

但是当我在代码的末尾运行它时:

try {
    $invoice->create($apiContext);
    dd($invoice);
}catch (PayPal\Exception\PayPalConnectionException $ex) {
    echo $ex->getCode(); // Prints the Error Code
    echo $ex->getData(); // Prints the detailed error message 
    die($ex);
}catch (Exception $ex) {
    die($ex);
}

我得到以下输出:

// ...
    "total_amount" => Currency {#634 ▼
      -_propMap: array:2 [▼
        "currency" => "USD"
        "value" => "0.00"
      ]
    }
// ...

注意: 我正在关注此代码:http://paypal.github.io/PayPal-PHP-SDK/sample/doc/invoice/CreateInvoice.html

为什么它将货币和值重置为USD和0(分别)?我错过了什么?

注意:使用了TemplateId:TEMP-9WT50262M1352140L <=> Amount

谢谢。

1 个答案:

答案 0 :(得分:0)

我已经抓了整个网络搜索解决方案但没有成功。所以我一直在阅读API类,并试图了解它的setter和getter是如何工作的。

在PayPal API团队提供的官方代码示例中,项目设置如下:

$items = array();
$items[0] = new InvoiceItem();
$items[0]
    ->setName("Sutures")
    ->setQuantity(100)
    ->setUnitPrice(new Currency());

$items[0]->getUnitPrice()
    ->setCurrency("USD")
    ->setValue(5);

在课程PayPal\Api\InvoiceItem上,您可以根据API设置Unit of MeasurementAmountQuantityHours。所以我将Unit of Measurement设置为Amount,如下所示:

$items = array();
$items[0] = new InvoiceItem();
$items[0]
    ->setUnitOfMeasure('Amount')
    ->setName("Sutures")
    ->setQuantity(1)
    ->setUnitPrice(new Currency());

$items[0]->getUnitPrice()
    ->setCurrency("EUR")
    ->setValue(99); // 1 EUR

注意:即使您使用Unit of Measurement Amount,仍然可以使用setQuantity

这解决了我的问题。我希望它能解决别人的问题。

祝你好运!